Add ErrorHandler cog

This commit is contained in:
ekzyis 2021-09-24 22:26:49 +02:00
parent d88ab63c2b
commit 65ddea7f27
2 changed files with 27 additions and 0 deletions

View File

@ -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:

25
src/error.py Normal file
View File

@ -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))