Skip to content

Commit 89ac77c

Browse files
committed
Document get_context
1 parent 57e138b commit 89ac77c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

twitchio/ext/commands/bot.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,36 @@ def get_component(self, name: str, /) -> Component | None:
324324
return self._components.get(name)
325325

326326
def get_context(self, message: ChatMessage, *, cls: Any = None) -> Any:
327+
"""Method to create and retrieve a :class:`~.commands.Context` object from a :class:`twitchio.ChatMessage`.
328+
329+
This method can be overriden to accept a custom class which inherits from :class:`~.commands.Context` for use within
330+
the command invocation process, instead of the default.
331+
332+
Parameters
333+
----------
334+
message: :class:`~twitchio.ChatMessage`
335+
The message to construct a :class:`~.commands.Context` or derivative from.
336+
cls: Any
337+
A custom class to use as the context. This should inherit from :class:`~.commands.Context`.
338+
339+
Examples
340+
--------
341+
342+
.. code:: python3
343+
344+
class CustomContext(commands.Context):
345+
...
346+
347+
class Bot(commands.Bot):
348+
349+
def get_context(self, message: twitchio.ChatMessage, *, cls: Any = None) -> CustomContext:
350+
cls = cls or CustomContext
351+
return cls(message, bot=self)
352+
353+
@commands.command()
354+
async def test(ctx: CustomContext) -> None:
355+
...
356+
"""
327357
cls = cls or Context
328358
return cls(message, bot=self)
329359

0 commit comments

Comments
 (0)