Skip to content

Commit 979f519

Browse files
Dorukyumpre-commit-ci[bot]Paillat-dev
authored
docs: update docstrings for app command decorators (#3099)
* docs: document app command decorators * hold up * style(pre-commit): auto fixes from pre-commit.com hooks * remove unused import * fix format * Update discord/bot.py Co-authored-by: Paillat <jeremiecotti@ik.me> Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> * use union --------- Signed-off-by: Dorukyum <53639936+Dorukyum@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Paillat <paillat@pycord.dev> Co-authored-by: Paillat <jeremiecotti@ik.me>
1 parent 1088549 commit 979f519

2 files changed

Lines changed: 43 additions & 24 deletions

File tree

discord/bot.py

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
if TYPE_CHECKING:
6969
from .member import Member
7070

71+
C = TypeVar("C", bound=MessageCommand | SlashCommand | UserCommand)
7172
CoroFunc = Callable[..., Coroutine[Any, Any, Any]]
7273
CFT = TypeVar("CFT", bound=CoroFunc)
7374

@@ -909,65 +910,74 @@ async def callback() -> None:
909910
autocomplete_task.cancel()
910911

911912
def slash_command(self, **kwargs):
912-
"""A shortcut decorator that invokes :func:`command` and adds it to
913-
the internal command list via :meth:`add_application_command`.
914-
This shortcut is made specifically for :class:`.SlashCommand`.
913+
"""A shortcut decorator for adding a slash command to the bot.
914+
This is equivalent to using :meth:`application_command`, providing
915+
the :class:`SlashCommand` class.
915916
916917
.. versionadded:: 2.0
917918
918919
Returns
919920
-------
920921
Callable[..., :class:`SlashCommand`]
921-
A decorator that converts the provided method into a :class:`.SlashCommand`, adds it to the bot,
922-
then returns it.
922+
A decorator that converts the provided function into a :class:`.SlashCommand`,
923+
adds it to the bot, and returns it.
923924
"""
924925
return self.application_command(cls=SlashCommand, **kwargs)
925926

926927
def user_command(self, **kwargs):
927-
"""A shortcut decorator that invokes :func:`command` and adds it to
928-
the internal command list via :meth:`add_application_command`.
929-
This shortcut is made specifically for :class:`.UserCommand`.
928+
"""A shortcut decorator for adding a user command to the bot.
929+
This is equivalent to using :meth:`application_command`, providing
930+
the :class:`UserCommand` class.
930931
931932
.. versionadded:: 2.0
932933
933934
Returns
934935
-------
935936
Callable[..., :class:`UserCommand`]
936-
A decorator that converts the provided method into a :class:`.UserCommand`, adds it to the bot,
937-
then returns it.
937+
A decorator that converts the provided function into a :class:`.UserCommand`,
938+
adds it to the bot, and returns it.
938939
"""
939940
return self.application_command(cls=UserCommand, **kwargs)
940941

941942
def message_command(self, **kwargs):
942-
"""A shortcut decorator that invokes :func:`command` and adds it to
943-
the internal command list via :meth:`add_application_command`.
944-
This shortcut is made specifically for :class:`.MessageCommand`.
943+
"""A shortcut decorator for adding a message command to the bot.
944+
This is equivalent to using :meth:`application_command`, providing
945+
the :class:`MessageCommand` class.
945946
946947
.. versionadded:: 2.0
947948
948949
Returns
949950
-------
950951
Callable[..., :class:`MessageCommand`]
951-
A decorator that converts the provided method into a :class:`.MessageCommand`, adds it to the bot,
952-
then returns it.
952+
A decorator that converts the provided function into a :class:`.MessageCommand`,
953+
adds it to the bot, and returns it.
953954
"""
954955
return self.application_command(cls=MessageCommand, **kwargs)
955956

956-
def application_command(self, **kwargs):
957-
"""A shortcut decorator that invokes :func:`command` and adds it to
957+
def application_command(self, cls: type[C] = SlashCommand, **kwargs):
958+
"""A shortcut decorator that converts the provided function into
959+
an application command via :func:`command` and adds it to
958960
the internal command list via :meth:`~.Bot.add_application_command`.
959961
960962
.. versionadded:: 2.0
961963
964+
Parameters
965+
----------
966+
cls: Type[:class:`ApplicationCommand`]
967+
The factory class that will be used to create the command.
968+
By default, this is :class:`.SlashCommand`. Should a custom
969+
class be provided, it must be a subclass of either
970+
:class:`SlashCommand`, :class:`MessageCommand` or :class:`UserCommand`.
971+
962972
Returns
963973
-------
964974
Callable[..., :class:`ApplicationCommand`]
965-
A decorator that converts the provided method into an :class:`.ApplicationCommand`, adds it to the bot,
966-
then returns it.
975+
A decorator that converts the provided function into an :class:`.ApplicationCommand`,
976+
adds it to the bot, and returns it.
967977
"""
968978

969-
def decorator(func) -> ApplicationCommand:
970-
result = command(**kwargs)(func)
979+
def decorator(func) -> C:
980+
result = command(cls=cls, **kwargs)(func)
971981
self.add_application_command(result)
972982
return result
973983

@@ -985,8 +995,8 @@ def command(self, **kwargs):
985995
Returns
986996
-------
987997
Callable[..., :class:`ApplicationCommand`]
988-
A decorator that converts the provided method into an :class:`.ApplicationCommand`, adds it to the bot,
989-
then returns it.
998+
A decorator that converts the provided function into an :class:`.ApplicationCommand`,
999+
adds it to the bot, and returns it.
9901000
"""
9911001
return self.application_command(**kwargs)
9921002

discord/commands/core.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,15 @@ def add_command(self, command: SlashCommand | SlashCommandGroup) -> None:
13991399
def command(
14001400
self, cls: type[T] = SlashCommand, **kwargs
14011401
) -> Callable[[Callable], SlashCommand]:
1402+
"""A shortcut decorator for adding a subcommand to this slash command group.
1403+
1404+
Returns
1405+
-------
1406+
Callable[..., :class:`SlashCommand`]
1407+
A decorator that converts the provided function into a :class:`.SlashCommand`,
1408+
adds it to this group, then returns it.
1409+
"""
1410+
14021411
def wrap(func) -> T:
14031412
command = cls(func, parent=self, **kwargs)
14041413
self.add_command(command)
@@ -1414,7 +1423,7 @@ def create_subgroup(
14141423
**kwargs,
14151424
) -> SlashCommandGroup:
14161425
"""
1417-
Creates a new subgroup for this SlashCommandGroup.
1426+
Creates a new subgroup under this slash command group.
14181427
14191428
Parameters
14201429
----------

0 commit comments

Comments
 (0)