Geoffrey-Bot/GeoffreyBot/bot.py

152 lines
5.2 KiB
Python

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()
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.setLevel(logging.INFO)
console = logging.StreamHandler(stdout)
console.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
bot_info_logger.addHandler(console)
def start_bot():
asyncio.set_event_loop(asyncio.new_event_loop())
bot = None
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.run("MTgzMDMyMDE5MTk2ODM3ODg4.DeSPKw.7FnBb4fu2b3CVL2Ls9PZZMDBQQc")
except KeyboardInterrupt:
logger.info("Bot received keyboard interrupt")
except Exception as e:
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...")