From 4e9ffd2954e42604596d5ea666467d3042e2483c Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Mon, 12 Apr 2021 17:27:41 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A5=85=20-=20Catching=20command=20errors?= =?UTF-8?q?=20better=20than=20ever?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/errors.py | 6 ++++++ python/main.py | 11 ++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 python/errors.py diff --git a/python/errors.py b/python/errors.py new file mode 100644 index 0000000..bc598ec --- /dev/null +++ b/python/errors.py @@ -0,0 +1,6 @@ +from discord.ext import commands + +errors = {commands.MissingRequiredArgument:"A required argument is missing !", + commands.TooManyArguments:"You entered too many arguments !", + commands.NotOwner:"Only the owner of the bot can use this command... *Where did you get it ?*", + commands.BotMissingPermissions:"I'm missing some permissions to execute this command !"} diff --git a/python/main.py b/python/main.py index cc6c778..1c63aa9 100644 --- a/python/main.py +++ b/python/main.py @@ -5,7 +5,7 @@ import discord from discord.ext import commands from miscellaneous import spellchecker from private import TOKEN_BOT - +from errors import errors print('Loading started') @@ -39,9 +39,14 @@ async def on_command_error(ctx, error): ' \'' + str(error) + '\' from ' + str(ctx.author) + ' on ' + (ctx.message.guild.name if ctx.message.guild is not None else 'DMs') + '.') if isinstance(error, commands.CommandNotFound): - await ctx.send(spellchecker(str(ctx.message.content)[1:], commandlist)) + msg = spellchecker(str(ctx.message.content)[1:], commandlist) else: - await ctx.send(error) + try: + msg = errors[type(error)] + "\nPlease follow this pattern :\n``" + \ + "$" + ctx.command.name + " " + ctx.command.signature + "``" + except KeyError: + msg = "Something really bad happened (" + str(error) + ")." + await ctx.send(msg) def init():