From e63d00e967ff35ee1a9254225857bb6b7bd9085c Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sat, 16 Feb 2019 21:21:29 -0600 Subject: [PATCH] Added town commands and fixed many bug in commands +Added add_town, add_resident, remove_resident +Fixed some bugs introduce by the last commit +Error message now sends as one message --- GeoffreyBot/GeoffreyApiHelper.py | 40 +++++- GeoffreyBot/geoffrey.py | 26 +++- GeoffreyBot/geoffrey_api.py | 205 ++++++++++++++++++++++++++----- 3 files changed, 231 insertions(+), 40 deletions(-) diff --git a/GeoffreyBot/GeoffreyApiHelper.py b/GeoffreyBot/GeoffreyApiHelper.py index b17fcfd..ddfcb23 100644 --- a/GeoffreyBot/GeoffreyApiHelper.py +++ b/GeoffreyBot/GeoffreyApiHelper.py @@ -20,19 +20,43 @@ def formatted_position(location): return '(x={}, z={})'.format(location["x_coord"], location["z_coord"]) -def formatted_shop(shop): - inventory = "" - for item in shop["items"]: - inventory = inventory + "\n" + (formatted_item_listing(item)) +def formatted_shop(shop, base_url): + shop_info_list = formatted_location_info(shop, base_url) - return formatted_location(shop) + inventory + if len(shop["items"]) > 0: + shop_info_list.append("**Inventory:**") + for item in shop["items"]: + shop_info_list.append(formatted_item_listing(item)) + + return shop_info_list + + +def formatted_location_info(location, base_url): + info = [] + + info.append(formatted_location(location)) + info.append(get_full_link(base_url, location["link"])) + + return info + + +def formatted_town(town, base_url): + town_info_list = formatted_location_info(town, base_url) + + if len(town["residents"]) > 0: + town_info_list.append("**Residents:**") + + for resident in town["residents"]: + town_info_list.append(resident["name"]) + + return town_info_list def formatted_owners(location): owner_list = '' if "owner" in location: - if len(location["owner"]) == 0: + if len(location["owner"]) == 1: owner_list = 'Owner: **{}**' owner_list = owner_list.format(location["owner"][0]["name"]) else: @@ -45,3 +69,7 @@ def formatted_owners(location): owner_list = owner_list.format(owners) return owner_list + + +def get_full_link(base_url, link): + return base_url + link diff --git a/GeoffreyBot/geoffrey.py b/GeoffreyBot/geoffrey.py index 49c83e2..e892830 100644 --- a/GeoffreyBot/geoffrey.py +++ b/GeoffreyBot/geoffrey.py @@ -75,10 +75,14 @@ class GeoffreyBot(commands.Bot): if isinstance(error, commands.errors.CommandNotFound): return elif isinstance(error, commands.errors.BadArgument): - error_str = "That's not how you use the command you ding dong." + error_str = "Well bud, you got this far. Good job! But you still h*cked up the syntax. " \ + "Check {}help.".format(self.prefix) elif isinstance(error, commands.errors.MissingRequiredArgument): error_str = "Well bud, you got this far. Good job! But you still h*cked up the syntax. " \ "Check {}help.".format(self.prefix) + elif hasattr(error, "original") and isinstance(error.original, ValueError): + error_str = "Well bud, you got this far. Good job! But you still h*cked up the syntax. Check {}help." \ + .format(self.prefix) elif hasattr(error, "original") and isinstance(error.original, request_exception.ConnectionError): error_str = "Unable to connect to the database. Hopefully someone is working on this..." await self.send_error_message("Can't connect to GeoffreyAPI, is it offline?") @@ -90,7 +94,7 @@ class GeoffreyBot(commands.Bot): elif e == "NoLocationsInDatabase": error_str = "You have no locations in the database, ding dongs like you can read {}help to figure out" \ "how to add them".format(self.prefix) - elif e == "TypeError": + elif e == "TypeError" or e == "ValueError": error_str = "Well bud, you got this far. Good job! But you still h*cked up the syntax. Check {}help." \ .format(self.prefix) elif e == "DataError": @@ -104,8 +108,11 @@ class GeoffreyBot(commands.Bot): ctx.invoked_with, ctx.args[1].args[2:])) + error_message = ["```python"] for tb in traceback.format_tb(error.original.__traceback__): - await self.send_error_message("```python\n{}```".format(tb)) + error_message.append(tb) + + error_message.append("```") error_str = bad_error_message.format(ctx.invoked_with) @@ -118,7 +125,11 @@ class GeoffreyBot(commands.Bot): async def send_error_message(self, msg): for user_id in self.error_users: user = await self.get_user_info(user_id) - await user.send(msg) + + if msg is list: + await self.send_list(user, msg) + else: + await user.send(msg) def run_command(self, command, **kwargs): URL = self.base_url + '/api/command/{}' @@ -171,8 +182,11 @@ class GeoffreyBot(commands.Bot): help_list.append(command["help"] + "\n") - help_msg = self.get_command(command_name).help - help_list.append(help_msg.format(self.prefix)) + c = self.get_command(command_name) + + if c is not None: + help_msg = c.help + help_list.append(help_msg.format(self.prefix)) help_list.append('```') diff --git a/GeoffreyBot/geoffrey_api.py b/GeoffreyBot/geoffrey_api.py index db7781c..6de2beb 100644 --- a/GeoffreyBot/geoffrey_api.py +++ b/GeoffreyBot/geoffrey_api.py @@ -55,12 +55,16 @@ class GeoffreyCommands: discord_uuid=ctx.message.author.id) await ctx.send( - '{}, your base has been added to the database: \n\n{}'.format(ctx.message.author.mention, + '{}, your base has been added to the database: \n{}'.format(ctx.message.author.mention, formatted_location(base))) except Exception as e: error_list = [ {"error": "EntryNameNotUniqueError", - "message": "{}, a base with that name already exists, be more unique ding dong".format( + "message": "{}, a location with that name already exists, be more unique ding dong".format( + ctx.message.author.mention) + }, + {"error": "LocationLookUpError", + "message": "{}, you have more than one location. Please specify a name.".format( ctx.message.author.mention) }, ] @@ -82,7 +86,7 @@ class GeoffreyCommands: item = run_command(self.base_url, self.api_token, "POST", "add_item", item_name=item_name, quantity=quantity, diamond_price=diamond_price, shop_name=shop_name, discord_uuid=ctx.message.author.id) - await ctx.send('{}, {} has been added to the inventory of {}'.format(ctx.message.author.mention, + await ctx.send('{}, **{}** has been added to the inventory of **{}**'.format(ctx.message.author.mention, item["item_name"], item["shop"]["name"])) except Exception as e: @@ -99,12 +103,14 @@ class GeoffreyCommands: await ctx.send(msg) @commands.command(pass_context=True) - async def add_owner(self, ctx, new_owner_name, location_name): + async def add_owner(self, ctx, new_owner_name, *args): ''' {}add_owner WARNING: The new owner had just as much power as you to edit or delete this location. ''' + location_name = get_name(args) + try: location = run_command(self.base_url, self.api_token, "POST", "add_owner", new_owner_name=new_owner_name, location_name=location_name, discord_uuid=ctx.message.author.id) @@ -113,17 +119,52 @@ class GeoffreyCommands: ctx.message.author.mention, new_owner_name, location["name"])) except Exception as e: error_list = [ - {"error": "PlayerNotFound", - "message": "{}, ain't no one in this darn database named {} you goob".format( - ctx.message.author.mention, new_owner_name) + {"error": "OwnerNotFoundError", + "message": "{}, ain't no one in this darn database named **{}** you goob".format( + ctx.message.author.mention, new_owner_name) }, - {"error": "PlayerInDBError", + {"error": "IsOwnerError", "message": "{}, **{}** is already an owner, stop having amosia.".format( ctx.message.author.mention, new_owner_name) }, {"error": "LocationLookUpError", - "message": "{}, you do not have a location by the name you ding dong goober.".format( - ctx.message.author) + "message": "{}, you do not have a location by that name you ding dong goober.".format( + ctx.message.author.mention) + } + + ] + + msg = check_error(e, error_list) + + await ctx.send(msg) + + @commands.command(pass_context=True) + async def add_resident(self, ctx, new_resident_name, *args): + ''' + {}add_resident + ''' + + town_name = get_name(args) + try: + location = run_command(self.base_url, self.api_token, "POST", "add_resident", + new_resident_name=new_resident_name, + town_name=town_name, discord_uuid=ctx.message.author.id) + + await ctx.send('{}, **{}** has been added as a resident to **{}**'.format( + ctx.message.author.mention, new_resident_name, location["name"])) + except Exception as e: + error_list = [ + {"error": "ResidentNotFoundError", + "message": "{}, ain't no one in this darn database named {} you goob".format( + ctx.message.author.mention, new_resident_name) + }, + {"error": "IsResidentError", + "message": "{}, **{}** is already a resident, stop having amosia.".format( + ctx.message.author.mention, new_resident_name) + }, + {"error": "LocationLookUpError", + "message": "{}, you do not have a town by that name you ding dong goober.".format( + ctx.message.author.mention) } ] @@ -142,18 +183,53 @@ class GeoffreyCommands: name = get_name(args) try: - base = run_command(self.base_url, self.api_token, "POST", "add_shop", x_pos=x_pos, z_pos=z_pos, name=name, + shop = run_command(self.base_url, self.api_token, "POST", "add_shop", x_pos=x_pos, z_pos=z_pos, name=name, discord_uuid=ctx.message.author.id) await ctx.send( - '{}, your shop has been added to the database: \n\n{}'.format(ctx.message.author.mention, - formatted_location(base))) + '{}, your shop has been added to the database: \n{}'.format(ctx.message.author.mention, + formatted_location(shop))) except Exception as e: error_list = [ {"error": "EntryNameNotUniqueError", - "message": "{}, a shop with that name already exists, be more unique ding dong".format( + "message": "{}, a location with that name already exists, be more unique ding dong".format( ctx.message.author.mention) - } + }, + {"error": "LocationLookUpError", + "message": "{}, you have more than one location. Please specify a name.".format( + ctx.message.author.mention) + }, + ] + + msg = check_error(e, error_list) + await ctx.send(msg) + + @commands.command(pass_context=True) + async def add_town(self, ctx, x_pos: int, z_pos: int, *args): + ''' + {}add_town + The Town Name parameter is optional if this is your first town + ''' + + name = get_name(args) + + try: + town = run_command(self.base_url, self.api_token, "POST", "add_town", x_pos=x_pos, z_pos=z_pos, name=name, + discord_uuid=ctx.message.author.id) + + await ctx.send( + '{}, your town has been added to the database: \n{}'.format(ctx.message.author.mention, + formatted_location(town))) + except Exception as e: + error_list = [ + {"error": "EntryNameNotUniqueError", + "message": "{}, a location with that name already exists, be more unique ding dong".format( + ctx.message.author.mention) + }, + {"error": "LocationLookUpError", + "message": "{}, you have more than one location. Please specify a name.".format( + ctx.message.author.mention) + }, ] msg = check_error(e, error_list) @@ -182,8 +258,10 @@ class GeoffreyCommands: {"error": "InvalidTunnelError", "message": "{}, {} is not a valid tunnel direction ya gub".format(ctx.message.author.mention, tunnel_direction) - } - + }, + {"error": "LocationLookUpError", + "message": "{}, you do not have a location by the name you ding dong goober.".format( + ctx.message.author.mention)} ] msg = check_error(e, error_list) @@ -206,7 +284,7 @@ class GeoffreyCommands: error_list = [ {"error": "LocationLookUpError", "message": "{}, you do not have a location by the name you ding dong goober.".format( - ctx.message.author)} + ctx.message.author.mention)} ] msg = check_error(e, error_list) @@ -230,7 +308,8 @@ class GeoffreyCommands: except Exception as e: error_list = [ {"error": "LocationLookUpError", - "message": "{}, you do not have a location by the name you ding dong goober."}, + "message": "{}, you do not have a shop by that name you ding dong goober.".format( + ctx.message.author.mention)}, {"error": "EntryNameNotUniqueError", "message": "{}, you have more than one location. Please specify a name, dingus.".format( ctx.message.author.mention)}, @@ -347,12 +426,7 @@ class GeoffreyCommands: error_list = [ {"error": "LocationLookUpError", "message": "{}, there are no locations in that area.".format(ctx.message.author.mention) - }, - { - "error": "ValueError", - "message": "{}, {} is an invalid radius, the radius must be a whole number.".format( - ctx.message.author.mention, args[0]) - } + } ] msg = check_error(e, error_list) @@ -410,9 +484,17 @@ class GeoffreyCommands: try: location = run_command(self.base_url, self.api_token, "GET", "info", location_name=location_name) - message = "{}, info on {}:\n{}".format(ctx.message.author.mention, location["name"], - formatted_location(location)) + message = "{}, info on {}:\n".format(ctx.message.author.mention, location["name"]) + + if location["type"] == "Shop": + info_list = formatted_shop(location, self.base_url) + elif location["type"] == "Town": + info_list = formatted_town(location, self.base_url) + else: + info_list = formatted_location_info(location, self.base_url) + await ctx.send(message) + await self.bot.send_list(ctx, info_list) except Exception as e: error_list = [ @@ -461,6 +543,38 @@ class GeoffreyCommands: msg = check_error(e, error_list) await ctx.send(msg) + @commands.command(pass_context=True) + async def remove_resident(self, ctx, resident_name, *args): + ''' + {}remove_resident + The Town Name is optional if you only have one town. + ''' + + town_name = get_name(args) + try: + location = run_command(self.base_url, self.api_token, "POST", "remove_resident", + resident_name=resident_name, + town_name=town_name, discord_uuid=ctx.message.author.id) + + await ctx.send('{}, **{}** has been remove as a resident of **{}**'.format( + ctx.message.author.mention, resident_name, location["name"])) + except Exception as e: + error_list = [ + {"error": "ResidentNotFoundError", + "message": "{}, ain't no one your town named {} you goob".format( + ctx.message.author.mention, resident_name) + }, + {"error": "LocationLookUpError", + "message": "{}, you do not have a town by that name you ding dong goober.".format( + ctx.message.author) + } + + ] + + msg = check_error(e, error_list) + + await ctx.send(msg) + @commands.command(pass_context=True) async def selling(self, ctx, *args): ''' @@ -475,7 +589,41 @@ class GeoffreyCommands: message = ["{} The following shop(s) sell **{}**:".format(ctx.message.author.mention, item)] for shop in results: - message.append(formatted_shop(shop)) + for line in formatted_shop(shop, base_url=self.base_url): + message.append(line) + + message.append('') + + await self.bot.send_list(ctx, message) + except Exception as e: + error_list = [ + {"error": "ItemNotFound", + "message": "{}, no shop was found selling {}".format( + ctx.message.author.mention, item) + } + ] + + msg = check_error(e, error_list) + await ctx.send(msg) + + @commands.command(pass_context=True) + async def selling_price(self, ctx, *args): + ''' + {}selling + Sorts by most recently added + ''' + item = get_name(args) + + try: + results = run_command(self.base_url, self.api_token, "GET", "selling_price", item_name=item) + + message = ["{} The following shop(s) sell **{}**:".format(ctx.message.author.mention, item)] + + for shop in results: + for line in formatted_shop(shop, base_url=self.base_url): + message.append(line) + + message.append('') await self.bot.send_list(ctx, message) except Exception as e: @@ -519,6 +667,7 @@ class GeoffreyCommands: msg = check_error(e, error_list) await ctx.send(msg) + @commands.command(pass_context=True) async def tunnel(self, ctx, *args): '''