switch to bot commands extension style bot setup

This commit is contained in:
Xevion
2021-02-07 14:10:42 -06:00
parent 5f832de5f2
commit 288f1e3a61
4 changed files with 109 additions and 65 deletions

44
contest/bot.py Normal file
View File

@@ -0,0 +1,44 @@
import logging
from typing import Optional
import discord
from discord.ext import commands
from contest import constants
from contest.db import ContestDatabase
logger = logging.getLogger(__file__)
logger.setLevel(constants.LOGGING_LEVEL)
async def fetch_prefix(bot: 'ContestBot', message: discord.Message):
"""Fetches the prefix used by the relevant guild."""
user_id = bot.user.id
base = [f'<@!{user_id}> ', f'<@{user_id}> ']
if message.guild:
if bot.db is not None:
base.append(await bot.db.get_prefix(message.guild.id))
return base
class ContestBot(commands.Bot):
def __init__(self, **options):
self.db: Optional[ContestDatabase] = None
super().__init__(fetch_prefix, **options)
async def on_ready(self):
if self.db is None:
self.db = await ContestDatabase.create()
logger.info('Bot is now ready and connected to Discord.')
guild_count = len(self.guilds)
logger.info(
f'Connected as {self.user.name}#{self.user.discriminator} to {guild_count} guild{"s" if guild_count > 1 else ""}.')
async def on_guild_join(self, guild: discord.Guild) -> None:
logger.info(f'Added to new guild: {guild.name} ({guild.id})')
await self.db.setup_guild(guild.id)
async def on_guild_remove(self, guild: discord.Guild) -> None:
logger.info(f'Removed from guild: {guild.name} ({guild.id})')
await self.db.teardown_guild(guild.id)