diff options
author | 2019-09-14 18:42:35 -0700 | |
---|---|---|
committer | 2019-09-14 19:05:44 -0700 | |
commit | c020004cb75414865583d1c200cc9a6df1a49cbb (patch) | |
tree | 99ed4629e426216138a437eb919894baef724ae4 | |
parent | Ignore handled errors by checking for a "handled" attribute (diff) |
Remove most local error handlers & handle InChannelCheckFailure globally
* Add error handler to ignore InChannelCheckFailure in the verification
cog
* Raise InChannelCheckFailure instead of MissingPermissions in !user
* Send message instead of raising BadArgument in !user to prevent help
message from being shown in such case
* Clean up !user command
-rw-r--r-- | bot/cogs/error_handler.py | 3 | ||||
-rw-r--r-- | bot/cogs/information.py | 57 | ||||
-rw-r--r-- | bot/cogs/snekbox.py | 31 | ||||
-rw-r--r-- | bot/cogs/utils.py | 12 | ||||
-rw-r--r-- | bot/cogs/verification.py | 8 |
5 files changed, 30 insertions, 81 deletions
diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index 033a49d39..cfcba6f26 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -18,6 +18,7 @@ from discord.ext.commands import Bot, Context from bot.api import ResponseCodeError from bot.constants import Channels +from bot.decorators import InChannelCheckFailure log = logging.getLogger(__name__) @@ -78,6 +79,8 @@ class ErrorHandler: f"{ctx.message.author} is missing permissions to invoke command {command}: " f"{e.missing_perms}" ) + elif isinstance(e, InChannelCheckFailure): + await ctx.send(e) elif isinstance(e, (CheckFailure, CommandOnCooldown, DisabledCommand)): log.debug( f"Command {command} invoked by {ctx.message.author} with error " diff --git a/bot/cogs/information.py b/bot/cogs/information.py index a2585f395..320750a24 100644 --- a/bot/cogs/information.py +++ b/bot/cogs/information.py @@ -1,16 +1,13 @@ import logging -import random import textwrap from discord import CategoryChannel, Colour, Embed, Member, TextChannel, VoiceChannel -from discord.ext.commands import ( - BadArgument, Bot, CommandError, Context, MissingPermissions, command -) +from discord.ext.commands import Bot, Context, command from bot.constants import ( - Channels, Emojis, Keys, MODERATION_ROLES, NEGATIVE_REPLIES, STAFF_ROLES + Channels, Emojis, Keys, MODERATION_ROLES, STAFF_ROLES ) -from bot.decorators import with_role +from bot.decorators import InChannelCheckFailure, with_role from bot.utils.checks import with_role_check from bot.utils.time import time_since @@ -131,30 +128,25 @@ class Information: Returns info about a user. """ - # Do a role check if this is being executed on - # someone other than the caller - if user and user != ctx.author: - if not with_role_check(ctx, *MODERATION_ROLES): - raise BadArgument("You do not have permission to use this command on users other than yourself.") + if user is None: + user = ctx.author + + # Do a role check if this is being executed on someone other than the caller + if user != ctx.author and not with_role_check(ctx, *MODERATION_ROLES): + await ctx.send("You may not use this command on users other than yourself.") + return - # Non-moderators may only do this in #bot-commands and can't see - # hidden infractions. + # Non-moderators may only do this in #bot-commands and can't see hidden infractions. if not with_role_check(ctx, *STAFF_ROLES): if not ctx.channel.id == Channels.bot: - raise MissingPermissions("You can't do that here!") + raise InChannelCheckFailure(Channels.bot) # Hide hidden infractions for users without a moderation role hidden = False - # Validates hidden input - hidden = str(hidden) - - if user is None: - user = ctx.author - # User information created = time_since(user.created_at, max_units=3) - name = f"{user.name}#{user.discriminator}" + name = str(user) if user.nick: name = f"{user.nick} ({name})" @@ -162,15 +154,13 @@ class Information: joined = time_since(user.joined_at, precision="days") # You're welcome, Volcyyyyyyyyyyyyyyyy - roles = ", ".join( - role.mention for role in user.roles if role.name != "@everyone" - ) + roles = ", ".join(role.mention for role in user.roles if role.name != "@everyone") # Infractions infractions = await self.bot.api_client.get( 'bot/infractions', params={ - 'hidden': hidden, + 'hidden': str(hidden), 'user__id': str(user.id) } ) @@ -209,23 +199,6 @@ class Information: await ctx.send(embed=embed) - @user_info.error - async def user_info_command_error(self, ctx: Context, error: CommandError): - embed = Embed(colour=Colour.red()) - - if isinstance(error, BadArgument): - embed.title = random.choice(NEGATIVE_REPLIES) - embed.description = str(error) - await ctx.send(embed=embed) - - elif isinstance(error, MissingPermissions): - embed.title = random.choice(NEGATIVE_REPLIES) - embed.description = f"Sorry, but you may only use this command within <#{Channels.bot}>." - await ctx.send(embed=embed) - - else: - log.exception(f"Unhandled error: {error}") - def setup(bot): bot.add_cog(Information(bot)) diff --git a/bot/cogs/snekbox.py b/bot/cogs/snekbox.py index 05834e421..c8705ac6f 100644 --- a/bot/cogs/snekbox.py +++ b/bot/cogs/snekbox.py @@ -1,18 +1,14 @@ import datetime import logging -import random import re import textwrap from signal import Signals from typing import Optional, Tuple -from discord import Colour, Embed -from discord.ext.commands import ( - Bot, CommandError, Context, NoPrivateMessage, command, guild_only -) +from discord.ext.commands import Bot, Context, command, guild_only -from bot.constants import Channels, ERROR_REPLIES, NEGATIVE_REPLIES, STAFF_ROLES, URLs -from bot.decorators import InChannelCheckFailure, in_channel +from bot.constants import Channels, STAFF_ROLES, URLs +from bot.decorators import in_channel from bot.utils.messages import wait_for_deletion @@ -224,27 +220,6 @@ class Snekbox: finally: del self.jobs[ctx.author.id] - @eval_command.error - async def eval_command_error(self, ctx: Context, error: CommandError): - embed = Embed(colour=Colour.red()) - - if isinstance(error, NoPrivateMessage): - embed.title = random.choice(NEGATIVE_REPLIES) - embed.description = "You're not allowed to use this command in private messages." - await ctx.send(embed=embed) - - elif isinstance(error, InChannelCheckFailure): - embed.title = random.choice(NEGATIVE_REPLIES) - embed.description = str(error) - await ctx.send(embed=embed) - - else: - original_error = getattr(error, 'original', "no original error") - log.error(f"Unhandled error in snekbox eval: {error} ({original_error})") - embed.title = random.choice(ERROR_REPLIES) - embed.description = "Some unhandled error occurred. Sorry for that!" - await ctx.send(embed=embed) - def setup(bot): bot.add_cog(Snekbox(bot)) diff --git a/bot/cogs/utils.py b/bot/cogs/utils.py index 0c6d9d2ba..98208723a 100644 --- a/bot/cogs/utils.py +++ b/bot/cogs/utils.py @@ -1,5 +1,4 @@ import logging -import random import re import unicodedata from email.parser import HeaderParser @@ -8,8 +7,8 @@ from io import StringIO from discord import Colour, Embed from discord.ext.commands import AutoShardedBot, Context, command -from bot.constants import Channels, NEGATIVE_REPLIES, STAFF_ROLES -from bot.decorators import InChannelCheckFailure, in_channel +from bot.constants import Channels, STAFF_ROLES +from bot.decorators import in_channel log = logging.getLogger(__name__) @@ -133,13 +132,6 @@ class Utils: await ctx.send(embed=embed) - async def __error(self, ctx, error): - embed = Embed(colour=Colour.red()) - if isinstance(error, InChannelCheckFailure): - embed.title = random.choice(NEGATIVE_REPLIES) - embed.description = str(error) - await ctx.send(embed=embed) - def setup(bot): bot.add_cog(Utils(bot)) diff --git a/bot/cogs/verification.py b/bot/cogs/verification.py index 56fcd63eb..6b42c9213 100644 --- a/bot/cogs/verification.py +++ b/bot/cogs/verification.py @@ -5,7 +5,7 @@ from discord.ext.commands import Bot, Context, command from bot.cogs.modlog import ModLog from bot.constants import Channels, Event, Roles -from bot.decorators import in_channel, without_role +from bot.decorators import InChannelCheckFailure, in_channel, without_role log = logging.getLogger(__name__) @@ -152,6 +152,12 @@ class Verification: ) @staticmethod + async def __error(ctx: Context, error): + if isinstance(error, InChannelCheckFailure): + # Do nothing; just ignore this error + error.handled = True + + @staticmethod def __global_check(ctx: Context): """ Block any command within the verification channel that is not !accept. |