From aba34799e1c4cc199f567d408a082c555dcc219e Mon Sep 17 00:00:00 2001 From: Interitio Date: Mon, 1 Sep 2025 22:12:49 +1000 Subject: [PATCH] feat: Add custom Context class. --- src/meta/__init__.py | 1 + src/meta/bot.py | 29 ++++++++++++++++++++++++++++- src/meta/context.py | 4 ++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/meta/context.py diff --git a/src/meta/__init__.py b/src/meta/__init__.py index c01addd..9eef7af 100644 --- a/src/meta/__init__.py +++ b/src/meta/__init__.py @@ -1,4 +1,5 @@ from .args import args from .bot import Bot +from .context import Context from .config import Conf, conf from .logger import setup_main_logger, log_context, log_action_stack, log_app, set_logging_context, logging_context, with_log_ctx, persist_task diff --git a/src/meta/bot.py b/src/meta/bot.py index 79cf7f4..3d81de7 100644 --- a/src/meta/bot.py +++ b/src/meta/bot.py @@ -1,5 +1,5 @@ import logging -from typing import Optional +from typing import TYPE_CHECKING, Any, Literal, Optional, overload from twitchio.authentication import UserTokenPayload from twitchio.ext import commands @@ -10,6 +10,10 @@ from botdata import BotData, UserAuth, BotChannel, VersionHistory from constants import BOTUSER_SCOPES, CHANNEL_SCOPES, SCHEMA_VERSIONS from .config import Conf +from .context import Context + +if TYPE_CHECKING: + from modules.profiles.profiles.twitch.component import ProfilesComponent logger = logging.getLogger(__name__) @@ -24,6 +28,7 @@ class Bot(commands.Bot): super().__init__(*args, **kwargs) + # Whether we should do eventsub via webhooks or websockets if config.bot.get('eventsub_secret', None): self.using_webhooks = True else: @@ -36,6 +41,28 @@ class Bot(commands.Bot): self.joined: dict[str, BotChannel] = {} + # Make the type checker happy about fetching components by name + # TODO: Move to stubs + + @property + def profiles(self): + return self.get_component('ProfilesComponent') + + @overload + def get_component(self, name: Literal['ProfilesComponent']) -> 'ProfilesComponent': + ... + + @overload + def get_component(self, name: str) -> Optional[commands.Component]: + ... + + def get_component(self, name: str) -> Optional[commands.Component]: + return super().get_component(name) + + def get_context(self, payload, *, cls: Any = None) -> Context: + cls = cls or Context + return cls(payload, bot=self) + async def event_ready(self): # logger.info(f"Logged in as {self.nick}. User id is {self.user_id}") logger.info("Logged in as %s", self.bot_id) diff --git a/src/meta/context.py b/src/meta/context.py new file mode 100644 index 0000000..2001830 --- /dev/null +++ b/src/meta/context.py @@ -0,0 +1,4 @@ +from twitchio.ext import commands as cmds + +class Context(cmds.Context): + ...