From 4b51951b656eee6b707a3cc75a329a53f5b03a6e Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sun, 5 Aug 2018 09:08:20 -0500 Subject: [PATCH] Fixed several small bugs in the discord commands --- Commands.py | 38 +++++++++++++++++++++++++++++++++++--- Geoffrey.py | 24 +++++++++++++++++++----- test_commands.py | 28 +++++++++++++++++++++++----- 3 files changed, 77 insertions(+), 13 deletions(-) diff --git a/Commands.py b/Commands.py index 3659d46..e0565cf 100644 --- a/Commands.py +++ b/Commands.py @@ -192,7 +192,17 @@ class Commands: try: player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid) - location = self.interface.find_location_by_name_and_owner(session, player, loc_name)[0] + if loc_name is None: + loc_list = self.interface.find_location_by_owner(session, player) + + if len(loc_list) == 0: + raise LocationLookUpError + elif len(loc_list) > 1: + raise EntryNameNotUniqueError + else: + location = loc_list[0] + else: + location = self.interface.find_location_by_name_and_owner(session, player, loc_name)[0] location.x = x location.z = z @@ -247,7 +257,17 @@ class Commands: try: player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid) - location = self.interface.find_location_by_name_and_owner(session, player, loc_name)[0] + if loc_name is None: + loc_list = self.interface.find_location_by_owner(session, player) + + if len(loc_list) == 0: + raise LocationLookUpError + elif len(loc_list) > 1: + raise EntryNameNotUniqueError + else: + location = loc_list[0] + else: + location = self.interface.find_location_by_name_and_owner(session, player, loc_name)[0] location.name = new_name loc_str = location.__str__() @@ -271,7 +291,19 @@ class Commands: try: player = self.get_player(session, discord_uuid=discord_uuid, mc_uuid=mc_uuid) - shop = self.interface.find_shop_by_name_and_owner(session, player, shop_name)[0] + + if shop_name is None: + shop_list = self.interface.find_shop_by_owner(session, player) + + if len(shop_list) == 0: + raise LocationLookUpError + elif len(shop_list) > 1: + raise EntryNameNotUniqueError + else: + shop = shop_list[0] + + else: + shop = self.interface.find_shop_by_name_and_owner(session, player, shop_name)[0] expr = (ItemListing.name == item) & (ItemListing.shop == shop) self.interface.database.delete_entry(session, ItemListing, expr) diff --git a/Geoffrey.py b/Geoffrey.py index 819b563..1f10eab 100644 --- a/Geoffrey.py +++ b/Geoffrey.py @@ -138,7 +138,7 @@ async def add_shop(ctx, x_pos: int, z_pos: int, *args): @bot.command(pass_context=True) async def add_tunnel(ctx, tunnel_color: str, tunnel_number: int, *args): ''' - Adds your tunnel to the database. + Adds your tunnel to the database. If you only have one location, you do not need to specify a location name. ?tunnel [Tunnel Color] [Tunnel Number] [Location Name] ''' @@ -192,7 +192,7 @@ async def tunnel(ctx, player: str): await bot.say('{}, **{}** owns the following tunnels: \n{}'.format(ctx.message.author.mention, player, result)) except LocationLookUpError: - await bot.say('{}, no tunnels for the player **{}** were found in the database/' + await bot.say('{}, no tunnels for **{}** were found in the database.' .format(ctx.message.author.mention, player)) @@ -235,6 +235,8 @@ async def find_around(ctx, x_pos: int, z_pos: int, * args): if len(args) > 0: if args[0] == '-d': dimension = args[1] + if len(args) > 1: + radius = int(args[2]) else: radius = int(args[0]) @@ -250,6 +252,9 @@ async def find_around(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: + await bot.say('{}, invalid radius, the radius must be a whole number.'.format(ctx.message.author.mention, + radius)) except InvalidDimError: await bot.say('{}, {} is an invalid dimension.'.format(ctx.message.author.mention, dimension)) @@ -313,7 +318,7 @@ async def info(ctx, * args): info_str = bot_commands.info(loc) await bot.say(info_str) except IndexError: - await bot.say('{}, no locations in the database match {}.'.format(ctx.message.author.mention, loc)) + await bot.say('{}, no locations in the database match **{}**.'.format(ctx.message.author.mention, loc)) return @@ -389,9 +394,18 @@ async def delete_item(ctx, item: str, * args): await bot.say('{}, **{}** has been removed from the inventory of **{}**.'. format(ctx.message.author.mention, item, shop)) except LocationLookUpError: - await bot.say('{}, you do not have a shop called **{}**.'.format(ctx.message.author.mention, shop)) + if shop is None: + await bot.say('{}, you do have any shops in the database.'.format(ctx.message.author.mention)) + else: + await bot.say('{}, you do not have a shop called **{}**.'.format(ctx.message.author.mention, shop)) + except EntryNameNotUniqueError: + await bot.say('{}, you have more than one shop in the database, please specify a shop name.' + .format(ctx.message.author.mention)) except DeleteEntryError: - await bot.say('{}, **{}** does not sell **{}**.'.format(ctx.message.author.mention, shop, item)) + if shop is not None: + await bot.say('{}, **{}** does not sell **{}**.'.format(ctx.message.author.mention, shop, item)) + else: + await bot.say('{}, your shop does not sell **{}**.'.format(ctx.message.author.mention, item)) # Helper Functions ************************************************************ diff --git a/test_commands.py b/test_commands.py index be2df2e..2b853ac 100644 --- a/test_commands.py +++ b/test_commands.py @@ -53,10 +53,6 @@ class TestCommands(TestCase): self.commands.register('ZeroHD', '143072699567177728') self.commands.add_shop(0, 0, shop_name='test shop', discord_uuid='143072699567177728') - tunnel1 = self.commands.add_tunnel('green', 50, None, discord_uuid='143072699567177728') - - self.assertGreater(len(tunnel1), 0) - tunnel2 = self.commands.add_tunnel('Green', 50, location_name='test_shop', discord_uuid='143072699567177728') if 'Green' not in tunnel2: @@ -181,6 +177,18 @@ class TestCommands(TestCase): else: self.fail() + self.commands.edit_pos(500, 500, None, discord_uuid='143072699567177728') + + if '500' in result: + pass + else: + self.fail() + + self.commands.delete(name='test shop', discord_uuid='143072699567177728') + + self.assertRaises(LocationLookUpError, self.commands.edit_pos, 5, 5, None, + discord_uuid='143072699567177728') + def test_edit_tunnel(self): self.commands.register('ZeroHD', '143072699567177728') self.commands.add_shop(0, 0, shop_name='test shop', discord_uuid='143072699567177728') @@ -201,11 +209,21 @@ class TestCommands(TestCase): self.commands.add_item('dirt', 5, 5, shop_name='test shop', discord_uuid='143072699567177728') self.commands.add_item('wood', 5, 5, shop_name='test shop', discord_uuid='143072699567177728') - result = self.commands.delete_item('dirt', 'test_shop', discord_uuid='143072699567177728') + result = self.commands.delete_item('dirt', None, discord_uuid='143072699567177728') if ('dirt' not in result) & ('wood' in result): pass else: self.fail() + self.commands.add_shop(0, 0, shop_name='test shop2', discord_uuid='143072699567177728') + self.assertRaises(EntryNameNotUniqueError, self.commands.delete_item, 'wood', None, + discord_uuid='143072699567177728') + + self.commands.delete('test shop', discord_uuid='143072699567177728') + self.commands.delete('test shop2', discord_uuid='143072699567177728') + + self.assertRaises(LocationLookUpError, self.commands.delete_item, 'wood', None, + discord_uuid='143072699567177728') +