Geoffrey-Bot/GeoffreyBot/geoffrey_api.py

701 lines
28 KiB
Python

from discord.ext import commands
from GeoffreyBot.DiscordHelperFunctions import *
from GeoffreyBot.GeoffreyApiHelper import *
import requests
def run_command(base_url, api_token, request_type, command, **kwargs):
URL = base_url + '/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:
raise Exception(json['error'], json["error_message"])
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
self.base_url = bot.base_url
self.api_token = bot.api_token
@commands.command(pass_context=True)
async def add_base(self, ctx, x_pos: int, z_pos: int, *args):
"""
{}add_base <X Coordinate> <Z Coordinate> <Name>
The Name parameter is optional if this is your first base
"""
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)
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)
@commands.command(pass_context=True)
async def add_item(self, ctx, item_name, quantity: int, diamond_price: int, *args):
"""
{}add_item <Item Name> <Quantity> <Diamond Price> <Shop Name>
If item name contains spaces, it must be wrapped in quotes. eg "Diamond Pickaxe"
The Shop Name parameter is optional if this is your first shop
"""
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)}
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_context=True)
async def add_owner(self, ctx, new_owner_name, *args):
"""
{}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.
"""
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)
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)
@commands.command(pass_context=True)
async def add_resident(self, ctx, new_resident_name, *args):
"""
{}add_resident <New Residents's Name> <Town Name>
"""
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)
}
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_context=True)
async def add_shop(self, ctx, x_pos: int, z_pos: int, *args):
"""
{}add_shop <X Coordinate> <Z Coordinate> <Shop Name>
The Shop Name parameter is optional if this is your first shop
"""
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)
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)
@commands.command(pass_context=True)
async def add_town(self, ctx, x_pos: int, z_pos: int, *args):
"""
{}add_town <X Coordinate> <Z Coordinate> <Shop Name>
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)
await ctx.send(msg)
@commands.command(pass_context=True)
async def add_tunnel(self, ctx, tunnel_direction, tunnel_number: int, *args):
"""
{}add_tunnel <Tunnel Direction> <Tunnel Number> <Location Name>
The Name parameter is optional if you only have one location
"""
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)
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)
async def delete(self, ctx, *args):
"""
{}delete <Location Name>
"""
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)}
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_context=True)
async def delete_item(self, ctx, item_name: str, *args):
"""
{}delete_item <Item Name> <Shop Name>
The Shop Name parameter is optional if you only have one location.
"""
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"]))
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)
async def edit_name(self, ctx, new_name: str, old_name: str):
"""
{}edit_name <New Name> <Old Name>
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)
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)},
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_conext=True)
async def edit_pos(self, ctx, new_x: int, new_z: int, *args):
"""
{}edit_post <New X Coordinate> <New Z Coordinate> <Location Name>
"""
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)},
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_conext=True)
async def edit_tunnel(self, ctx, new_tunnel_direction: str, new_tunnel_number: int, *args):
"""
{}edit_tunnel <New Tunnel Direction> <New Tunnel Number> <Location Name>
"""
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)
}
]
msg = check_error(e, error_list)
await ctx.send(msg)
@commands.command(pass_context=True)
async def find_around(self, ctx, x_pos, z_pos, *args):
"""
{}find_around <X Coordinate> <Z Coordinate> <Radius>
The Radius parameter is optional and defaults to 200 blocks
"""
try:
if len(args) > 0:
radius = int(args[0])
else:
radius = 200
locations = run_command(self.base_url, self.api_token, "GET", "find_around", x_pos=x_pos, z_pos=z_pos)
message = ["{}, the following locations are within **{}** blocks of (x={}, z={}):".format(
ctx.message.author.mention, radius, x_pos, z_pos)]
for location in locations:
message.append(formatted_location(location))
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"])
async def find_location(self, ctx, *args):
"""
{}find_location <Location or Player Name>
"""
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)]
for location in locations:
message.append(formatted_location(location))
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)
async def help(self, ctx, *args):
if ctx.message.guild is not None:
await ctx.send("{}, I sent you some help in the DMs.".format(ctx.message.author.mention))
if len(args) > 0:
command = args[0].lower()
try:
await self.bot.send_list(ctx.message.author, self.bot.help_dict[command])
except KeyError:
await ctx.message.author.send("{}, no command named **{}** exists. Try {}help".format(
ctx.message.author.mention, command, self.bot.prefix))
else:
await self.bot.send_list(ctx.message.author, self.bot.help_page)
@commands.command(pass_context=True)
async def info(self, ctx, *args):
"""
{}info <Location Name>
"""
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"])
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 = [
{"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)
async def me(self, ctx):
"""
{}me
"""
locations = run_command(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)]
for location in locations:
message.append(formatted_location(location))
await self.bot.send_list(ctx, message)
@commands.command(pass_context=True)
async def register(self, ctx):
"""
{}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)
}
]
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 <Resident Name> <Town Name>
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):
"""
{}selling <Item Name>
Sorts by most recently added
"""
item = get_name(args)
try:
results = run_command(self.base_url, self.api_token, "GET", "selling", 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:
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 <Item Name>
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:
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 restock(self, ctx, item_name, *args):
"""
{}restock <Item name> <Shop Name>
The Shop Name is optional if you only have one shop.
"""
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)
@commands.command(pass_context=True)
async def tunnel(self, ctx, *args):
"""
{}tunnel <Player Name>
"""
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)]
for tunnel in tunnels:
message.append(formatted_tunnel(tunnel))
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):
bot.add_cog(GeoffreyCommands(bot))