Changed how errors are handled

+ removed a lot of copy/pasted code
+ errors handled mainly by run_command
+ on_command_error mainly for bot related errors, not Geoffrey errors
master
Joey Hines 2019-05-10 16:22:10 -05:00
parent 78fb85cf17
commit 0fee5135d8
2 changed files with 269 additions and 433 deletions

View File

@ -7,6 +7,7 @@ import logging
import time import time
import traceback import traceback
import sys import sys
from GeoffreyBot.geoffrey_api import HandledError
logger = logging.getLogger('GeoffreyBot') logger = logging.getLogger('GeoffreyBot')
@ -71,34 +72,22 @@ class GeoffreyBot(commands.Bot):
async def on_command_error(self, ctx, error): async def on_command_error(self, ctx, error):
error_str = "" error_str = ""
if isinstance(error, commands.errors.CommandNotFound):
if hasattr(error, "original") and isinstance(error.original, HandledError):
return return
elif isinstance(error, commands.errors.BadArgument): elif isinstance(error, commands.errors.CommandNotFound):
error_str = "Well bud, you got this far. Good job! But you still h*cked up the syntax. " \ return
"Check {}help.".format(self.prefix) elif isinstance(error, commands.errors.BadArgument) or isinstance(error,
elif isinstance(error, commands.errors.MissingRequiredArgument): commands.errors.MissingRequiredArgument) or (
error_str = "Well bud, you got this far. Good job! But you still h*cked up the syntax. " \ hasattr(error, "original") and isinstance(error.original, ValueError)):
"Check {}help.".format(self.prefix) error_str = "Well bud, you got this far. Good job! But you still h*cked up the syntax:"
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." \ for line in self.help_dict[ctx.command.name]:
.format(self.prefix) error_str += line
elif hasattr(error, "original") and isinstance(error.original, request_exception.ConnectionError): 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..." 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?") await self.send_error_message("Can't connect to the 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"
if error_str is '': if error_str is '':
await self.send_error_message( await self.send_error_message(

View File

@ -4,8 +4,26 @@ from GeoffreyBot.GeoffreyApiHelper import *
import requests 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/{}/' URL = base_url + '/api/command/{}/'
kwargs["api"] = api_token kwargs["api"] = api_token
@ -18,23 +36,23 @@ def run_command(base_url, api_token, request_type, command, **kwargs):
raise TypeError raise TypeError
json = response.json() json = response.json()
if "error" in json: if "error" in json:
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"]) raise Exception(json['error'], json["error_message"])
await ctx.send("{}, {}".format(ctx.message.author.mention, msg))
raise HandledError
else: else:
return json 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): class GeoffreyCommands(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
@ -48,29 +66,20 @@ class GeoffreyCommands(commands.Cog):
The Name parameter is optional if this is your first base 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) name = get_name(args)
try: base = await run_command(ctx, self.base_url, self.api_token, "POST", "add_base", x_pos=x_pos, z_pos=z_pos,
base = run_command(self.base_url, self.api_token, "POST", "add_base", x_pos=x_pos, z_pos=z_pos, name=name, name=name,
discord_uuid=ctx.message.author.id) discord_uuid=ctx.message.author.id, errors=errors)
await ctx.send( await ctx.send(
'{}, your base has been added to the database: \n{}'.format(ctx.message.author.mention, '{}, your base has been added to the database: \n{}'.format(ctx.message.author.mention,
formatted_location(base))) 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)
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def add_item(self, ctx, item_name, quantity: int, diamond_price: int, *args): 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 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) shop_name = get_name(args)
try: item = await run_command(ctx, self.base_url, self.api_token, "POST", "add_item",
item = run_command(self.base_url, self.api_token, "POST", "add_item", item_name=item_name, item_name=item_name,
quantity=quantity, quantity=quantity,
diamond_price=diamond_price, shop_name=shop_name, discord_uuid=ctx.message.author.id) diamond_price=diamond_price,
shop_name=shop_name,
discord_uuid=ctx.message.author.id,
errors=errors)
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["item_name"],
item["shop"]["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)}
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def add_owner(self, ctx, new_owner_name, *args): async def add_owner(self, ctx, new_owner_name, *args):
@ -108,70 +115,42 @@ class GeoffreyCommands(commands.Cog):
{}add_owner <New Owner's Name> <Location Name> {}add_owner <New Owner's Name> <Location Name>
WARNING: The new owner had just as much power as you to edit or delete this location. 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) location_name = get_name(args)
try: location = await run_command(ctx, self.base_url, self.api_token, "POST", "add_owner", errors=errors,
location = run_command(self.base_url, self.api_token, "POST", "add_owner", new_owner_name=new_owner_name, new_owner_name=new_owner_name,
location_name=location_name, discord_uuid=ctx.message.author.id) location_name=location_name, discord_uuid=ctx.message.author.id)
await ctx.send('{}, **{}** has been added as an owner to **{}**'.format( await ctx.send('{}, **{}** has been added as an owner to **{}**'.format(
ctx.message.author.mention, new_owner_name, location["name"])) 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)
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def add_resident(self, ctx, new_resident_name, *args): async def add_resident(self, ctx, new_resident_name, *args):
""" """
{}add_resident <New Residents's Name> <Town Name> {}add_resident <New Residents's Name> <Town Name>
""" """
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) town_name = get_name(args)
try: location = await run_command(ctx, self.base_url, self.api_token, "POST", "add_resident", errors=errors,
location = run_command(self.base_url, self.api_token, "POST", "add_resident",
new_resident_name=new_resident_name, new_resident_name=new_resident_name,
town_name=town_name, discord_uuid=ctx.message.author.id) town_name=town_name, discord_uuid=ctx.message.author.id)
await ctx.send('{}, **{}** has been added as a resident to **{}**'.format( await ctx.send('{}, **{}** has been added as a resident to **{}**'.format(
ctx.message.author.mention, new_resident_name, location["name"])) 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)
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def add_shop(self, ctx, x_pos: int, z_pos: int, *args): 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 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) name = get_name(args)
try: shop = await run_command(ctx, self.base_url, self.api_token, "POST", "add_shop", errors=errors, x_pos=x_pos,
shop = run_command(self.base_url, self.api_token, "POST", "add_shop", x_pos=x_pos, z_pos=z_pos, name=name, z_pos=z_pos, name=name, discord_uuid=ctx.message.author.id)
discord_uuid=ctx.message.author.id)
await ctx.send( await ctx.send('{}, your shop has been added to the database: \n{}'.format(ctx.message.author.mention,
'{}, your shop has been added to the database: \n{}'.format(ctx.message.author.mention,
formatted_location(shop))) 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)
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def add_town(self, ctx, x_pos: int, z_pos: int, *args): async def add_town(self, ctx, x_pos: int, z_pos: int, *args):
@ -210,30 +177,19 @@ class GeoffreyCommands(commands.Cog):
{}add_town <X Coordinate> <Z Coordinate> <Shop Name> {}add_town <X Coordinate> <Z Coordinate> <Shop Name>
The Town Name parameter is optional if this is your first 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) name = get_name(args)
try: town = await run_command(ctx, self.base_url, self.api_token, "POST", "add_town", errors=errors, x_pos=x_pos,
town = run_command(self.base_url, self.api_token, "POST", "add_town", x_pos=x_pos, z_pos=z_pos, name=name, z_pos=z_pos, name=name,
discord_uuid=ctx.message.author.id) discord_uuid=ctx.message.author.id)
await ctx.send( await ctx.send(
'{}, your town has been added to the database: \n{}'.format(ctx.message.author.mention, '{}, your town has been added to the database: \n{}'.format(ctx.message.author.mention,
formatted_location(town))) 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)
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def add_tunnel(self, ctx, tunnel_direction, tunnel_number: int, *args): async def add_tunnel(self, ctx, tunnel_direction, tunnel_number: int, *args):
@ -241,54 +197,35 @@ class GeoffreyCommands(commands.Cog):
{}add_tunnel <Tunnel Direction> <Tunnel Number> <Location Name> {}add_tunnel <Tunnel Direction> <Tunnel Number> <Location Name>
The Name parameter is optional if you only have one location 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) name = get_name(args)
try: tunnel = await run_command(ctx, self.base_url, self.api_token, "POST", "add_tunnel", errors=errors,
tunnel = run_command(self.base_url, self.api_token, "POST", "add_tunnel", tunnel_direction=tunnel_direction, tunnel_direction=tunnel_direction,
tunnel_number=tunnel_number, location_name=name, discord_uuid=ctx.message.author.id) 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)) 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)
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def delete(self, ctx, *args): async def delete(self, ctx, *args):
""" """
{}delete <Location Name> {}delete <Location Name>
""" """
name = get_name(args) name = get_name(args)
try:
location = run_command(self.base_url, self.api_token, "POST", "delete", name=name, errors = {
"LocationLookUpError": "you do not have a location by the name **{}** you ding dong goober.".format(name)
}
location = await run_command(ctx, self.base_url, self.api_token, "POST", "delete", errors=errors, name=name,
discord_uuid=ctx.message.author.id) discord_uuid=ctx.message.author.id)
await ctx.send("{}, **{}** has been deleted from Geoffrey, good riddance.".format(ctx.message.author.mention await ctx.send("{}, **{}** has been deleted from Geoffrey, good riddance.".format(ctx.message.author.mention
, 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)}
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def delete_item(self, ctx, item_name: str, *args): async def delete_item(self, ctx, item_name: str, *args):
@ -298,29 +235,20 @@ class GeoffreyCommands(commands.Cog):
""" """
shop_name = get_name(args) shop_name = get_name(args)
try:
shop = run_command(self.base_url, self.api_token, "POST", "delete_item", item=item_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)
}
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) shop_name=shop_name, discord_uuid=ctx.message.author.id)
await ctx.send("{}, **{}** has been deleted from {}, no one bought it anyway.".format( await ctx.send("{}, **{}** has been deleted from {}, no one bought it anyway.".format(
ctx.message.author.mention, item_name, shop["name"])) ctx.message.author.mention, item_name, shop["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)}
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_conext=True) @commands.command(pass_conext=True)
async def edit_name(self, ctx, new_name: str, old_name: str): 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" If the name has spaces in it, it must be wrapped in quotes. eg "Cool Shop 123"
""" """
try: errors = {
location = run_command(self.base_url, self.api_token, "POST", "edit_name", loc_name=old_name, "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)
}
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) new_name=new_name, discord_uuid=ctx.message.author.id)
await ctx.send("{}, **{}** has been renamed to **{}**.".format(ctx.message.author.mention, old_name, await ctx.send("{}, **{}** has been renamed to **{}**.".format(ctx.message.author.mention, old_name,
location["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)},
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_conext=True) @commands.command(pass_conext=True)
async def edit_pos(self, ctx, new_x: int, new_z: int, *args): 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) loc_name = get_name(args)
try:
location = run_command(self.base_url, self.api_token, "POST", "edit_pos", x=new_x, z=new_z, 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) loc_name=loc_name, discord_uuid=ctx.message.author.id)
await ctx.send("{}, **{}** has been moved to **{}**".format(ctx.message.author.mention, location["name"], await ctx.send("{}, **{}** has been moved to **{}**".format(ctx.message.author.mention, location["name"],
location["location"])) 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)},
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_conext=True) @commands.command(pass_conext=True)
async def edit_tunnel(self, ctx, new_tunnel_direction: str, new_tunnel_number: int, *args): 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) loc_name = get_name(args)
try:
location = run_command(self.base_url, self.api_token, "POST", "edit_tunnel", 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)
}
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, tunnel_direction=new_tunnel_direction, tunnel_number=new_tunnel_number,
loc_name=loc_name, discord_uuid=ctx.message.author.id) loc_name=loc_name, discord_uuid=ctx.message.author.id)
await ctx.send("{}, **{}**'s tunnel been moved to **{}**".format(ctx.message.author.mention, await ctx.send("{}, **{}**'s tunnel been moved to **{}**".format(ctx.message.author.mention,
location["name"], location["name"],
location["tunnel"])) 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)
}
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def find_around(self, ctx, x_pos, z_pos, *args): async def find_around(self, ctx, x_pos, z_pos, *args):
@ -406,13 +317,17 @@ class GeoffreyCommands(commands.Cog):
The Radius parameter is optional and defaults to 200 blocks The Radius parameter is optional and defaults to 200 blocks
""" """
try: errors = {
"LocationLookUpError": "there are no locations in that area."
}
if len(args) > 0: if len(args) > 0:
radius = int(args[0]) radius = int(args[0])
else: else:
radius = 200 radius = 200
locations = run_command(self.base_url, self.api_token, "GET", "find_around", x_pos=x_pos, z_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)
message = ["{}, the following locations are within **{}** blocks of (x={}, z={}):".format( message = ["{}, the following locations are within **{}** blocks of (x={}, z={}):".format(
ctx.message.author.mention, radius, x_pos, z_pos)] ctx.message.author.mention, radius, x_pos, z_pos)]
@ -422,16 +337,6 @@ class GeoffreyCommands(commands.Cog):
await self.bot.send_list(ctx, message) await self.bot.send_list(ctx, message)
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)
@commands.command(pass_context=True, aliases=["find"]) @commands.command(pass_context=True, aliases=["find"])
async def find_location(self, ctx, *args): async def find_location(self, ctx, *args):
""" """
@ -439,8 +344,12 @@ class GeoffreyCommands(commands.Cog):
""" """
search = get_name(args) search = get_name(args)
try:
locations = run_command(self.base_url, self.api_token, "GET", "find_location", search=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)
message = ["{}, the following locations match **{}**:".format(ctx.message.author.mention, search)] message = ["{}, the following locations match **{}**:".format(ctx.message.author.mention, search)]
@ -449,16 +358,6 @@ class GeoffreyCommands(commands.Cog):
await self.bot.send_list(ctx, message) await self.bot.send_list(ctx, message)
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)
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def help(self, ctx, *args): async def help(self, ctx, *args):
if ctx.message.guild is not None: if ctx.message.guild is not None:
@ -481,8 +380,13 @@ class GeoffreyCommands(commands.Cog):
""" """
location_name = get_name(args) location_name = get_name(args)
try:
location = run_command(self.base_url, self.api_token, "GET", "info", location_name=location_name) errors = {
"LocationLookUpError": "there are no locations that match **{}**.".format(location_name)
}
location = await run_command(ctx, self.base_url, self.api_token, "GET", "info", location_name=location_name,
errors=errors)
message = "{}, info on {}:\n".format(ctx.message.author.mention, location["name"]) message = "{}, info on {}:\n".format(ctx.message.author.mention, location["name"])
@ -496,24 +400,14 @@ class GeoffreyCommands(commands.Cog):
await ctx.send(message) await ctx.send(message)
await self.bot.send_list(ctx, info_list) await self.bot.send_list(ctx, info_list)
except Exception as e:
error_list = [
{"error": "LocationLookUpError",
"message": "{}, there are no locations that match **{}**.".format(ctx.message.author.mention,
location_name)
}
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def me(self, ctx): async def me(self, ctx):
""" """
{}me {}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)] message = ["{}, you have the following locations:".format(ctx.message.author.mention)]
@ -527,21 +421,14 @@ class GeoffreyCommands(commands.Cog):
""" """
{}register {}register
""" """
try: errors = {
run_command(self.base_url, self.api_token, "POST", "register", player_name=ctx.message.author.display_name, "PlayerinDBError": "you are already registered with Geoffrey you ding dong."
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)
} }
]
msg = check_error(e, error_list) await run_command(self.base_url, self.api_token, "POST", "register", errors=errors,
await ctx.send(msg) 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) @commands.command(pass_context=True)
async def remove_resident(self, ctx, resident_name, *args): async def remove_resident(self, ctx, resident_name, *args):
@ -551,29 +438,18 @@ class GeoffreyCommands(commands.Cog):
""" """
town_name = get_name(args) town_name = get_name(args)
try:
location = run_command(self.base_url, self.api_token, "POST", "remove_resident", 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, resident_name=resident_name,
town_name=town_name, discord_uuid=ctx.message.author.id) town_name=town_name, discord_uuid=ctx.message.author.id)
await ctx.send('{}, **{}** has been remove as a resident of **{}**'.format( await ctx.send('{}, **{}** has been remove as a resident of **{}**'.format(
ctx.message.author.mention, resident_name, location["name"])) 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) @commands.command(pass_context=True)
async def selling(self, ctx, *args): async def selling(self, ctx, *args):
@ -583,8 +459,11 @@ class GeoffreyCommands(commands.Cog):
""" """
item = get_name(args) item = get_name(args)
try: errors = {
results = run_command(self.base_url, self.api_token, "GET", "selling", item_name=item) "ItemNotFound": "no shop was found selling {}".format(item)
}
results = await run_command(ctx, self.base_url, self.api_token, "GET", "selling", errors=errors, item_name=item)
message = ["{} The following shop(s) sell **{}**:".format(ctx.message.author.mention, item)] message = ["{} The following shop(s) sell **{}**:".format(ctx.message.author.mention, item)]
@ -595,27 +474,21 @@ class GeoffreyCommands(commands.Cog):
message.append('') message.append('')
await self.bot.send_list(ctx, message) 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) @commands.command(pass_context=True)
async def selling_price(self, ctx, *args): async def selling_price(self, ctx, *args):
""" """
{}selling <Item Name> {}selling_rpice <Item Name>
Sorts by most recently added Sorts by best price
""" """
item = get_name(args) item = get_name(args)
try: errors = {
results = run_command(self.base_url, self.api_token, "GET", "selling_price", item_name=item) "ItemNotFound": "no shop was found selling {}".format(item)
}
results = await run_command(ctx, self.base_url, self.api_token, "GET", "selling_price", errors=errors,
item_name=item)
message = ["{} The following shop(s) sell **{}**:".format(ctx.message.author.mention, item)] message = ["{} The following shop(s) sell **{}**:".format(ctx.message.author.mention, item)]
@ -626,16 +499,6 @@ class GeoffreyCommands(commands.Cog):
message.append('') message.append('')
await self.bot.send_list(ctx, message) 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) @commands.command(pass_context=True)
async def restock(self, ctx, item_name, *args): async def restock(self, ctx, item_name, *args):
@ -646,27 +509,17 @@ class GeoffreyCommands(commands.Cog):
shop_name = get_name(args) shop_name = get_name(args)
try: errors = {
item = run_command(self.base_url, self.api_token, "POST", "restock", item_name=item_name, "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) shop_name=shop_name, discord_uuid=ctx.message.author.id)
await ctx.send('{}, **{}** has been restocked at **{}**'.format(ctx.message.author.mention, item_name, await ctx.send('{}, **{}** has been restocked at **{}**'.format(ctx.message.author.mention, item_name,
item[0]["shop"]["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)
@commands.command(pass_context=True) @commands.command(pass_context=True)
async def tunnel(self, ctx, *args): async def tunnel(self, ctx, *args):
@ -675,8 +528,12 @@ class GeoffreyCommands(commands.Cog):
""" """
player_name = get_name(args) player_name = get_name(args)
try:
tunnels = run_command(self.base_url, self.api_token, "GET", "tunnel", player_name=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)
message = ["{}, **{}** has the following tunnels:".format(ctx.message.author.mention, player_name)] message = ["{}, **{}** has the following tunnels:".format(ctx.message.author.mention, player_name)]
@ -685,16 +542,6 @@ class GeoffreyCommands(commands.Cog):
await self.bot.send_list(ctx, message) await self.bot.send_list(ctx, message)
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)
def setup(bot): def setup(bot):
bot.add_cog(GeoffreyCommands(bot)) bot.add_cog(GeoffreyCommands(bot))