-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from royce-mathew/master
Bug Fixes, Moderation Logs, New Help Command
- Loading branch information
Showing
7 changed files
with
195 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import discord | ||
from discord.ext import commands | ||
import sys | ||
import traceback | ||
|
||
|
||
# Credit to https://gist.github.com/EvieePy for creating this method of Exception handling through one Cog | ||
class ErrorHandler(commands.Cog): | ||
def __init__(self, client: discord.Client): | ||
self.client = client | ||
|
||
@commands.Cog.listener() | ||
async def on_command_error(self, ctx: commands.Context, error): | ||
"""The event triggered when an error is raised while invoking a command. | ||
Parameters | ||
------------ | ||
ctx: commands.Context | ||
The context used for command invocation. | ||
error: commands.CommandError | ||
The Exception raised. | ||
""" | ||
|
||
# This prevents any commands with local handlers being handled here in on_command_error. | ||
if hasattr(ctx.command, 'on_error'): | ||
return | ||
|
||
# This prevents any cogs with an overwritten cog_command_error being handled here. | ||
cog = ctx.cog | ||
if cog: | ||
if cog._get_overridden_method(cog.cog_command_error) is not None: | ||
return | ||
|
||
ignored = (commands.CommandNotFound, ) | ||
|
||
# Allows us to check for original exceptions raised and sent to CommandInvokeError. | ||
# If nothing is found. We keep the exception passed to on_command_error. | ||
error = getattr(error, 'original', error) | ||
|
||
# Anything in ignored will return and prevent anything happening. | ||
if isinstance(error, ignored): | ||
return | ||
|
||
if isinstance(error, commands.DisabledCommand): | ||
await ctx.send(f'{ctx.command} has been disabled.') | ||
|
||
elif isinstance(error, commands.NoPrivateMessage): | ||
try: | ||
await ctx.author.send(f'{ctx.command} can not be used in Private Messages.') | ||
except discord.HTTPException: | ||
pass | ||
|
||
# For this error example we check to see where it came from... | ||
elif isinstance(error, commands.BadArgument): | ||
if ctx.command.qualified_name == 'tag list': # Check if the command being invoked is 'tag list' | ||
await ctx.send('I could not find that member. Please try again.') | ||
|
||
else: | ||
# All other Errors not returned come here. And we can just print the default TraceBack. | ||
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr) | ||
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr) | ||
|
||
|
||
async def setup(client: discord.Client): | ||
await client.add_cog(ErrorHandler(client)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters