Initial Template.

This commit is contained in:
2023-11-02 10:09:06 +02:00
commit 1ae7c60ba8
52 changed files with 6786 additions and 0 deletions

6
src/core/__init__.py Normal file
View File

@@ -0,0 +1,6 @@
async def setup(bot):
from .cog import CoreCog
await bot.add_cog(CoreCog(bot))

76
src/core/cog.py Normal file
View File

@@ -0,0 +1,76 @@
import logging
from typing import Optional
from collections import defaultdict
from weakref import WeakValueDictionary
import discord
import discord.app_commands as appcmd
from meta import LionBot, LionCog, LionContext
from meta.app import shardname, appname
from meta.logger import log_wrap
from utils.lib import utc_now
from .data import CoreData
logger = logging.getLogger(__name__)
class keydefaultdict(defaultdict):
def __missing__(self, key):
if self.default_factory is None:
raise KeyError(key)
else:
ret = self[key] = self.default_factory(key)
return ret
class CoreCog(LionCog):
def __init__(self, bot: LionBot):
self.bot = bot
self.data = CoreData()
bot.db.load_registry(self.data)
self.app_config: Optional[CoreData.AppConfig] = None
self.bot_config: Optional[CoreData.BotConfig] = None
self.app_cmd_cache: list[discord.app_commands.AppCommand] = []
self.cmd_name_cache: dict[str, discord.app_commands.AppCommand] = {}
self.mention_cache: dict[str, str] = keydefaultdict(self.mention_cmd)
async def cog_load(self):
# Fetch (and possibly create) core data rows.
self.app_config = await self.data.AppConfig.fetch_or_create(appname)
self.bot_config = await self.data.BotConfig.fetch_or_create(appname)
# Load the app command cache
await self.reload_appcmd_cache()
async def reload_appcmd_cache(self):
for guildid in self.bot.testing_guilds:
self.app_cmd_cache += await self.bot.tree.fetch_commands(guild=discord.Object(guildid))
self.app_cmd_cache += await self.bot.tree.fetch_commands()
self.cmd_name_cache = {cmd.name: cmd for cmd in self.app_cmd_cache}
self.mention_cache = self._mention_cache_from(self.app_cmd_cache)
def _mention_cache_from(self, cmds: list[appcmd.AppCommand | appcmd.AppCommandGroup]):
cache = keydefaultdict(self.mention_cmd)
for cmd in cmds:
cache[cmd.qualified_name if isinstance(cmd, appcmd.AppCommandGroup) else cmd.name] = cmd.mention
subcommands = [option for option in cmd.options if isinstance(option, appcmd.AppCommandGroup)]
if subcommands:
subcache = self._mention_cache_from(subcommands)
cache |= subcache
return cache
def mention_cmd(self, name: str):
"""
Create an application command mention for the given names.
If not found in cache, creates a 'fake' mention with an invalid id.
"""
if name in self.mention_cache:
mention = self.mention_cache[name]
else:
mention = f"</{name}:1110834049204891730>"
return mention

45
src/core/data.py Normal file
View File

@@ -0,0 +1,45 @@
from enum import Enum
from itertools import chain
from psycopg import sql
from cachetools import TTLCache
import discord
from meta import conf
from meta.logger import log_wrap
from data import Table, Registry, Column, RowModel, RegisterEnum
from data.models import WeakCache
from data.columns import Integer, String, Bool, Timestamp
class CoreData(Registry, name="core"):
class AppConfig(RowModel):
"""
Schema
------
CREATE TABLE app_config(
appname TEXT PRIMARY KEY,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
"""
_tablename_ = 'app_config'
appname = String(primary=True)
created_at = Timestamp()
class BotConfig(RowModel):
"""
Schema
------
CREATE TABLE bot_config(
appname TEXT PRIMARY KEY REFERENCES app_config(appname) ON DELETE CASCADE,
sponsor_prompt TEXT,
sponsor_message TEXT,
default_skin TEXT
);
"""
_tablename_ = 'bot_config'
appname = String(primary=True)
default_skin = String()
sponsor_prompt = String()
sponsor_message = String()