Skip to content
Open
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
155 changes: 148 additions & 7 deletions discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@
from .utils import MISSING, async_all, find, get

if TYPE_CHECKING:
from typing_extensions import Never

from .cog import Cog
from .commands import Option
from .ext.commands import Cooldown
from .member import Member
from .permissions import Permissions

C = TypeVar("C", bound=MessageCommand | SlashCommand | UserCommand)
CoroFunc = Callable[..., Coroutine[Any, Any, Any]]
Expand Down Expand Up @@ -909,7 +915,26 @@ async def callback() -> None:
if not autocomplete_task.done():
autocomplete_task.cancel()

def slash_command(self, **kwargs):
def slash_command(
self,
*,
checks: list[Callable[[ApplicationContext], bool]] | None = MISSING,
cog: Cog | None = MISSING,
contexts: set[InteractionContextType] | None = MISSING,
cooldown: Cooldown | None = MISSING,
default_member_permissions: Permissions | None = MISSING,
description: str | None = MISSING,
description_localizations: dict[str, str] | None = MISSING,
guild_ids: list[int] | None = MISSING,
guild_only: bool | None = MISSING,
integration_types: set[IntegrationType] | None = MISSING,
name: str | None = MISSING,
name_localizations: dict[str, str] | None = MISSING,
nsfw: bool | None = MISSING,
options: list[Option] | None = MISSING,
parent: SlashCommandGroup | None = MISSING,
**kwargs: Never,
) -> Callable[..., SlashCommand]:
"""A shortcut decorator for adding a slash command to the bot.
This is equivalent to using :meth:`application_command`, providing
the :class:`SlashCommand` class.
Expand All @@ -922,9 +947,42 @@ def slash_command(self, **kwargs):
A decorator that converts the provided function into a :class:`.SlashCommand`,
adds it to the bot, and returns it.
"""
return self.application_command(cls=SlashCommand, **kwargs)
return self.application_command(
cls=SlashCommand,
checks=checks,
cog=cog,
contexts=contexts,
cooldown=cooldown,
default_member_permissions=default_member_permissions,
description=description,
description_localizations=description_localizations,
guild_ids=guild_ids,
guild_only=guild_only,
integration_types=integration_types,
name=name,
name_localizations=name_localizations,
nsfw=nsfw,
options=options,
parent=parent,
**kwargs,
)

def user_command(self, **kwargs):
def user_command(
self,
*,
checks: list[Callable[[ApplicationContext], bool]] | None = MISSING,
cog: Cog | None = MISSING,
contexts: set[InteractionContextType] | None = MISSING,
cooldown: Cooldown | None = MISSING,
default_member_permissions: Permissions | None = MISSING,
guild_ids: list[int] | None = MISSING,
guild_only: bool | None = MISSING,
integration_types: set[IntegrationType] | None = MISSING,
name: str | None = MISSING,
name_localizations: dict[str, str] | None = MISSING,
nsfw: bool | None = MISSING,
**kwargs: Never,
) -> Callable[..., UserCommand]:
"""A shortcut decorator for adding a user command to the bot.
This is equivalent to using :meth:`application_command`, providing
the :class:`UserCommand` class.
Expand All @@ -937,9 +995,38 @@ def user_command(self, **kwargs):
A decorator that converts the provided function into a :class:`.UserCommand`,
adds it to the bot, and returns it.
"""
return self.application_command(cls=UserCommand, **kwargs)
return self.application_command(
cls=UserCommand,
checks=checks,
cog=cog,
contexts=contexts,
cooldown=cooldown,
default_member_permissions=default_member_permissions,
guild_ids=guild_ids,
guild_only=guild_only,
integration_types=integration_types,
name=name,
name_localizations=name_localizations,
nsfw=nsfw,
**kwargs,
)

def message_command(self, **kwargs):
def message_command(
self,
*,
checks: list[Callable[[ApplicationContext], bool]] | None = MISSING,
cog: Cog | None = MISSING,
contexts: set[InteractionContextType] | None = MISSING,
cooldown: Cooldown | None = MISSING,
default_member_permissions: Permissions | None = MISSING,
guild_ids: list[int] | None = MISSING,
guild_only: bool | None = MISSING,
integration_types: set[IntegrationType] | None = MISSING,
name: str | None = MISSING,
name_localizations: dict[str, str] | None = MISSING,
nsfw: bool | None = MISSING,
**kwargs: Never,
) -> Callable[..., MessageCommand]:
"""A shortcut decorator for adding a message command to the bot.
This is equivalent to using :meth:`application_command`, providing
the :class:`MessageCommand` class.
Expand All @@ -952,9 +1039,43 @@ def message_command(self, **kwargs):
A decorator that converts the provided function into a :class:`.MessageCommand`,
adds it to the bot, and returns it.
"""
return self.application_command(cls=MessageCommand, **kwargs)
return self.application_command(
cls=MessageCommand,
checks=checks,
cog=cog,
contexts=contexts,
cooldown=cooldown,
default_member_permissions=default_member_permissions,
guild_ids=guild_ids,
guild_only=guild_only,
integration_types=integration_types,
name=name,
name_localizations=name_localizations,
nsfw=nsfw,
**kwargs,
)

def application_command(self, cls: type[C] = SlashCommand, **kwargs):
def application_command(
self,
*,
cls: type[C] = SlashCommand,
checks: list[Callable[[ApplicationContext], bool]] | None = MISSING,
cog: Cog | None = MISSING,
contexts: set[InteractionContextType] | None = MISSING,
cooldown: Cooldown | None = MISSING,
default_member_permissions: Permissions | None = MISSING,
description: str | None = MISSING,
description_localizations: dict[str, str] | None = MISSING,
guild_ids: list[int] | None = MISSING,
guild_only: bool | None = MISSING,
integration_types: set[IntegrationType] | None = MISSING,
name: str | None = MISSING,
name_localizations: dict[str, str] | None = MISSING,
nsfw: bool | None = MISSING,
options: list[Option] | None = MISSING,
parent: SlashCommandGroup | None = MISSING,
**kwargs: Any,
) -> Callable[..., C]:
"""A shortcut decorator that converts the provided function into
an application command via :func:`command` and adds it to
the internal command list via :meth:`~.Bot.add_application_command`.
Expand All @@ -976,6 +1097,26 @@ class be provided, it must be a subclass of either
adds it to the bot, and returns it.
"""

params = {
"checks": checks,
"cog": cog,
"contexts": contexts,
"cooldown": cooldown,
"default_member_permissions": default_member_permissions,
"description": description,
"description_localizations": description_localizations,
"guild_ids": guild_ids,
"guild_only": guild_only,
"integration_types": integration_types,
"name": name,
"name_localizations": name_localizations,
"nsfw": nsfw,
"options": options,
"parent": parent,
**kwargs,
}
kwargs = {k: v for k, v in params.items() if v is not MISSING}

def decorator(func) -> C:
result = command(cls=cls, **kwargs)(func)
self.add_application_command(result)
Expand Down
Loading
Loading