Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions twitchio/ext/commands/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from twitchio.user import User

from .components import Component
from .types_ import AutoBotOptions, BotOptions
from .types_ import AutoBotOptions, BotOptions, BotT

PrefixT: TypeAlias = str | Iterable[str] | Callable[["Bot", "ChatMessage"], Coroutine[Any, Any, str | Iterable[str]]]

Expand Down Expand Up @@ -433,15 +433,15 @@ async def test(ctx: CustomContext) -> None:
async def _process_commands(
self, payload: ChatMessage | ChannelPointsRedemptionAdd | ChannelPointsRedemptionUpdate
) -> None:
ctx: Context = self.get_context(payload)
ctx = self.get_context(payload)
await self.invoke(ctx)

async def process_commands(
self, payload: ChatMessage | ChannelPointsRedemptionAdd | ChannelPointsRedemptionUpdate
) -> None:
await self._process_commands(payload)

async def invoke(self, ctx: Context) -> None:
async def invoke(self, ctx: Context[BotT]) -> None:
try:
await ctx.invoke()
except CommandError as e:
Expand Down Expand Up @@ -482,7 +482,7 @@ async def event_command_error(self, payload: CommandErrorPayload) -> None:
msg = f'Ignoring exception in command "{payload.context.command}":\n'
logger.error(msg, exc_info=payload.exception)

async def before_invoke(self, ctx: Context) -> None:
async def before_invoke(self, ctx: Context[BotT]) -> None:
"""A pre invoke hook for all commands that have been added to the bot.

Commands from :class:`~.commands.Component`'s are included, however if you wish to control them separately,
Expand Down Expand Up @@ -513,7 +513,7 @@ async def before_invoke(self, ctx: Context) -> None:
The context associated with command invocation, before being passed to the command.
"""

async def after_invoke(self, ctx: Context) -> None:
async def after_invoke(self, ctx: Context[BotT]) -> None:
"""A post invoke hook for all commands that have been added to the bot.

Commands from :class:`~.commands.Component`'s are included, however if you wish to control them separately,
Expand Down Expand Up @@ -544,7 +544,7 @@ async def after_invoke(self, ctx: Context) -> None:
The context associated with command invocation, after being passed through the command.
"""

async def global_guard(self, ctx: Context, /) -> bool:
async def global_guard(self, ctx: Context[BotT], /) -> bool:
"""|coro|

A global guard applied to all commmands added to the bot.
Expand Down
6 changes: 3 additions & 3 deletions twitchio/ext/commands/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from collections.abc import Callable

from .context import Context
from .types_ import ComponentOptions
from .types_ import BotT, ComponentOptions


__all__ = ("Component",)
Expand Down Expand Up @@ -262,13 +262,13 @@ async def component_teardown(self) -> None:
This method is intended to be overwritten, by default it does nothing.
"""

async def component_before_invoke(self, ctx: Context) -> None:
async def component_before_invoke(self, ctx: Context[BotT]) -> None:
"""Hook called before a :class:`~.commands.Command` in this Component is invoked.

Similar to :meth:`~.commands.Bot.before_invoke` but only applies to commands in this Component.
"""

async def component_after_invoke(self, ctx: Context) -> None:
async def component_after_invoke(self, ctx: Context[BotT]) -> None:
"""Hook called after a :class:`~.commands.Command` has successfully invoked in this Component.

Similar to :meth:`~.commands.Bot.after_invoke` but only applies to commands in this Component.
Expand Down
11 changes: 6 additions & 5 deletions twitchio/ext/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
from __future__ import annotations

from collections.abc import Iterable
from typing import TYPE_CHECKING, Any, Literal, TypeAlias
from typing import TYPE_CHECKING, Any, Generic, Literal, TypeAlias

from twitchio.models.eventsub_ import ChannelPointsRedemptionAdd, ChannelPointsRedemptionUpdate, ChatMessage

from .core import CommandErrorPayload, ContextType, RewardCommand, RewardStatus
from .exceptions import *
from .types_ import BotT
from .view import StringView


Expand All @@ -50,7 +51,7 @@
PrefixT: TypeAlias = str | Iterable[str] | Callable[[Bot, ChatMessage], Coroutine[Any, Any, str | Iterable[str]]]


class Context:
class Context(Generic[BotT]):
"""The Context class constructed when a message or reward redemption in the respective events is received and processed
in a :class:`~.commands.Bot`.

Expand All @@ -77,10 +78,10 @@ def __init__(
self,
payload: ChatMessage | ChannelPointsRedemptionAdd | ChannelPointsRedemptionUpdate,
*,
bot: Bot,
bot: BotT,
) -> None:
self._payload: ChatMessage | ChannelPointsRedemptionAdd | ChannelPointsRedemptionUpdate = payload
self._bot: Bot = bot
self._bot = bot
self._component: Component | None = None
self._prefix: str | None = None

Expand Down Expand Up @@ -220,7 +221,7 @@ def channel(self) -> PartialUser:
return self.broadcaster

@property
def bot(self) -> Bot:
def bot(self) -> BotT:
"""Property returning the :class:`~.commands.Bot` object."""
return self._bot

Expand Down
3 changes: 2 additions & 1 deletion twitchio/ext/commands/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
if TYPE_CHECKING:
from .bot import Bot
from .context import Context
from .types_ import BotT

__all__ = ("_BaseConverter",)

Expand Down Expand Up @@ -67,7 +68,7 @@ def _bool(self, arg: str) -> bool:

return result

async def _user(self, context: Context, arg: str) -> User:
async def _user(self, context: Context[BotT], arg: str) -> User:
arg = arg.lower()
users: list[User]
msg: str = 'Failed to convert "{}" to User. A User with the ID or login could not be found.'
Expand Down
5 changes: 3 additions & 2 deletions twitchio/ext/commands/cooldowns.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import twitchio

from .context import Context
from .types_ import BotT


__all__ = ("BaseCooldown", "Bucket", "BucketType", "Cooldown", "GCRACooldown")
Expand Down Expand Up @@ -65,7 +66,7 @@ class BucketType(enum.Enum):
channel = 2
chatter = 3

def get_key(self, payload: twitchio.ChatMessage | Context) -> Any:
def get_key(self, payload: twitchio.ChatMessage | Context[BotT]) -> Any:
if self is BucketType.user:
return payload.chatter.id

Expand All @@ -75,7 +76,7 @@ def get_key(self, payload: twitchio.ChatMessage | Context) -> Any:
elif self is BucketType.chatter:
return (payload.broadcaster.id, payload.chatter.id)

def __call__(self, payload: twitchio.ChatMessage | Context) -> Any:
def __call__(self, payload: twitchio.ChatMessage | Context[BotT]) -> Any:
return self.get_key(payload)


Expand Down
Loading