50 lines
1.6 KiB
Python
50 lines
1.6 KiB
Python
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.",
|
|
"EntryNameNotUniqueError": "that name has already been used, be more creative dingus kong"
|
|
}
|
|
|
|
|
|
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 + '/GeoffreyApp/api/command/{}/'
|
|
|
|
kwargs["api"] = api_token
|
|
|
|
if request_type == "GET":
|
|
response = requests.get(url=URL.format(command), params=kwargs)
|
|
elif request_type == "POST":
|
|
response = requests.post(url=URL.format(command), data=kwargs)
|
|
else:
|
|
raise TypeError
|
|
|
|
json = response.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"])
|
|
|
|
await ctx.send("{}, {}".format(ctx.message.author.mention, msg))
|
|
|
|
raise HandledError
|
|
else:
|
|
return json |