diff --git a/GeoffreyBot/geoffrey.py b/GeoffreyBot/geoffrey.py index e534d08..fd43463 100644 --- a/GeoffreyBot/geoffrey.py +++ b/GeoffreyBot/geoffrey.py @@ -7,6 +7,7 @@ import logging import time import traceback import sys +from GeoffreyBot.geoffrey_api import HandledError logger = logging.getLogger('GeoffreyBot') @@ -71,41 +72,29 @@ class GeoffreyBot(commands.Bot): async def on_command_error(self, ctx, error): error_str = "" - if isinstance(error, commands.errors.CommandNotFound): + + if hasattr(error, "original") and isinstance(error.original, HandledError): return - elif isinstance(error, commands.errors.BadArgument): - 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 isinstance(error, commands.errors.CommandNotFound): + return + elif isinstance(error, commands.errors.BadArgument) or isinstance(error, + commands.errors.MissingRequiredArgument) or ( + 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:" + + for line in self.help_dict[ctx.command.name]: + error_str += line + 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?") - elif len(error.original.args) > 0 and hasattr(error, "original"): - e = error.original.args[0] - - if e == "PlayerNotFound": - error_str = "You are not in the database, register first ding dong!" - 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" 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": - error_str = "Slow down their slugger, that's a long word or number you are trying to cram into me, " \ - "try again with something smaller, please" + await self.send_error_message("Can't connect to the GeoffreyAPI, is it offline?") if error_str is '': await self.send_error_message( 'Geoffrey encountered unhandled exception: {} Command: **{}** Context: {}' - .format(error, - ctx.invoked_with, - ctx.args[1].args[2:])) + .format(error, + ctx.invoked_with, + ctx.args[1].args[2:])) error_message = ["```python"] for tb in traceback.format_tb(error.original.__traceback__): diff --git a/GeoffreyBot/geoffrey_api.py b/GeoffreyBot/geoffrey_api.py index e403ff0..a8d77a9 100644 --- a/GeoffreyBot/geoffrey_api.py +++ b/GeoffreyBot/geoffrey_api.py @@ -4,8 +4,26 @@ from GeoffreyBot.GeoffreyApiHelper import * import requests +default_error_messages = { + "PlayerNotFound": "You are not in the database, do ?register first!", + "NoLocationsInDatabase": "You have no locations in the database, you need to add some first!", + "DataError": "Slow down their slugger, that's a long word or number you are trying to cram into me, try again with " + "something smaller, please", + "LocationHasTunnelError": "that location already has a tunnel you goober." +} -def run_command(base_url, api_token, request_type, command, **kwargs): + +class HandledError(Exception): + pass + + +class CommandError: + def __init__(self, error, message): + self.error = error + self.message = message + + +async def run_command(ctx, base_url, api_token, request_type, command, errors=None, **kwargs): URL = base_url + '/api/command/{}/' kwargs["api"] = api_token @@ -18,23 +36,23 @@ def run_command(base_url, api_token, request_type, command, **kwargs): raise TypeError json = response.json() + if "error" in json: - raise Exception(json['error'], json["error_message"]) + error_name = json["error"] + if error_name in errors: + msg = errors[error_name] + elif error_name in default_error_messages: + msg = default_error_messages[error_name] + else: + raise Exception(json['error'], json["error_message"]) + + await ctx.send("{}, {}".format(ctx.message.author.mention, msg)) + + raise HandledError else: return json -def check_error(exception, handled_errors): - if len(exception.args) == 0: - raise exception - else: - for error in handled_errors: - if exception.args[0] == error["error"]: - return error["message"] - - raise exception - - class GeoffreyCommands(commands.Cog): def __init__(self, bot): self.bot = bot @@ -48,29 +66,20 @@ class GeoffreyCommands(commands.Cog): The Name parameter is optional if this is your first base """ + + errors = { + "LocationLookUpError": "you have more than one location. Please specify a name." + } + name = get_name(args) - try: - base = run_command(self.base_url, self.api_token, "POST", "add_base", x_pos=x_pos, z_pos=z_pos, name=name, - discord_uuid=ctx.message.author.id) + base = await run_command(ctx, self.base_url, self.api_token, "POST", "add_base", x_pos=x_pos, z_pos=z_pos, + name=name, + discord_uuid=ctx.message.author.id, errors=errors) - await ctx.send( - '{}, 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 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) + await ctx.send( + '{}, your base has been added to the database: \n{}'.format(ctx.message.author.mention, + formatted_location(base))) @commands.command(pass_context=True) async def add_item(self, ctx, item_name, quantity: int, diamond_price: int, *args): @@ -80,27 +89,25 @@ class GeoffreyCommands(commands.Cog): The Shop Name parameter is optional if this is your first shop """ + errors = { + "LocationLookUpError": "You do not have a shop by that name, goober.", + "EntryNameNotUniqueError": "You have more than one location. Please specify a name, dingus." + + } + shop_name = get_name(args) - try: - 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, - item["item_name"], - item["shop"]["name"])) - except Exception as e: - error_list = [ - {"error": "LocationLookUpError", - "message": "{}, you do not have a shop named {}.".format(ctx.message.author.mention, shop_name) - }, - {"error": "EntryNameNotUniqueError", - "message": "{}, you have more than one location. Please specify a name, dingus.".format( - ctx.message.author.mention)} - ] + item = await run_command(ctx, 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, + errors=errors) - msg = check_error(e, error_list) - await ctx.send(msg) + await ctx.send('{}, **{}** has been added to the inventory of **{}**'.format(ctx.message.author.mention, + item["item_name"], + item["shop"]["name"])) @commands.command(pass_context=True) async def add_owner(self, ctx, new_owner_name, *args): @@ -108,70 +115,42 @@ class GeoffreyCommands(commands.Cog): {}add_owner WARNING: The new owner had just as much power as you to edit or delete this location. """ + errors = { + "OwnerNotFoundError": "ain't no one in this darn database named **{}** you goob".format( + ctx.message.author.mention, new_owner_name), + "IsOwnerError": "**{}** is already an owner, stop having amosia.".format( + ctx.message.author.mention, new_owner_name), + "LocationLookUpError": "you do not have a location by that name you ding dong goober." + } 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) + location = await run_command(ctx, self.base_url, self.api_token, "POST", "add_owner", errors=errors, + new_owner_name=new_owner_name, + location_name=location_name, discord_uuid=ctx.message.author.id) - await ctx.send('{}, **{}** has been added as an owner to **{}**'.format( - ctx.message.author.mention, new_owner_name, location["name"])) - except Exception as e: - error_list = [ - {"error": "OwnerNotFoundError", - "message": "{}, ain't no one in this darn database named **{}** you goob".format( - ctx.message.author.mention, new_owner_name) - }, - {"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 that name you ding dong goober.".format( - ctx.message.author.mention) - } - - ] - - msg = check_error(e, error_list) - - await ctx.send(msg) + await ctx.send('{}, **{}** has been added as an owner to **{}**'.format( + ctx.message.author.mention, new_owner_name, location["name"])) @commands.command(pass_context=True) async def add_resident(self, ctx, new_resident_name, *args): """ {}add_resident """ + errors = { + "ResidentNotFoundError": "ain't no one in this darn database named **{}** you goob".format( + new_resident_name), + "IsResidentError": "**{}** is already a resident, stop having amosia.".format(new_resident_name), + "LocationLookupError": "you do not have a town by that name you ding dong goober." + } 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) + location = await run_command(ctx, self.base_url, self.api_token, "POST", "add_resident", errors=errors, + 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) - } - - ] - - msg = check_error(e, error_list) - - await ctx.send(msg) + await ctx.send('{}, **{}** has been added as a resident to **{}**'.format( + ctx.message.author.mention, new_resident_name, location["name"])) @commands.command(pass_context=True) async def add_shop(self, ctx, x_pos: int, z_pos: int, *args): @@ -180,29 +159,17 @@ class GeoffreyCommands(commands.Cog): The Shop Name parameter is optional if this is your first shop """ + errors = { + "LocationLookUpError": "you have more than one location. Please specify a name." + } + name = get_name(args) - try: - 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) + shop = await run_command(ctx, self.base_url, self.api_token, "POST", "add_shop", errors=errors, 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{}'.format(ctx.message.author.mention, - formatted_location(shop))) - 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) - await ctx.send(msg) + await ctx.send('{}, your shop has been added to the database: \n{}'.format(ctx.message.author.mention, + formatted_location(shop))) @commands.command(pass_context=True) async def add_town(self, ctx, x_pos: int, z_pos: int, *args): @@ -210,30 +177,19 @@ class GeoffreyCommands(commands.Cog): {}add_town The Town Name parameter is optional if this is your first town """ + errors = { + "LocationLookUpError": "you have more than one location. Please specify a name." + } 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) + town = await run_command(ctx, self.base_url, self.api_token, "POST", "add_town", errors=errors, 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) - await ctx.send(msg) + await ctx.send( + '{}, your town has been added to the database: \n{}'.format(ctx.message.author.mention, + formatted_location(town))) @commands.command(pass_context=True) async def add_tunnel(self, ctx, tunnel_direction, tunnel_number: int, *args): @@ -241,54 +197,35 @@ class GeoffreyCommands(commands.Cog): {}add_tunnel The Name parameter is optional if you only have one location """ + errors = { + "InvalidTunnelError": "{} is not a valid tunnel direction ya gub".format(tunnel_direction), + "LocationLookUpError": "you do not have a location by the name you ding dong goober." + } name = get_name(args) - try: - tunnel = run_command(self.base_url, self.api_token, "POST", "add_tunnel", tunnel_direction=tunnel_direction, - tunnel_number=tunnel_number, location_name=name, discord_uuid=ctx.message.author.id) + tunnel = await run_command(ctx, self.base_url, self.api_token, "POST", "add_tunnel", errors=errors, + tunnel_direction=tunnel_direction, + tunnel_number=tunnel_number, location_name=name, discord_uuid=ctx.message.author.id) - await ctx.send("{}, your tunnel has been added to the database!".format(ctx.message.author.mention)) - - except Exception as e: - error_list = [ - {"error": "LocationHasTunnelError", - "message": "{}, that location already has a tunnel you goober.".format(ctx.message.author.mention) - }, - {"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) - await ctx.send(msg) + await ctx.send("{}, your tunnel has been added to the database!".format(ctx.message.author.mention)) @commands.command(pass_context=True) async def delete(self, ctx, *args): """ {}delete """ - name = get_name(args) - try: - location = run_command(self.base_url, self.api_token, "POST", "delete", name=name, - discord_uuid=ctx.message.author.id) - await ctx.send("{}, **{}** has been deleted from Geoffrey, good riddance.".format(ctx.message.author.mention - , location)) - except Exception as e: - error_list = [ - {"error": "LocationLookUpError", - "message": "{}, you do not have a location by the name you ding dong goober.".format( - ctx.message.author.mention)} - ] + errors = { + "LocationLookUpError": "you do not have a location by the name **{}** you ding dong goober.".format(name) + } - msg = check_error(e, error_list) - await ctx.send(msg) + location = await run_command(ctx, self.base_url, self.api_token, "POST", "delete", errors=errors, name=name, + discord_uuid=ctx.message.author.id) + + await ctx.send("{}, **{}** has been deleted from Geoffrey, good riddance.".format(ctx.message.author.mention + , location)) @commands.command(pass_context=True) async def delete_item(self, ctx, item_name: str, *args): @@ -298,28 +235,19 @@ class GeoffreyCommands(commands.Cog): """ shop_name = get_name(args) - try: - shop = run_command(self.base_url, self.api_token, "POST", "delete_item", item=item_name, - shop_name=shop_name, discord_uuid=ctx.message.author.id) - await ctx.send("{}, **{}** has been deleted from {}, no one bought it anyway.".format( - ctx.message.author.mention, item_name, shop["name"])) + errors = { + "LocationLookUpError": "you do not have a shop by that name you ding dong goober.", + "EntryNameNotUniqueError": "you have more than one location. Please specify a name, dingus.", + "ItemNotFound": "your shop does not sell **{}**. Try again buddy boy.".format(item_name) + } - except Exception as e: - error_list = [ - {"error": "LocationLookUpError", - "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)}, - {"error": "ItemNotFound", - "message": "{}, your shop does not sell **{}**. Try again buddy boy.".format( - ctx.message.author.mention, item_name)} - ] + shop = await run_command(ctx, self.base_url, self.api_token, "POST", "delete_item", errors=errors, + item=item_name, + shop_name=shop_name, discord_uuid=ctx.message.author.id) - msg = check_error(e, error_list) - await ctx.send(msg) + await ctx.send("{}, **{}** has been deleted from {}, no one bought it anyway.".format( + ctx.message.author.mention, item_name, shop["name"])) @commands.command(pass_conext=True) async def edit_name(self, ctx, new_name: str, old_name: str): @@ -328,24 +256,18 @@ class GeoffreyCommands(commands.Cog): If the name has spaces in it, it must be wrapped in quotes. eg "Cool Shop 123" """ - try: - location = run_command(self.base_url, self.api_token, "POST", "edit_name", loc_name=old_name, - new_name=new_name, discord_uuid=ctx.message.author.id) + errors = { + "EntryNameNotUniqueError": "a location is already called **{}** you ding dong goober".format(old_name), + "LocationLookupError": "you do not have a location by the name **{}** you ding dong goober.".format( + old_name) + } - await ctx.send("{}, **{}** has been renamed to **{}**.".format(ctx.message.author.mention, old_name, - location["name"])) - except Exception as e: - error_list = [ - {"error": "EntryNameNotUniqueError", - "message": "{}, a location is already called **{}** you ding dong goober".format( - ctx.message.author.mention, new_name)}, - {"error": "LocationLookUpError", - "message": "{}, you do not have a location by the name **{}** you ding dong goober.".format( - ctx.message.author.mention, old_name)}, - ] + location = await run_command(ctx, self.base_url, self.api_token, "POST", "edit_name", errors=errors, + loc_name=old_name, + new_name=new_name, discord_uuid=ctx.message.author.id) - msg = check_error(e, error_list) - await ctx.send(msg) + await ctx.send("{}, **{}** has been renamed to **{}**.".format(ctx.message.author.mention, old_name, + location["name"])) @commands.command(pass_conext=True) async def edit_pos(self, ctx, new_x: int, new_z: int, *args): @@ -354,21 +276,17 @@ class GeoffreyCommands(commands.Cog): """ loc_name = get_name(args) - try: - location = run_command(self.base_url, self.api_token, "POST", "edit_pos", x=new_x, z=new_z, - loc_name=loc_name, discord_uuid=ctx.message.author.id) - await ctx.send("{}, **{}** has been moved to **{}**".format(ctx.message.author.mention, location["name"], - location["location"])) - except Exception as e: - error_list = [ - {"error": "LocationLookUpError", - "message": "{}, you do not have a location by the name **{}** you ding dong goober.".format( - ctx.message.author.mention, loc_name)}, - ] + errors = { + "LocationLookUpError", + "you do not have a location by the name **{}** you ding dong goober.".format(loc_name) + } + location = await run_command(ctx, self.base_url, self.api_token, "POST", "edit_pos", errors=errors, x=new_x, + z=new_z, + loc_name=loc_name, discord_uuid=ctx.message.author.id) - msg = check_error(e, error_list) - await ctx.send(msg) + await ctx.send("{}, **{}** has been moved to **{}**".format(ctx.message.author.mention, location["name"], + location["location"])) @commands.command(pass_conext=True) async def edit_tunnel(self, ctx, new_tunnel_direction: str, new_tunnel_number: int, *args): @@ -377,27 +295,20 @@ class GeoffreyCommands(commands.Cog): """ loc_name = get_name(args) - try: - location = run_command(self.base_url, self.api_token, "POST", "edit_tunnel", - tunnel_direction=new_tunnel_direction, tunnel_number=new_tunnel_number, - loc_name=loc_name, discord_uuid=ctx.message.author.id) - await ctx.send("{}, **{}**'s tunnel been moved to **{}**".format(ctx.message.author.mention, - location["name"], - location["tunnel"])) - except Exception as e: - error_list = [ - {"error": "LocationLookUpError", - "message": "{}, you do not have a location by the name **{}** you ding dong goober.".format( - ctx.message.author.mention, loc_name)}, - {"error": "InvalidTunnelError", - "message": "{}, {} is not a valid tunnel direction ya gub".format(ctx.message.author.mention, - new_tunnel_direction) - } - ] + errors = { + "LocationLookUpError": "you do not have a location by the name **{}** you ding dong goober.".format( + "loc_name"), + "InvalidLookUpError": "{} is not a valid tunnel direction ya gub".format(new_tunnel_direction) + } - msg = check_error(e, error_list) - await ctx.send(msg) + location = run_command(ctx, self.base_url, self.api_token, "POST", "edit_tunnel", errors=errors, + tunnel_direction=new_tunnel_direction, tunnel_number=new_tunnel_number, + loc_name=loc_name, discord_uuid=ctx.message.author.id) + + await ctx.send("{}, **{}**'s tunnel been moved to **{}**".format(ctx.message.author.mention, + location["name"], + location["tunnel"])) @commands.command(pass_context=True) async def find_around(self, ctx, x_pos, z_pos, *args): @@ -406,31 +317,25 @@ class GeoffreyCommands(commands.Cog): The Radius parameter is optional and defaults to 200 blocks """ - try: - if len(args) > 0: - radius = int(args[0]) - else: - radius = 200 + errors = { + "LocationLookUpError": "there are no locations in that area." + } - locations = run_command(self.base_url, self.api_token, "GET", "find_around", x_pos=x_pos, z_pos=z_pos) + if len(args) > 0: + radius = int(args[0]) + else: + radius = 200 - message = ["{}, the following locations are within **{}** blocks of (x={}, z={}):".format( - ctx.message.author.mention, radius, x_pos, z_pos)] + locations = await run_command(ctx, self.base_url, self.api_token, "GET", "find_around", errors=errors, + x_pos=x_pos, z_pos=z_pos) - for location in locations: - message.append(formatted_location(location)) + message = ["{}, the following locations are within **{}** blocks of (x={}, z={}):".format( + ctx.message.author.mention, radius, x_pos, z_pos)] - await self.bot.send_list(ctx, message) + for location in locations: + message.append(formatted_location(location)) - except Exception as e: - error_list = [ - {"error": "LocationLookUpError", - "message": "{}, there are no locations in that area.".format(ctx.message.author.mention) - } - ] - - msg = check_error(e, error_list) - await ctx.send(msg) + await self.bot.send_list(ctx, message) @commands.command(pass_context=True, aliases=["find"]) async def find_location(self, ctx, *args): @@ -439,25 +344,19 @@ class GeoffreyCommands(commands.Cog): """ search = get_name(args) - try: - locations = run_command(self.base_url, self.api_token, "GET", "find_location", search=search) - message = ["{}, the following locations match **{}**:".format(ctx.message.author.mention, search)] + errors = { + "LocationLookUpError": "there are no locations that match **{}**.".format(search) + } + locations = await run_command(ctx, self.base_url, self.api_token, "GET", "find_location", errors=errors, + search=search) - for location in locations: - message.append(formatted_location(location)) + message = ["{}, the following locations match **{}**:".format(ctx.message.author.mention, search)] - await self.bot.send_list(ctx, message) + for location in locations: + message.append(formatted_location(location)) - except Exception as e: - error_list = [ - {"error": "LocationLookUpError", - "message": "{}, there are no locations that match **{}**.".format(ctx.message.author.mention, search) - } - ] - - msg = check_error(e, error_list) - await ctx.send(msg) + await self.bot.send_list(ctx, message) @commands.command(pass_context=True) async def help(self, ctx, *args): @@ -481,31 +380,25 @@ class GeoffreyCommands(commands.Cog): """ location_name = get_name(args) - 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"]) + errors = { + "LocationLookUpError": "there are no locations that match **{}**.".format(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) + location = await run_command(ctx, self.base_url, self.api_token, "GET", "info", location_name=location_name, + errors=errors) - await ctx.send(message) - await self.bot.send_list(ctx, info_list) + message = "{}, info on {}:\n".format(ctx.message.author.mention, location["name"]) - except Exception as e: - error_list = [ - {"error": "LocationLookUpError", - "message": "{}, there are no locations that match **{}**.".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) - msg = check_error(e, error_list) - await ctx.send(msg) + await ctx.send(message) + await self.bot.send_list(ctx, info_list) @commands.command(pass_context=True) async def me(self, ctx): @@ -513,7 +406,8 @@ class GeoffreyCommands(commands.Cog): {}me """ - locations = run_command(self.base_url, self.api_token, "GET", "me", discord_uuid=ctx.message.author.id) + locations = await run_command(ctx, self.base_url, self.api_token, "GET", "me", + discord_uuid=ctx.message.author.id) message = ["{}, you have the following locations:".format(ctx.message.author.mention)] @@ -527,21 +421,14 @@ class GeoffreyCommands(commands.Cog): """ {}register """ - try: - run_command(self.base_url, self.api_token, "POST", "register", player_name=ctx.message.author.display_name, - discord_uuid=ctx.message.author.id) - await ctx.send("{}, you have been added to the database. Do {}help to see what this bot can do." - .format(ctx.message.author.mention, self.bot.prefix)) - except Exception as e: - error_list = [ - {"error": "PlayerInDBError", - "message": "{}, you are already registered with Geoffrey you ding dong.".format( - ctx.message.author.mention) - } - ] + errors = { + "PlayerinDBError": "you are already registered with Geoffrey you ding dong." + } - msg = check_error(e, error_list) - await ctx.send(msg) + await run_command(self.base_url, self.api_token, "POST", "register", errors=errors, + player_name=ctx.message.author.display_name, + discord_uuid=ctx.message.author.id) + await ctx.send("{}, you have been added to the database. Do {}help to see what this bot can do.") @commands.command(pass_context=True) async def remove_resident(self, ctx, resident_name, *args): @@ -551,29 +438,18 @@ class GeoffreyCommands(commands.Cog): """ 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) - } + errors = { + "ResidentNotFoundError": "ain't no one your town named {} you goob".format(resident_name), + "LocationLookUpError": "you do not have a town called **{}** you ding dong goober.".format(town_name) + } - ] + location = await run_command(ctx, self.base_url, self.api_token, "POST", "remove_resident", errors=errors, + resident_name=resident_name, + town_name=town_name, discord_uuid=ctx.message.author.id) - msg = check_error(e, error_list) - - await ctx.send(msg) + await ctx.send('{}, **{}** has been remove as a resident of **{}**'.format( + ctx.message.author.mention, resident_name, location["name"])) @commands.command(pass_context=True) async def selling(self, ctx, *args): @@ -583,59 +459,46 @@ class GeoffreyCommands(commands.Cog): """ item = get_name(args) - try: - results = run_command(self.base_url, self.api_token, "GET", "selling", item_name=item) + errors = { + "ItemNotFound": "no shop was found selling {}".format(item) + } - message = ["{} The following shop(s) sell **{}**:".format(ctx.message.author.mention, item)] + results = await run_command(ctx, self.base_url, self.api_token, "GET", "selling", errors=errors, item_name=item) - for shop in results: - for line in formatted_shop(shop, base_url=self.base_url): - message.append(line) + message = ["{} The following shop(s) sell **{}**:".format(ctx.message.author.mention, item)] - message.append('') + for shop in results: + for line in formatted_shop(shop, base_url=self.base_url): + message.append(line) - 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) - } - ] + message.append('') - msg = check_error(e, error_list) - await ctx.send(msg) + await self.bot.send_list(ctx, message) @commands.command(pass_context=True) async def selling_price(self, ctx, *args): """ - {}selling - Sorts by most recently added + {}selling_rpice + Sorts by best price """ item = get_name(args) - try: - results = run_command(self.base_url, self.api_token, "GET", "selling_price", item_name=item) + errors = { + "ItemNotFound": "no shop was found selling {}".format(item) + } - message = ["{} The following shop(s) sell **{}**:".format(ctx.message.author.mention, item)] + results = await run_command(ctx, self.base_url, self.api_token, "GET", "selling_price", errors=errors, + item_name=item) - for shop in results: - for line in formatted_shop(shop, base_url=self.base_url): - message.append(line) + message = ["{} The following shop(s) sell **{}**:".format(ctx.message.author.mention, item)] - message.append('') + for shop in results: + for line in formatted_shop(shop, base_url=self.base_url): + message.append(line) - 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) - } - ] + message.append('') - msg = check_error(e, error_list) - await ctx.send(msg) + await self.bot.send_list(ctx, message) @commands.command(pass_context=True) async def restock(self, ctx, item_name, *args): @@ -646,27 +509,17 @@ class GeoffreyCommands(commands.Cog): shop_name = get_name(args) - try: - item = run_command(self.base_url, self.api_token, "POST", "restock", item_name=item_name, - shop_name=shop_name, discord_uuid=ctx.message.author.id) - await ctx.send('{}, **{}** has been restocked at **{}**'.format(ctx.message.author.mention, item_name, - item[0]["shop"]["name"])) - except Exception as e: - error_list = [ - {"error": "ItemNotFound", - "message": "{}, that shop does not have **{}** in its inventory".format(ctx.message.author.mention, - item_name)}, - {"error": "LocationLookUpError", - "message": "{}, you do not have a shop named **{}**.".format(ctx.message.author.mention, shop_name) - }, - {"error": "EntryNameNotUniqueError", - "message": "{}, you have more than one location. Please specify a name, dingus.".format( - ctx.message.author.mention)} - ] - - msg = check_error(e, error_list) - await ctx.send(msg) + errors = { + "ItemNotFound": "that shop does not have **{}** in its inventory".format(item_name), + "LocationLookUpError": "you do not have a shop named **{}**.".format(shop_name), + "EntryNameNotUniqueError": "you have more than one location. Please specify a name, dingus." + } + item = await run_command(ctx, self.base_url, self.api_token, "POST", "restock", errors=errors, + item_name=item_name, + shop_name=shop_name, discord_uuid=ctx.message.author.id) + await ctx.send('{}, **{}** has been restocked at **{}**'.format(ctx.message.author.mention, item_name, + item[0]["shop"]["name"])) @commands.command(pass_context=True) async def tunnel(self, ctx, *args): @@ -675,25 +528,19 @@ class GeoffreyCommands(commands.Cog): """ player_name = get_name(args) - try: - tunnels = run_command(self.base_url, self.api_token, "GET", "tunnel", player_name=player_name) - message = ["{}, **{}** has the following tunnels:".format(ctx.message.author.mention, player_name)] + errors = { + "LocationLookUpError": "**{}** has no tunnels in the database.".format(player_name) + } + tunnels = await run_command(ctx, self.base_url, self.api_token, "GET", "tunnel", errors=errors, + player_name=player_name) - for tunnel in tunnels: - message.append(formatted_tunnel(tunnel)) + message = ["{}, **{}** has the following tunnels:".format(ctx.message.author.mention, player_name)] - await self.bot.send_list(ctx, message) + for tunnel in tunnels: + message.append(formatted_tunnel(tunnel)) - except Exception as e: - error_list = [ - {"error": "LocationLookUpError", - "message": "{}, **{}** has no tunnels in the database.".format(ctx.message.author.mention, player_name) - } - ] - - msg = check_error(e, error_list) - await ctx.send(msg) + await self.bot.send_list(ctx, message) def setup(bot):