Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unhandled_by_cog parameter to ModmailBot.on_command_error #3178

Closed

Conversation

Jerrie-Aries
Copy link
Contributor

As per title, this allows plugin developers to have their own custom exceptions and have more control on how to handle errors from their plugins without flooding the console, etc.

Example in plugin file:

  • Specific command error handler.
# imports

class MyPlugin(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    @checks.has_permissions(PermissionLevel.OWNER)
    async def test(self, ctx, *, value: str = None):
        if value == "1":
            raise TypeError("TypeError.")
        elif value == "2":
            raise commands.BadArgument("BadArgument.")
        else:
            await ctx.send("None.")

    @test.error
    async def test_error(self, ctx, error):
        if hasattr(error, "original"):
            # usually (I'm not really sure of this) if the exception is raised inside the command and the type of exception is not inherited
            # from .CommandError, the type of error here would be .CommandInvokeError and the original exception
            # can be retrieved from the .original attribute.
            error = error.original
        if isinstance(error, TypeError):
            await ctx.send(str(error))
        else:
            await self.bot.on_command_error(ctx, error, unhandled_by_cog=True)
  • Cog error handler, this will catch every command error raised from the plugin.
# imports

class MyPlugin(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    @checks.has_permissions(PermissionLevel.OWNER)
    async def test(self, ctx, *, value: str = None):
        if value == "1":
            raise TypeError("TypeError.")
        elif value == "2":
            raise commands.BadArgument("BadArgument.")
        else:
            await ctx.send("None.")

    async def cog_command_error(self, ctx, error):
        if hasattr(error, "original"):
            error = error.original
        if isinstance(error, TypeError):
            await ctx.send(str(error))
        else:
            await self.bot.on_command_error(ctx, error, unhandled_by_cog=True)

@ghost
Copy link

ghost commented Aug 24, 2022

Does this mean the bot will handle all errors as it does right now as long as there is no error handler in the cog and if there is a error handler in the cog it will respect the cogs error handler over the eh of the bot?

@Jerrie-Aries
Copy link
Contributor Author

Yes Cordilla. The bot will still handle all errors like before.

@fourjr
Copy link
Collaborator

fourjr commented Sep 6, 2022

LGTM Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] ModmailBot.on_command_error does not respect cog's cog_command_error error handler.
2 participants