Small refactor
+ renamed GeoffreyApiHelper to geoffrey_formatter + moved some utility functions to utilmaster
parent
a5ba46388c
commit
84ac092ffb
|
@ -8,7 +8,7 @@ import time
|
||||||
import traceback
|
import traceback
|
||||||
import sys
|
import sys
|
||||||
from GeoffreyBot.geoffrey_api import HandledError
|
from GeoffreyBot.geoffrey_api import HandledError
|
||||||
from GeoffreyBot.GeoffreyApiHelper import format_message
|
from GeoffreyBot.geoffrey_formatter import format_message
|
||||||
|
|
||||||
logger = logging.getLogger('GeoffreyBot')
|
logger = logging.getLogger('GeoffreyBot')
|
||||||
|
|
||||||
|
|
|
@ -1,57 +1,7 @@
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from GeoffreyBot.DiscordHelperFunctions import *
|
from GeoffreyBot.DiscordHelperFunctions import *
|
||||||
from GeoffreyBot.GeoffreyApiHelper import *
|
from GeoffreyBot.geoffrey_formatter import *
|
||||||
|
from GeoffreyBot.util import run_command, HandledError
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
class GeoffreyCommands(commands.Cog):
|
class GeoffreyCommands(commands.Cog):
|
||||||
|
|
|
@ -27,10 +27,10 @@ def formatted_item_listing(item):
|
||||||
def formatted_location(location):
|
def formatted_location(location):
|
||||||
if location["tunnel"] is not None:
|
if location["tunnel"] is not None:
|
||||||
return format_message("**{}** @ **{}** **{}** {}", location["name"], formatted_position(location),
|
return format_message("**{}** @ **{}** **{}** {}", location["name"], formatted_position(location),
|
||||||
location["tunnel"],
|
location["tunnel"], formatted_owners(location))
|
||||||
formatted_owners(location))
|
|
||||||
else:
|
else:
|
||||||
return format_message("**{}** @ **{}** {}", location["name"], formatted_position(location), formatted_owners(location))
|
return format_message("**{}** @ **{}** {}", location["name"], formatted_position(location),
|
||||||
|
formatted_owners(location))
|
||||||
|
|
||||||
|
|
||||||
def formatted_tunnel(tunnel):
|
def formatted_tunnel(tunnel):
|
|
@ -0,0 +1,50 @@
|
||||||
|
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
|
Loading…
Reference in New Issue