Skip to content

Commit 68cac52

Browse files
committed
Add run_guards method to Command.
1 parent 7e7a003 commit 68cac52

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

twitchio/ext/commands/core.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,37 @@ async def _guard_runner(self, guards: list[Callable[..., bool] | Callable[..., C
501501
if result is not True:
502502
raise GuardFailure(exc_msg, guard=guard)
503503

504+
async def run_guards(self, ctx: Context, *, with_cooldowns: bool = False) -> None:
505+
"""|coro|
506+
507+
Method which allows a :class:`~twitchio.ext.commands.Command` to run and check all associated Guards, including
508+
any cooldowns, with the provided :class:`~twitchio.ext.commands.Context`.
509+
510+
This is useful if you would like to verify if a chatter is allowed to run a command without invoking the callback. E.g.
511+
for displaying help or a list of available commands to a Chatter.
512+
513+
.. versionadded:: 3.1
514+
515+
Parameters
516+
----------
517+
ctx: :class:`~twitchio.ext.commands.Context`
518+
The :class:`~twitchio.ext.commands.Context` to run the Guards against.
519+
with_cooldowns: bool
520+
An optional :class:`bool` which indicates whether any associated cooldowns on this command should also be checked.
521+
Since running a cooldown will trigger an update in the Cooldown bucket, this is ``False`` by default.
522+
523+
Returns
524+
-------
525+
None
526+
The Guard checks successfully ran.
527+
528+
Raises
529+
------
530+
GuardFailure
531+
A Guard failed and would have prevented a user from invoking this command.
532+
"""
533+
await self._run_guards(ctx, with_cooldowns=with_cooldowns)
534+
504535
async def _run_guards(self, context: Context, *, with_cooldowns: bool = True) -> None:
505536
if with_cooldowns and self._cooldowns_first:
506537
await self._run_cooldowns(context)

0 commit comments

Comments
 (0)