From 509aba4941068ef6626935928e6908f4572cd506 Mon Sep 17 00:00:00 2001 From: DerpDays <34582078+DerpDays@users.noreply.github.com> Date: Sun, 14 Oct 2018 14:39:19 +0100 Subject: Added a error handler! --- bot/cogs/error_handler.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 bot/cogs/error_handler.py (limited to 'bot/cogs/error_handler.py') diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py new file mode 100644 index 00000000..3f228b14 --- /dev/null +++ b/bot/cogs/error_handler.py @@ -0,0 +1,51 @@ +import discord +from discord.ext import commands +import asyncio + +import sys +import traceback +import math + +class CommandErrorHandler: + def __init__(self, bot): + self.bot = bot + + async def on_command_error(self, ctx, error): + if hasattr(ctx.command, 'on_error'): + return + error = getattr(error, 'original', error) + + if isinstance(error, commands.CommandNotFound): + return + + if isinstance(error, commands.UserInputError): + return await ctx.send(':no_entry: The command you specified failed to run because the arguments you provided were invalid.') + + if isinstance(error, commands.CommandOnCooldown): + return await ctx.send("This command is on cooldown, please retry in {}s.".format(math.ceil(error.retry_after))) + + if isinstance(error, commands.DisabledCommand): + return await ctx.send(':no_entry: This command has been disabled.') + + if isinstance(error, commands.NoPrivateMessage): + try: + return await ctx.author.send(':no_entry: This command can only be used inside a server.') + except: + pass + + if isinstance(error, commands.BadArgument): + if ctx.command.qualified_name == 'tag list': + return await ctx.send('I could not find that member. Please try again.') + else: + return await ctx.send("The argument you provided was invalid.") + + if isinstance(error, commands.CheckFailure): + await ctx.send(":no_entry: You do not have permission to use this command.") + return + + print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr) + traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr) + + +def setup(bot): + bot.add_cog(CommandErrorHandler(bot)) -- cgit v1.2.3 From a23d3b8b777566d7676f073133c91b13c8fa176d Mon Sep 17 00:00:00 2001 From: DerpDays <34582078+DerpDays@users.noreply.github.com> Date: Sun, 14 Oct 2018 21:54:47 +0100 Subject: Fixed the PEP8 errors, ill add logging soon. --- bot/cogs/error_handler.py | 54 +++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 20 deletions(-) (limited to 'bot/cogs/error_handler.py') diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index 3f228b14..7d881617 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -1,50 +1,64 @@ import discord from discord.ext import commands import asyncio - import sys import traceback import math + class CommandErrorHandler: def __init__(self, bot): self.bot = bot - async def on_command_error(self, ctx, error): if hasattr(ctx.command, 'on_error'): return error = getattr(error, 'original', error) - if isinstance(error, commands.CommandNotFound): return - if isinstance(error, commands.UserInputError): - return await ctx.send(':no_entry: The command you specified failed to run because the arguments you provided were invalid.') - + return await ctx.send( + ":no_entry: The command you specified failed to run." + "This is because the arguments you provided were invalid." + ) if isinstance(error, commands.CommandOnCooldown): - return await ctx.send("This command is on cooldown, please retry in {}s.".format(math.ceil(error.retry_after))) - + return await ctx.send( + "This command is on cooldown," + "please retry in {}s.".format(math.ceil(error.retry_after)) + ) if isinstance(error, commands.DisabledCommand): - return await ctx.send(':no_entry: This command has been disabled.') - + return await ctx.send( + ":no_entry: This command has been disabled." + ) if isinstance(error, commands.NoPrivateMessage): try: - return await ctx.author.send(':no_entry: This command can only be used inside a server.') + return await ctx.author.send( + ":no_entry: This command can only be used inside a server." + ) except: pass - if isinstance(error, commands.BadArgument): if ctx.command.qualified_name == 'tag list': - return await ctx.send('I could not find that member. Please try again.') + return await ctx.send( + "I could not find that member. Please try again." + ) else: - return await ctx.send("The argument you provided was invalid.") - + return await ctx.send( + "The argument you provided was invalid." + ) if isinstance(error, commands.CheckFailure): - await ctx.send(":no_entry: You do not have permission to use this command.") - return - - print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr) - traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr) + return await ctx.send( + ":no_entry: You do not have permission to use this command." + ) + print( + "Ignoring exception in command {}:".format(ctx.command), + file=sys.stderr + ) + traceback.print_exception( + type(error), + error, + error.__traceback__, + file=sys.stderr + ) def setup(bot): -- cgit v1.2.3 From ba5c07b5c224992fe311c622e3efe6c67c44e0a6 Mon Sep 17 00:00:00 2001 From: DerpDays <34582078+DerpDays@users.noreply.github.com> Date: Sun, 14 Oct 2018 21:58:01 +0100 Subject: Forgot some commas --- bot/cogs/error_handler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/cogs/error_handler.py') diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index 7d881617..e37e8272 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -17,12 +17,12 @@ class CommandErrorHandler: return if isinstance(error, commands.UserInputError): return await ctx.send( - ":no_entry: The command you specified failed to run." + ":no_entry: The command you specified failed to run.", "This is because the arguments you provided were invalid." ) if isinstance(error, commands.CommandOnCooldown): return await ctx.send( - "This command is on cooldown," + "This command is on cooldown,", "please retry in {}s.".format(math.ceil(error.retry_after)) ) if isinstance(error, commands.DisabledCommand): -- cgit v1.2.3 From 7807eb7aeaffa25cb9601a655f545ee88c0ad185 Mon Sep 17 00:00:00 2001 From: DerpDays <34582078+DerpDays@users.noreply.github.com> Date: Sun, 14 Oct 2018 22:10:28 +0100 Subject: Ignore that last commit lol --- bot/cogs/error_handler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bot/cogs/error_handler.py') diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index e37e8272..268ed35a 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -17,12 +17,12 @@ class CommandErrorHandler: return if isinstance(error, commands.UserInputError): return await ctx.send( - ":no_entry: The command you specified failed to run.", + ":no_entry: The command you specified failed to run." + "This is because the arguments you provided were invalid." ) if isinstance(error, commands.CommandOnCooldown): return await ctx.send( - "This command is on cooldown,", + "This command is on cooldown," + "please retry in {}s.".format(math.ceil(error.retry_after)) ) if isinstance(error, commands.DisabledCommand): -- cgit v1.2.3 From 477bb2e0130401d52ed01bdb797c75347e0b186e Mon Sep 17 00:00:00 2001 From: DerpDays <34582078+DerpDays@users.noreply.github.com> Date: Sun, 14 Oct 2018 22:15:38 +0100 Subject: I believe this fixes the flake8 issues --- bot/cogs/error_handler.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'bot/cogs/error_handler.py') diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index 268ed35a..72ce5f59 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -1,6 +1,4 @@ -import discord from discord.ext import commands -import asyncio import sys import traceback import math @@ -9,6 +7,7 @@ import math class CommandErrorHandler: def __init__(self, bot): self.bot = bot + async def on_command_error(self, ctx, error): if hasattr(ctx.command, 'on_error'): return @@ -23,19 +22,16 @@ class CommandErrorHandler: if isinstance(error, commands.CommandOnCooldown): return await ctx.send( "This command is on cooldown," + - "please retry in {}s.".format(math.ceil(error.retry_after)) + " please retry in {}s.".format(math.ceil(error.retry_after)) ) if isinstance(error, commands.DisabledCommand): return await ctx.send( ":no_entry: This command has been disabled." ) if isinstance(error, commands.NoPrivateMessage): - try: - return await ctx.author.send( - ":no_entry: This command can only be used inside a server." - ) - except: - pass + return await ctx.author.send( + ":no_entry: This command can only be used inside a server." + ) if isinstance(error, commands.BadArgument): if ctx.command.qualified_name == 'tag list': return await ctx.send( -- cgit v1.2.3 From 3f2baef46053c0f26eb0bfa7e724ccdcb700ca59 Mon Sep 17 00:00:00 2001 From: DerpDays <34582078+DerpDays@users.noreply.github.com> Date: Sun, 14 Oct 2018 22:42:49 +0100 Subject: Added logging i believe, tell me if i messed up anywhere! --- bot/cogs/error_handler.py | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'bot/cogs/error_handler.py') diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index 72ce5f59..17254527 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -2,6 +2,7 @@ from discord.ext import commands import sys import traceback import math +import logging class CommandErrorHandler: @@ -10,45 +11,86 @@ class CommandErrorHandler: async def on_command_error(self, ctx, error): if hasattr(ctx.command, 'on_error'): + logging.debug( + "A command error occured but " + + "the command had it's own error handler" + ) return error = getattr(error, 'original', error) if isinstance(error, commands.CommandNotFound): + logging.debug( + f"{ctx.author} called '{ctx.message.content}' " + + "but no command was found" + ) return if isinstance(error, commands.UserInputError): + logging.debug( + f"{ctx.author} called the command '{ctx.command}' " + + "but entered invalid input!" + ) return await ctx.send( ":no_entry: The command you specified failed to run." + "This is because the arguments you provided were invalid." ) if isinstance(error, commands.CommandOnCooldown): + logging.debug( + f"{ctx.author} called the command '{ctx.command}' " + + "but they were on cooldown!" + ) return await ctx.send( "This command is on cooldown," + " please retry in {}s.".format(math.ceil(error.retry_after)) ) if isinstance(error, commands.DisabledCommand): + logging.debug( + f"{ctx.author} called the command '{ctx.command}' " + + "but the command was disabled!" + ) return await ctx.send( ":no_entry: This command has been disabled." ) if isinstance(error, commands.NoPrivateMessage): + logging.debug( + f"{ctx.author} called the command '{ctx.command}' " + + "in a private message however the command was guild only!" + ) return await ctx.author.send( ":no_entry: This command can only be used inside a server." ) if isinstance(error, commands.BadArgument): if ctx.command.qualified_name == 'tag list': + logging.debug( + f"{ctx.author} called the command '{ctx.command}' " + + "but entered an invalid user!" + ) return await ctx.send( "I could not find that member. Please try again." ) else: + logging.debug( + f"{ctx.author} called the command '{ctx.command}' " + + "but entered a bad argument!" + ) return await ctx.send( "The argument you provided was invalid." ) if isinstance(error, commands.CheckFailure): + logging.debug( + f"{ctx.author} called the command '{ctx.command}' " + + "but the checks failed!" + ) return await ctx.send( - ":no_entry: You do not have permission to use this command." + ":no_entry: You are not authorized to use this command." ) print( "Ignoring exception in command {}:".format(ctx.command), file=sys.stderr ) + logging.warning( + f"{ctx.author} called the command '{ctx.command}' " + + "however the command failed to run with the error:" + + f"-------------\n{error}" + ) traceback.print_exception( type(error), error, -- cgit v1.2.3 From 6067d895a32acaa65f97c80153f62fee8771df3e Mon Sep 17 00:00:00 2001 From: DerpDays <34582078+DerpDays@users.noreply.github.com> Date: Tue, 30 Oct 2018 17:11:18 +0000 Subject: Made the local imports come before the external ones --- bot/cogs/error_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/cogs/error_handler.py') diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index 17254527..91bbf2e8 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -1,8 +1,8 @@ -from discord.ext import commands import sys import traceback import math import logging +from discord.ext import commands class CommandErrorHandler: -- cgit v1.2.3 From 80bd798be84c819542756f425632110a8dc4708e Mon Sep 17 00:00:00 2001 From: DerpDays <34582078+DerpDays@users.noreply.github.com> Date: Tue, 30 Oct 2018 17:34:44 +0000 Subject: Litterally got no idea why it failed checks last time --- bot/cogs/error_handler.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'bot/cogs/error_handler.py') diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index 91bbf2e8..9f0da2e0 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -6,23 +6,25 @@ from discord.ext import commands class CommandErrorHandler: + """A error handler for the PythonDiscord server!""" + def __init__(self, bot): self.bot = bot async def on_command_error(self, ctx, error): + """Activates when a command opens an error""" + if hasattr(ctx.command, 'on_error'): - logging.debug( + return logging.debug( "A command error occured but " + "the command had it's own error handler" ) - return error = getattr(error, 'original', error) if isinstance(error, commands.CommandNotFound): - logging.debug( + return logging.debug( f"{ctx.author} called '{ctx.message.content}' " + "but no command was found" ) - return if isinstance(error, commands.UserInputError): logging.debug( f"{ctx.author} called the command '{ctx.command}' " + -- cgit v1.2.3