Geoffrey-Bot/GeoffreyBot/geoffrey.py

138 lines
5.3 KiB
Python

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)