From cc1324c5bb61afd882ca6e1925534e6e37858f7b Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 22 Jul 2018 19:26:21 -0500 Subject: [PATCH] Fixed error handling for tunnels and dims --- BotErrors.py | 6 ++++++ DatabaseModels.py | 6 +++--- Geoffrey.py | 19 +++++++++++-------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/BotErrors.py b/BotErrors.py index 0cdb111..23c31c7 100644 --- a/BotErrors.py +++ b/BotErrors.py @@ -33,5 +33,11 @@ class DatabaseValueError(DataBaseError): class ItemNotFound(DataBaseError): '''No item matches found in database''' +class InvalidDimError(DataBaseError): + """Invalid dimension name""" + +class InvalidTunnelError(DataBaseError): + """Invalid tunnel name""" + diff --git a/DatabaseModels.py b/DatabaseModels.py index 6996625..972a571 100644 --- a/DatabaseModels.py +++ b/DatabaseModels.py @@ -49,7 +49,6 @@ class GeoffreyDatabase: except: session.rollback() - def query_by_filter(self, session, obj_type, * args): filter_value = self.combine_filter(args) return session.query(obj_type).filter(filter_value).all() @@ -79,6 +78,7 @@ class GeoffreyDatabase: def combine_filter(self, filter_value): return expression.and_(filter_value[0]) + class TunnelDirection(enum.Enum): North = 'green' East = 'blue' @@ -97,7 +97,7 @@ class TunnelDirection(enum.Enum): elif check_similarity(TunnelDirection.West.value, arg): return TunnelDirection.West else: - raise ValueError + raise InvalidTunnelError class Dimension(enum.Enum): @@ -114,7 +114,7 @@ class Dimension(enum.Enum): elif check_similarity(Dimension.end.value, arg): return Dimension.end else: - raise ValueError + raise InvalidDimError class Player(SQL_Base): diff --git a/Geoffrey.py b/Geoffrey.py index caa9348..c5b8a47 100644 --- a/Geoffrey.py +++ b/Geoffrey.py @@ -133,7 +133,7 @@ async def tunnel(ctx, tunnel_color: str, tunnel_number: int, *args): ''' Adds your tunnel to the database. The location name is optional. If the location has a tunnel, it is updated. - ?addtunnel [Tunnel Color] [Tunnel_Number] [Location Name] + ?tunnel [Tunnel Color] [Tunnel Number] [Location Name] ''' try: if len(args) > 0: @@ -150,22 +150,25 @@ async def tunnel(ctx, tunnel_color: str, tunnel_number: int, *args): except LocationLookUpError: await bot.say('{}, you do not have a location called **{}**.'.format( ctx.message.author.mention, args[0])) - except ValueError: - raise commands.UserInputError + except TunnelInitError: + await bot.say('{}, invalid tunnel color.'.format(ctx.message.author.mention)) + except InvalidTunnelError: + await bot.say('{}, {} is an invalid tunnel color.'.format(ctx.message.author.mention, tunnel_color)) @bot.command(pass_context=True) -async def find(ctx, search: str): +async def find(ctx, * args): ''' Finds all the locations and tunnels matching the search term ?find [Search] ''' try: + search = ' '.join(args) result = bot_commands.find(search) await bot.say('{}, The following entries match **{}**:\n{}'.format(ctx.message.author.mention, search, result)) except LocationLookUpError: - await bot.say('{}, no matches **{}** were found in the database'.format(ctx.message.author.mention, search)) + await bot.say('{}, no matches to **{}** were found in the database'.format(ctx.message.author.mention, search)) @bot.command(pass_context=True) async def delete(ctx, * args): @@ -175,7 +178,7 @@ async def delete(ctx, * args): ''' try: name = ' '.join(args) - commands.delete(name, discord_uuid=ctx.message.author.id) + bot_commands.delete(name, discord_uuid=ctx.message.author.id) await bot.say('{}, your location named **{}** has been deleted.'.format(ctx.message.author.mention, name)) except (DeleteEntryError, PlayerNotFound): await bot.say('{}, you do not have a location named **{}**.'.format(ctx.message.author.mention, name)) @@ -216,8 +219,8 @@ async def findaround(ctx, x_pos: int, z_pos: int, * args): else: await bot.say('{}, there are no locations within {} blocks of that point' .format(ctx.message.author.mention, radius)) - except ValueError: - raise commands.UserInputError + except InvalidDimError: + await bot.say('{}, {} is an invalid dimension.'.format(ctx.message.author.mention, dimension)) @bot.command(pass_context=True)