WIP Cleaning up the code base + impl slash commands

This commit is contained in:
2021-10-30 22:05:40 +02:00
parent 425d3a3a92
commit bcf3d957a8
16 changed files with 973 additions and 541 deletions
+32 -44
View File
@@ -1,68 +1,55 @@
import discord
from discord.colour import Color
from discord.ext import commands
import sys
import json
import asyncio
import sqlalchemy as sa
from aiomysql.sa import Engine
from tables import get_tables
from typing import Sequence
from aiomysql.sa import create_engine
from discord import ActivityType
from discord import Status
from typing import Mapping
from typing import Any, Dict, Sequence
from discord import Message
class Database():
def __init__(self, engine: Engine):
class Database:
def __init__(self):
self.loop = asyncio.get_event_loop()
self.metadata = sa.MetaData()
self.tables = get_tables(self.metadata)
self.engine = engine
@staticmethod
async def init(dbconf: dict):
engine = await create_engine(**dbconf, autocommit=True)
return Database(engine)
class CloudKid(commands.Bot):
class ChristmasBot(commands.Bot):
INITIAL_EXTENSIONS = [
'cogs.owner', 'cogs.settings', 'cogs.information', 'cogs.music'
"cogs.owner",
"cogs.settings",
"cogs.information",
"cogs.music",
]
def __init__(self, config: dict):
def __init__(self, config: Dict[Any, Any]):
intents: discord.Intents = discord.Intents.none()
intents.voice_states = True
intents.guild_messages = True
intents.guilds = True
intents.messages = True
self.config = config
self.colors = self.process_colors(config["colors"])
self.colors: Dict[str, Color] = self.process_colours(config.get("colors", []))
super().__init__(command_prefix=self.prefix_callable,
description='CloudKid Radio',
case_insensitive=True,
fetch_offline_members=False,
intents=intents)
super().__init__(
command_prefix=self.prefix_callable,
description=self.config["info"].get("description"),
case_insensitive=False,
fetch_offline_members=False,
intents=intents,
slash_command_guilds=[227431704426446848],
)
self.database = None
self.loop.create_task(self.async_init())
async def async_init(self):
dbconf = self.config["database"]
self.database = await Database.init(dbconf)
async def prefix_callable(self, _, msg):
async def prefix_callable(self, _, msg: Message):
return commands.when_mentioned_or(*self.config["prefixes"])(self, msg)
async def load_cogs(self, cog_names: Sequence[str]):
for cog in cog_names:
try:
self.load_extension(cog)
print(f'Succesfully loaded extension {cog}.')
print(f"Succesfully loaded extension {cog}.")
except Exception as e:
print(f'Failed to load extension {cog}.\n\t{e}',
file=sys.stderr)
print(f"Failed to load extension {cog}.\n\t{e}", file=sys.stderr)
async def on_ready(self):
print(f"Logged in as: {self.user}")
@@ -74,14 +61,15 @@ class CloudKid(commands.Bot):
await self.change_presence(activity=discord.Game(text))
await self.load_cogs(self.INITIAL_EXTENSIONS)
def process_colors(
self, colors: Mapping[str, str]) -> Mapping[str, discord.Color]:
def process_colours(self, colors: Dict[str, str]) -> Dict[str, Color]:
colour_dict: Dict[str, Color] = {}
for name, color in colors.items():
colors[name] = discord.Color(int(color, 16))
return colors
colour_dict[name] = Color(int(color, 16))
return colour_dict
if __name__ == "__main__":
config = json.load(open('config.json', 'r', encoding='utf-8'))
config = json.load(open("config.json", "r", encoding="utf-8"))
token = config.pop("token")
CloudKid(config).run(token, reconnect=True)
bot = ChristmasBot(config)
bot.run(token, reconnect=True)