modmail-dev / Modmail

A Discord bot that functions as a shared inbox between staff and members, similar to Reddit's Modmail.
https://docs.modmail.dev
GNU Affero General Public License v3.0
1.58k stars 4.59k forks source link

Fix bug with `?plugin update`. #3295

Closed Jerrie-Aries closed 11 months ago

Jerrie-Aries commented 1 year ago

This PR fixes bug occured when running ?plugin update command and only if the updated plugin is broken.

Jerrie-Aries commented 1 year ago

Traceback:

Traceback (most recent call last):
  File "/User/Projects/modmail/cogs/plugins.py", line 259, in load_plugin
    await self.bot.load_extension(plugin.ext_string)
  File "/User/Projects/modmail/.venv/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 1012, in load_extension
    await self._load_from_module_spec(spec, name)
  File "/User/Projects/modmail/.venv/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 937, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'plugins.@local.supportutils.supportutils' raised an error: SyntaxError: invalid syntax (supportutils.py, line 22)
2023-07-22 14:55:42 __main__[1531] - ERROR: Unexpected exception:
Traceback (most recent call last):
  File "/User/Projects/modmail/.venv/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 934, in _load_from_module_spec
    spec.loader.exec_module(lib)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 846, in exec_module
  File "<frozen importlib._bootstrap_external>", line 983, in get_code
  File "<frozen importlib._bootstrap_external>", line 913, in source_to_code
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/User/Projects/modmail/plugins/@local/supportutils/supportutils.py", line 22
    .
    ^
SyntaxError: invalid syntax

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/User/Projects/modmail/cogs/plugins.py", line 259, in load_plugin
    await self.bot.load_extension(plugin.ext_string)
  File "/User/Projects/modmail/.venv/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 1012, in load_extension
    await self._load_from_module_spec(spec, name)
  File "/User/Projects/modmail/.venv/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 937, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'plugins.@local.supportutils.supportutils' raised an error: SyntaxError: invalid syntax (supportutils.py, line 22)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/User/Projects/modmail/cogs/plugins.py", line 499, in update_plugin
    await self.load_plugin(plugin)
  File "/User/Projects/modmail/cogs/plugins.py", line 265, in load_plugin
    raise InvalidPluginError("Cannot load extension, plugin invalid.") from exc
cogs.plugins.InvalidPluginError: Cannot load extension, plugin invalid.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/User/Projects/modmail/.venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 190, in wrapped
    ret = await coro(*args, **kwargs)
  File "/User/Projects/modmail/cogs/plugins.py", line 530, in plugins_update
    await self.update_plugin(ctx, plugin_name)
  File "/User/Projects/modmail/cogs/plugins.py", line 505, in update_plugin
    self.bot.config["plugins"].remove(plugin_name)
ValueError: list.remove(x): x not in list

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/User/Projects/modmail/.venv/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 1347, in invoke
    await ctx.command.invoke(ctx)
  File "/User/Projects/modmail/.venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 1605, in invoke
    await ctx.invoked_subcommand.invoke(ctx)
  File "/User/Projects/modmail/.venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 986, in invoke
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
  File "/User/Projects/modmail/.venv/lib/python3.9/site-packages/discord/ext/commands/core.py", line 199, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ValueError: list.remove(x): x not in list

This occured when I used command ?plugin update local/supportutils. And the plugin file had a syntax error.

This is the part that we want to solve:

self.bot.config["plugins"].remove(plugin_name)
ValueError: list.remove(x): x not in list

Because of this error, the bot fails to remove the plugin from config.