510 lines
20 KiB
Python
510 lines
20 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:
|
|
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_pos> <z_pos> <Name>
|
|
The name parameter is optional.
|
|
'''
|
|
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\n{}'.format(ctx.message.author.mention,
|
|
formatted_location(base)))
|
|
except Exception as e:
|
|
error_list = [
|
|
{"error": "EntryNameNotUniqueError",
|
|
"message": "{}, a base with that name already exists, be more unique ding dong".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>
|
|
The name parameter is optional.
|
|
'''
|
|
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_shop(self, ctx, x_pos: int, z_pos: int, *args):
|
|
'''
|
|
{}add_shop <x_pos> <z_pos> <Name>
|
|
The name parameter is optional.
|
|
'''
|
|
|
|
name = get_name(args)
|
|
|
|
try:
|
|
base = 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\n{}'.format(ctx.message.author.mention,
|
|
formatted_location(base)))
|
|
except Exception as e:
|
|
error_list = [
|
|
{"error": "EntryNameNotUniqueError",
|
|
"message": "{}, a shop with that name already exists, be more unique ding dong".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.
|
|
'''
|
|
|
|
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)
|
|
}
|
|
|
|
]
|
|
|
|
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)}
|
|
]
|
|
|
|
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> <Location Name>
|
|
The name parameter is optional.
|
|
'''
|
|
|
|
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 location by the name you ding dong goober."},
|
|
{"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 Coord> <New Z Coord> <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 Position> <Z Position> <radius>
|
|
'''
|
|
|
|
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)
|
|
},
|
|
{
|
|
"error": "ValueError",
|
|
"message": "{}, {} is an invalid radius, the radius must be a whole number.".format(
|
|
ctx.message.author.mention, args[0])
|
|
}
|
|
]
|
|
|
|
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"],
|
|
formatted_location(location))
|
|
await ctx.send(message)
|
|
|
|
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 selling(self, ctx, *args):
|
|
'''
|
|
{}selling <item name>
|
|
'''
|
|
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:
|
|
message.append(formatted_shop(shop))
|
|
|
|
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
|
|
'''
|
|
|
|
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))
|