
from discord import app_commands
from discord.ext import commands

import logging
import sys
import traceback

import sentry_sdk


class CommandTree(app_commands.CommandTree):

    async def on_error(self, interaction, error):
        if (
            isinstance(error, app_commands.TransformerError) and
            isinstance(
                error.__cause__, commands.PartialEmojiConversionFailure
            )
        ):
            ctx = await interaction.client.get_context(interaction)
            await ctx.embed_reply(
                f"{ctx.bot.error_emoji} "
                f"`{error.value}` doesn't seem to be a custom emoji"
            )
            return

        sentry_sdk.capture_exception(error)
        print(
            f"Ignoring exception in slash command {interaction.command.name}",
            # TODO: Use full name
            file = sys.stderr
        )
        traceback.print_exception(
            type(error), error, error.__traceback__, file = sys.stderr
        )
        logging.getLogger("errors").error(
            "Uncaught exception\n",
            exc_info = (type(error), error, error.__traceback__)
        )

@@ -221,16 +221,899 @@
 error):%0A
+        # Command Invoke Error%0A        if isinstance(error, app_commands.CommandInvokeError):%0A            # Bot missing permissions%0A            if isinstance(error.original, commands.BotMissingPermissions):%0A                bot = interaction.client%0A                ctx = await bot.get_context(interaction)%0A                missing_permissions = bot.inflect_engine.join(%5B%0A                    f%22%60%7Bpermission%7D%60%22%0A                    for permission in error.original.missing_permissions%0A                %5D)%0A                permission_declension = bot.inflect_engine.plural(%0A                    'permission', len(error.original.missing_permissions)%0A                )%0A                await ctx.embed_reply(%0A                    %22I don't have permission to do that here%5Cn%22%0A                    f%22I need the %7Bmissing_permissions%7D %7Bpermission_declension%7D%22%0A                )%0A                return%0A%0A
         

Handle bot missing permissions for app commands