import discord 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 class Database(): def __init__(self, engine: Engine): 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): INITIAL_EXTENSIONS = [ 'cogs.owner', 'cogs.settings', 'cogs.information', 'cogs.music' ] def __init__(self, config: dict): intents: discord.Intents = discord.Intents.none() intents.voice_states = True intents.guild_messages = True self.config = config self.colors = self.process_colors(config["colors"]) super().__init__(command_prefix=self.prefix_callable, description='CloudKid Radio', case_insensitive=True, fetch_offline_members=False, intents=intents) 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): 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}.') except Exception as e: 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}") print(f"Version: {discord.__version__}") print( f"Invite: https://discord.com/oauth2/authorize?scope=bot&client_id=257816631072391168&permissions=70642768" ) text = "ck!connect | ck!help" 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]: for name, color in colors.items(): colors[name] = discord.Color(int(color, 16)) return colors if __name__ == "__main__": config = json.load(open('config.json', 'r', encoding='utf-8')) token = config.pop("token") CloudKid(config).run(token, reconnect=True)