mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-06-07 20:28:38 +00:00
[commands] Add Bot.hybrid_group and Bot.hybrid_command decorators
This commit is contained in:
parent
0fa9bfc925
commit
242d3f7ab7
@ -60,6 +60,7 @@ from .context import Context
|
|||||||
from . import errors
|
from . import errors
|
||||||
from .help import HelpCommand, DefaultHelpCommand
|
from .help import HelpCommand, DefaultHelpCommand
|
||||||
from .cog import Cog
|
from .cog import Cog
|
||||||
|
from .hybrid import hybrid_command, hybrid_group, HybridCommand, HybridGroup
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from typing_extensions import Self
|
from typing_extensions import Self
|
||||||
@ -78,6 +79,7 @@ if TYPE_CHECKING:
|
|||||||
MaybeAwaitableFunc,
|
MaybeAwaitableFunc,
|
||||||
)
|
)
|
||||||
from .core import Command
|
from .core import Command
|
||||||
|
from .hybrid import CommandCallback, ContextT, P
|
||||||
|
|
||||||
_Prefix = Union[Iterable[str], str]
|
_Prefix = Union[Iterable[str], str]
|
||||||
_PrefixCallable = MaybeAwaitableFunc[[BotT, Message], _Prefix]
|
_PrefixCallable = MaybeAwaitableFunc[[BotT, Message], _Prefix]
|
||||||
@ -247,6 +249,52 @@ class BotBase(GroupMixin[None]):
|
|||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
|
def hybrid_command(
|
||||||
|
self,
|
||||||
|
name: str = MISSING,
|
||||||
|
*args: Any,
|
||||||
|
**kwargs: Any,
|
||||||
|
) -> Callable[[CommandCallback[Any, ContextT, P, T]], HybridCommand[Any, P, T]]:
|
||||||
|
"""A shortcut decorator that invokes :func:`~discord.ext.commands.hybrid_command` and adds it to
|
||||||
|
the internal command list via :meth:`add_command`.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
--------
|
||||||
|
Callable[..., :class:`HybridCommand`]
|
||||||
|
A decorator that converts the provided method into a Command, adds it to the bot, then returns it.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def decorator(func: CommandCallback[Any, ContextT, P, T]):
|
||||||
|
kwargs.setdefault('parent', self)
|
||||||
|
result = hybrid_command(name=name, *args, **kwargs)(func)
|
||||||
|
self.add_command(result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
|
def hybrid_group(
|
||||||
|
self,
|
||||||
|
name: str = MISSING,
|
||||||
|
*args: Any,
|
||||||
|
**kwargs: Any,
|
||||||
|
) -> Callable[[CommandCallback[Any, ContextT, P, T]], HybridGroup[Any, P, T]]:
|
||||||
|
"""A shortcut decorator that invokes :func:`~discord.ext.commands.hybrid_group` and adds it to
|
||||||
|
the internal command list via :meth:`add_command`.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
--------
|
||||||
|
Callable[..., :class:`HybridGroup`]
|
||||||
|
A decorator that converts the provided method into a Group, adds it to the bot, then returns it.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def decorator(func: CommandCallback[Any, ContextT, P, T]):
|
||||||
|
kwargs.setdefault('parent', self)
|
||||||
|
result = hybrid_group(name=name, *args, **kwargs)(func)
|
||||||
|
self.add_command(result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
return decorator
|
||||||
|
|
||||||
# Error handler
|
# Error handler
|
||||||
|
|
||||||
async def on_command_error(self, context: Context[BotT], exception: errors.CommandError, /) -> None:
|
async def on_command_error(self, context: Context[BotT], exception: errors.CommandError, /) -> None:
|
||||||
|
@ -510,7 +510,7 @@ class HybridGroup(Group[CogT, P, T]):
|
|||||||
|
|
||||||
Returns
|
Returns
|
||||||
--------
|
--------
|
||||||
Callable[..., :class:`Command`]
|
Callable[..., :class:`HybridCommand`]
|
||||||
A decorator that converts the provided method into a Command, adds it to the bot, then returns it.
|
A decorator that converts the provided method into a Command, adds it to the bot, then returns it.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -528,12 +528,12 @@ class HybridGroup(Group[CogT, P, T]):
|
|||||||
*args: Any,
|
*args: Any,
|
||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
) -> Callable[[CommandCallback[CogT, ContextT, P2, T]], HybridGroup[CogT, P2, T]]:
|
) -> Callable[[CommandCallback[CogT, ContextT, P2, T]], HybridGroup[CogT, P2, T]]:
|
||||||
"""A shortcut decorator that invokes :func:`.group` and adds it to
|
"""A shortcut decorator that invokes :func:`~discord.ext.commands.hybrid_group` and adds it to
|
||||||
the internal command list via :meth:`~.GroupMixin.add_command`.
|
the internal command list via :meth:`~.GroupMixin.add_command`.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
--------
|
--------
|
||||||
Callable[..., :class:`Group`]
|
Callable[..., :class:`HybridGroup`]
|
||||||
A decorator that converts the provided method into a Group, adds it to the bot, then returns it.
|
A decorator that converts the provided method into a Group, adds it to the bot, then returns it.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user