import yaml # YAML parser for Bot config files import asyncio # Discord Py Dependency import discord # Main Lib from discord.ext import commands # Commands module from discord_slash import SlashCommand, SlashContext, cog_ext, utils # Slash Command Library from discord_slash.utils.manage_commands import create_choice, create_option # Slash Command features import logging # logger and handler from bot import configFile, yaml_load, yaml_dump #### Error Handler Event Listener class on_command_error(commands.Cog, name='On Command Error'): def __init__(self, client): self.client = client @commands.Cog.listener() async def on_command_error(self, ctx, error): if isinstance(error, discord.DiscordException): if isinstance(error, commands.CommandNotFound): print(f'Error: User {ctx.author.name}#{ctx.author.discriminator} / {ctx.author.display_name} entered an invalid command <{ctx.message.clean_content}> in the guild {ctx.guild.name}.') await ctx.reply(f'```Error: This is not a valid command.```') elif isinstance(error, commands.CheckFailure): print(f'Error: User {ctx.author.name}#{ctx.author.discriminator} / {ctx.author.display_name} is not authorised to issue the command <{ctx.command.name}> in the guild {ctx.guild.name}.') await ctx.reply(f'```Error: You are not authorised to issue this command.```') else: print(f'User {ctx.author.name}#{ctx.author.discriminator} / {ctx.author.display_name} received error: "{error}" when attempting to issue command <{ctx.command.name}> in the guild {ctx.guild.name}.') await ctx.reply(f'```Error: {error}```') def setup(client): client.add_cog(on_command_error(client))