Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add once kwarg to Cog.listener #2403

Merged
merged 5 commits into from Mar 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion discord/cog.py
Expand Up @@ -379,7 +379,9 @@ def _get_overridden_method(cls, method: FuncT) -> FuncT | None:
)

@classmethod
def listener(cls, name: str = MISSING) -> Callable[[FuncT], FuncT]:
def listener(
cls, name: str = MISSING, once: bool = False
) -> Callable[[FuncT], FuncT]:
"""A decorator that marks a function as a listener.

This is the cog equivalent of :meth:`.Bot.listen`.
Expand All @@ -389,6 +391,9 @@ def listener(cls, name: str = MISSING) -> Callable[[FuncT], FuncT]:
name: :class:`str`
The name of the event being listened to. If not provided, it
defaults to the function's name.
once: :class:`bool`
If this listener should only be called once after each cog load.
Defaults to false.

Raises
------
Expand All @@ -411,6 +416,7 @@ def decorator(func: FuncT) -> FuncT:
raise TypeError("Listener function must be a coroutine function.")
actual.__cog_listener__ = True
to_assign = name or actual.__name__
actual._once = once
try:
actual.__cog_listener_names__.append(to_assign)
except AttributeError:
Expand Down