Skip to content

Commit 4b7b1f9

Browse files
committed
Better error messages
1 parent ce91cb9 commit 4b7b1f9

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

bot.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -784,11 +784,31 @@ async def on_error(self, event_method, *args, **kwargs):
784784
logger.error(error('Ignoring exception in {}'.format(event_method)))
785785
logger.error(error('Unexpected exception:'), exc_info=sys.exc_info())
786786

787-
async def on_command_error(self, context, exception):
788-
if isinstance(exception, (commands.MissingRequiredArgument,
789-
commands.UserInputError)):
790-
await context.invoke(self.get_command('help'),
791-
command=str(context.command))
787+
async def on_command_error(self, ctx, exception):
788+
789+
def human_join(strings):
790+
if len(strings) <= 2:
791+
return ' or '.join(strings)
792+
else:
793+
return ', '.join(strings[:len(strings)-1]) + ' or ' + strings[-1]
794+
795+
if isinstance(exception, commands.BadUnionArgument):
796+
msg = 'Could not find the specified ' + human_join([c.__name__ for c in exception.converters])
797+
await ctx.trigger_typing()
798+
await ctx.send(embed=discord.Embed(
799+
color=discord.Color.red(),
800+
description=msg
801+
))
802+
803+
elif isinstance(exception, commands.BadArgument):
804+
await ctx.trigger_typing()
805+
await ctx.send(embed=discord.Embed(
806+
color=discord.Color.red(),
807+
description=str(exception)
808+
))
809+
elif isinstance(exception, commands.MissingRequiredArgument):
810+
await ctx.invoke(self.get_command('help'),
811+
command=str(ctx.command))
792812
elif isinstance(exception, commands.CommandNotFound):
793813
logger.warning(error('CommandNotFound: ' + str(exception)))
794814
elif isinstance(exception, commands.CheckFailure):

cogs/modmail.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import re
33
from datetime import datetime
44
from typing import Optional, Union
5+
from types import SimpleNamespace as p
56

67
import discord
78
from discord.ext import commands
@@ -420,7 +421,7 @@ async def logs(self, ctx, *, member: User = None):
420421
if not member:
421422
thread = ctx.thread
422423
if not thread:
423-
raise commands.UserInputError
424+
raise commands.MissingRequiredArgument(p(name=member))
424425
user = thread.recipient
425426
else:
426427
user = member
@@ -690,14 +691,14 @@ async def block(self, ctx, user: Optional[User] = None, *,
690691
if thread:
691692
user = thread.recipient
692693
else:
693-
raise commands.UserInputError
694+
raise commands.MissingRequiredArgument(p(name='user'))
694695

695696
if after is not None:
696697
reason = after.arg
697698
if reason.startswith('System Message: '):
698-
raise commands.UserInputError
699+
raise commands.BadArgument('The reason cannot start with `System Message:`.')
699700
if re.search(r'%(.+?)%$', reason) is not None:
700-
raise commands.UserInputError
701+
raise commands.MissingRequiredArgument(p(name='reason'))
701702
if after.dt > after.now:
702703
reason = f'{reason} %{after.dt.isoformat()}%'
703704

@@ -754,7 +755,7 @@ async def unblock(self, ctx, *, user: User = None):
754755
if thread:
755756
user = thread.recipient
756757
else:
757-
raise commands.UserInputError
758+
raise commands.MissingRequiredArgument(p(name='user'))
758759

759760
mention = user.mention if hasattr(user, 'mention') else f'`{user.id}`'
760761

cogs/utility.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ async def format_not_found(self, ctx, command):
130130

131131
choices = set()
132132

133-
for name, c in self.bot.all_commands:
133+
for name, c in self.bot.all_commands.items():
134134
if not c.hidden:
135135
choices.add(name)
136136

@@ -909,7 +909,7 @@ async def add_perms_command(self, ctx, command: str, *, user_or_role: Union[User
909909
elif user_or_role in {'everyone', 'all'}:
910910
value = -1
911911
else:
912-
raise commands.UserInputError('Invalid user or role.')
912+
raise commands.BadArgument('Could not find the specified User or Role.')
913913

914914
await self.bot.update_perms(self.bot.all_commands[command].name, value)
915915
embed = Embed(
@@ -937,7 +937,7 @@ async def add_perms_level(self, ctx, level: str, *, user_or_role: Union[User, Ro
937937
elif user_or_role in {'everyone', 'all'}:
938938
value = -1
939939
else:
940-
raise commands.UserInputError('Invalid user or role.')
940+
raise commands.BadArgument('Could not find the specified User or Role.')
941941

942942
await self.bot.update_perms(PermissionLevel[level.upper()], value)
943943
embed = Embed(
@@ -973,7 +973,7 @@ async def remove_perms_command(self, ctx, command: str, *, user_or_role: Union[U
973973
elif user_or_role in {'everyone', 'all'}:
974974
value = -1
975975
else:
976-
raise commands.UserInputError('Invalid user or role.')
976+
raise commands.BadArgument('Could not find the specified User or Role.')
977977

978978
await self.bot.update_perms(self.bot.all_commands[command].name, value, add=False)
979979
embed = Embed(
@@ -1001,7 +1001,7 @@ async def remove_perms_level(self, ctx, level: str, *, user_or_role: Union[User,
10011001
elif user_or_role in {'everyone', 'all'}:
10021002
value = -1
10031003
else:
1004-
raise commands.UserInputError('Invalid user or role.')
1004+
raise commands.BadArgument('Could not find the specified User or Role.')
10051005

10061006
await self.bot.update_perms(PermissionLevel[level.upper()], value, add=False)
10071007
embed = Embed(
@@ -1021,7 +1021,7 @@ async def get_perms(self, ctx, *, user_or_role: Union[User, Role, str]):
10211021
elif user_or_role in {'everyone', 'all'}:
10221022
value = -1
10231023
else:
1024-
raise commands.UserInputError('Invalid user or role.')
1024+
raise commands.BadArgument('Could not find the specified User or Role.')
10251025

10261026
cmds = []
10271027
levels = []

0 commit comments

Comments
 (0)