Skip to content

Instantly share code, notes, and snippets.

@ITKewai
Created April 10, 2022 13:47
Show Gist options
  • Save ITKewai/869e5848c89b25359bd21cc184e1b9ce to your computer and use it in GitHub Desktop.
Save ITKewai/869e5848c89b25359bd21cc184e1b9ce to your computer and use it in GitHub Desktop.
import discord
from discord.ext import commands
from discord import app_commands, Interaction
from discord.app_commands import AppCommandError, Command, ContextMenu
from typing import Union
class COG_NAME(commands.Cog):
def __init__(self, bot):
self.bot = bot
...
async def global_app_command_error_handler(bot: commands.Bot):
@bot.tree.error
async def app_command_error(
interaction: Interaction,
command: Union[Command, ContextMenu],
error: AppCommandError
):
# print(interaction, command, error)
if isinstance(error, app_commands.CommandInvokeError):
print(error.original, error.command)
elif isinstance(error, app_commands.TransformerError):
print(error.value, error.type, error.transformer)
elif isinstance(error, app_commands.CheckFailure):
if isinstance(error, app_commands.NoPrivateMessage):
pass
elif isinstance(error, app_commands.MissingRole):
print(error.missing_role)
elif isinstance(error, app_commands.MissingAnyRole):
for role in error.missing_roles:
print(role)
elif isinstance(error, app_commands.MissingPermissions):
for permission in error.missing_permissions:
print(permission)
elif isinstance(error, app_commands.BotMissingPermissions):
for permission in error.missing_permissions:
print(permission)
elif isinstance(error, app_commands.CommandOnCooldown):
print(error.cooldown, error.retry_after)
elif isinstance(error, app_commands.CommandLimitReached):
print(error.type, error.guild_id, error.limit)
elif isinstance(error, app_commands.CommandAlreadyRegistered):
print(error.name, error.guild_id)
elif isinstance(error, app_commands.CommandSignatureMismatch):
print(error.command)
elif isinstance(error, app_commands.CommandNotFound):
print(error.name, error.parents, error.type)
async def setup(bot):
await global_app_command_error_handler(bot=bot)
await bot.add_cog(COG_NAME(bot))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment