All v1.0 Geoffrey bot commands re-implemented

master
Joey Hines 2019-01-14 14:20:16 -06:00
parent 29f2bab6d4
commit 2cfe8aeab8
7 changed files with 603 additions and 127 deletions

2
.gitignore vendored
View File

@ -115,4 +115,6 @@ dmypy.json
.pyre/
.idea/*
start_geoffrey.py

View File

@ -1,3 +1,29 @@
def formatted_item_listing(item):
return "{} {} for {}D".format(item["amount"], item["item_name"], item["price"])
return "{} {} for {}D".format(item["amount"], item["item_name"], item["price"])
def formatted_location(location):
if location["tunnel"] is not None:
tunnel = "**{}** ".format(location["tunnel"])
else:
tunnel = ""
return '**{}** @ **{}** {}Owner: **{}**'.format(location["name"], formatted_position(location), tunnel,
location["owner"]["name"])
def formatted_tunnel(tunnel):
return '**{} {}**: {}'.format(tunnel["tunnel_direction"], tunnel["tunnel_number"], tunnel["location_name"])
def formatted_position(location):
return '(x={}, z={})'.format(location["x_coord"], location["z_coord"])
def formatted_shop(shop):
inventory = ""
for item in shop["items"]:
inventory = inventory + "\n" + (formatted_item_listing(item))
return formatted_location(shop) + inventory

View File

@ -0,0 +1 @@
__version__ = '2.0'

View File

@ -1,120 +1,12 @@
import asyncio
import logging
import time
from discord import Game
from discord.ext import commands
from discord.utils import oauth_url
from sys import stdout
import requests
logger = logging.getLogger(__name__)
description = '''
Geoffrey (pronounced JOFF-ree) started his life as an inside joke none of you will understand.
At some point, she was to become an airhorn discord_bot. Now, they know where your stuff is.
Please respect Geoffrey, the discord_bot is very sensitive.
All commands must be prefaced with '?'
If have a suggestion or if something is borked, you can PM my ding dong of a creator BirbHD.
*You must use ?register before adding things to Geoffrey*
For a better a explanation on how this discord_bot works go the following link:
https://github.com/joeyahines/Geoffrey/blob/master/README.md
'''
bad_error_message = 'OOPSIE WOOPSIE!! Uwu We made a fucky wucky!! A wittle fucko boingo! The admins at our ' \
'headquarters are working VEWY HAWD to fix this! (Error in command {})'
extensions = [
#'GeoffreyApp.cogs.Add_Commands',
#'GeoffreyApp.cogs.Delete_Commands',
#'GeoffreyApp.cogs.Edit_Commands',
'GeoffreyApp.assets.bots.discord_bot.cogs.Search_Commands',
#'GeoffreyApp.cogs.Admin_Commands'
]
class GeoffreyBot(commands.Bot):
def __init__(self):
URL = 'http://127.0.0.1:8000/api/settings/'
setting = {}
while True:
try:
setting = requests.get(url=URL).json()
break
except Exception:
time.sleep(1)
super().__init__(command_prefix=setting['BOT_PREFIX'], description=description, pm_help=True,
case_insensitive=True)
self.error_users = setting['ERROR_USERS']
self.admin_users = setting['MOD_RANK']
self.special_users = []
self.default_status = setting['DEFAULT_STATUS']
for extension in extensions:
try:
self.load_extension(extension)
except Exception as e:
logger.info('Failed to load extension {}'.format(extension))
raise e
async def on_ready(self):
logger.info("%s Online, ID: %s", self.user.name, self.user.id)
info = await self.application_info()
url = oauth_url(info.id)
logger.info("Bot url: %s", url)
await self.change_presence(activity=Game(self.default_status))
async def on_command(self, ctx):
URL = 'http://127.0.0.1:8000/api/command/{}'
if ctx.invoked_subcommand is None:
subcommand = ""
else:
subcommand = ":" + ctx.invoked_subcommand.__str__()
logger.info("User %s, used command %s%s with context: %s", ctx.message.author, ctx.command.name, subcommand,
ctx.args)
if ctx.invoked_with.lower() == 'help' and ctx.message.guild is not None:
await ctx.send("{}, I sent you some help in the DMs.".format(ctx.message.author.mention))
async def on_command_error(self, ctx, error):
error_str = ''
if error_str is '':
await self.send_error_message(
'Geoffrey encountered unhandled exception: {} Command: **{}** Context: {}'.format(error,
ctx.command.name,
ctx.args))
error_str = bad_error_message.format(ctx.invoked_with)
logger.error("Geoffrey encountered exception: %s", error)
await ctx.message.channel.send('{} **Error Running Command:** {}'.format(
ctx.message.author.mention, error_str))
async def send_error_message(self, msg):
for user_id in self.error_users:
user = await self.get_user_info(user_id)
await user.send(msg)
def run_command(self, command, args):
URL = 'http://127.0.0.1:8000/api/command/{}'
return requests.get(url=URL.format(command), params=args).json()
from GeoffreyBot.geoffrey import *
def setup_logging():
discord_logger = logging.getLogger('discord')
discord_logger.setLevel(logging.INFO)
bot_info_logger = logging.getLogger('GeoffreyApp.api.discord_bot.discord_bot')
bot_info_logger = logging.getLogger('GeoffreyBot')
bot_info_logger.setLevel(logging.INFO)
console = logging.StreamHandler(stdout)
console.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
@ -122,23 +14,12 @@ def setup_logging():
bot_info_logger.addHandler(console)
def start_bot():
asyncio.set_event_loop(asyncio.new_event_loop())
bot = None
def start_bot(discord_token, geoffrey_api_token, geoffrey_base_url):
try:
bot = GeoffreyBot()
@bot.command(pass_context=True)
async def test(ctx):
"""
Checks if the discord_bot is alive.
"""
await ctx.send('I\'m here you ding dong')
await ctx.send(bot.get)
setup_logging()
bot = GeoffreyBot(geoffrey_base_url, geoffrey_api_token)
bot.run("MTgzMDMyMDE5MTk2ODM3ODg4.DeSPKw.7FnBb4fu2b3CVL2Ls9PZZMDBQQc")
bot.run(discord_token)
except KeyboardInterrupt:
logger.info("Bot received keyboard interrupt")
@ -146,6 +27,4 @@ def start_bot():
print(e)
logger.info('Bot encountered the following unhandled exception %s', e)
finally:
if bot is not None:
bot.loop.stop()
logger.info("Bot shutting down...")

View File

@ -0,0 +1,137 @@
from discord import Game
from discord.ext import commands
from discord.utils import oauth_url
import requests
import logging
import time
import traceback
import sys
logger = logging.getLogger('GeoffreyBot')
description = '''
Geoffrey (pronounced JOFF-ree) started his life as an inside joke none of you will understand.
At some point, she was to become an airhorn discord_bot. Now, they know where your stuff is.
Please respect Geoffrey, the discord_bot is very sensitive.
All commands must be prefaced with '?'
If have a suggestion or if something is borked, you can PM my ding dong of a creator ZeroHD.
*You must use ?register before adding things to Geoffrey*
For a better a explanation on how this discord_bot works go the following link:
https://github.com/joeyahines/Geoffrey/blob/master/README.md
'''
bad_error_message = 'OOPSIE WOOPSIE!! Uwu We made a fucky wucky!! A wittle fucko boingo! The admins at our ' \
'headquarters are working VEWY HAWD to fix this! (Error in command {})'
class GeoffreyBot(commands.Bot):
def __init__(self, base_url, api_token):
self.base_url = base_url
self.api_token = api_token
URL = base_url + '/api/settings/'
logger.info("Connecting to the Geoffrey API... ")
while True:
try:
setting = requests.get(url=URL, params={"api": self.api_token}).json()
break
except Exception:
time.sleep(1)
self.prefix = setting['BOT_PREFIX']
self.error_users = setting['ERROR_USERS']
self.admin_users = setting['MOD_RANKS']
self.special_users = []
self.default_status = setting['STATUS']
super().__init__(command_prefix=self.prefix, description=description, pm_help=True,
case_insensitive=True)
self.load_extension('GeoffreyBot.geoffrey_api')
async def on_ready(self):
logger.info("%s Online, ID: %s", self.user.name, self.user.id)
info = await self.application_info()
url = oauth_url(info.id)
logger.info("Bot url: %s", url)
await self.change_presence(activity=Game(self.default_status))
async def on_command(self, ctx):
URL = self.base_url + '/api/command/{}'
if ctx.invoked_subcommand is None:
subcommand = ""
else:
subcommand = ":" + ctx.invoked_subcommand.__str__()
logger.info("User %s, used command %s%s with context: %s", ctx.message.author, ctx.command.name, subcommand,
ctx.args)
if ctx.invoked_with.lower() == 'help' and ctx.message.guild is not None:
await ctx.send("{}, I sent you some help in the DMs.".format(ctx.message.author.mention))
async def on_command_error(self, ctx, error):
error_str = ""
if isinstance(error, commands.errors.CommandNotFound):
return
elif isinstance(error, commands.errors.BadArgument):
error_str = "That's not how you use the command you ding dong."
elif isinstance(error, commands.errors.MissingRequiredArgument):
error_str = "Well bud, you got this far. Good job! But you still h*cked up the syntax. " \
"Check {}help.".format(self.prefix)
elif len(error.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":
error_str = "Well bud, you got this far. Good job! But you still h*cked up the syntax. Check {}help."\
.format(self.prefix)
if error_str is '':
await self.send_error_message(
'Geoffrey encountered unhandled exception: {} Command: **{}** Context: {}'.format(error,
ctx.command.name,
ctx.args))
error_str = bad_error_message.format(ctx.invoked_with)
logger.error("Geoffrey encountered exception: %s", error)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
await ctx.message.channel.send('{} **Error Running Command:** {}'.format(
ctx.message.author.mention, error_str))
async def send_error_message(self, msg):
for user_id in self.error_users:
user = await self.get_user_info(user_id)
await user.send(msg)
def run_command(self, command, **kwargs):
URL = self.base_url + '/api/command/{}'
return requests.get(url=URL.format(command), params=kwargs).json()
async def send_list(self, ctx, send_list):
size = 0
msg = ""
for s in send_list:
if (size + len(s)) > 2000:
await ctx.send(msg)
msg = s
size = len(s)
else:
msg = msg + '\n' + s
size = size + len(s)
if len(msg) > 0:
await ctx.send(msg)

View File

@ -0,0 +1,422 @@
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):
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):
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):
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):
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):
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."}
]
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):
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):
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):
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):
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):
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)
async def find(self, ctx, *args):
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 info(self, ctx, *args):
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):
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):
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):
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):
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):
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))

9
setup.py 100644
View File

@ -0,0 +1,9 @@
from distutils.core import setup
setup(
name='Geoffrey-Discordbot',
version=__import__('GeoffreyBot').__version__,
packages=['GeoffreyBot, GeoffreyBot.cogs'],
install_requires=['git+https://github.com/Rapptz/discord.py@rewrite', 'requests'],
long_description=open('README').read(),
)