From 65ddea7f2769081d9bc6bd5d0575dc964b451a6f Mon Sep 17 00:00:00 2001 From: ekzyis Date: Fri, 24 Sep 2021 22:26:49 +0200 Subject: [PATCH] Add ErrorHandler cog --- src/bot.py | 2 ++ src/error.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/error.py diff --git a/src/bot.py b/src/bot.py index 826ab6b..927ac97 100644 --- a/src/bot.py +++ b/src/bot.py @@ -5,6 +5,7 @@ from discord.ext import commands from dotenv import load_dotenv from yt import YTDLSource +from error import ErrorHandler load_dotenv() @@ -49,6 +50,7 @@ if __name__ == "__main__": print('------') bot.add_cog(Music(bot)) + bot.add_cog(ErrorHandler(bot)) token = os.environ.get("BOT_TOKEN", None) if not token: diff --git a/src/error.py b/src/error.py new file mode 100644 index 0000000..553dfae --- /dev/null +++ b/src/error.py @@ -0,0 +1,25 @@ +from discord.ext import commands + + +class ErrorHandler(commands.Cog): + """A cog for global error handling.""" + + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.Cog.listener() + async def on_command_error(self, ctx: commands.Context, error: commands.CommandError): + if isinstance(error, commands.CommandNotFound): + return + elif isinstance(error, commands.MissingPermissions): + message = "You are missing the required permissions to run this command!" + elif isinstance(error, commands.UserInputError): + message = "Something about your input was wrong, please check your input and try again!" + else: + message = "Oh no! Something went wrong while running the command!" + + await ctx.send(message) + + +def setup(bot: commands.Bot): + bot.add_cog(ErrorHandler(bot))