Browse Source

ext.commands: hybrid

pull/10189/head
Soheab_ 2 weeks ago
parent
commit
7e8a254541
  1. 65
      discord/ext/commands/hybrid.py

65
discord/ext/commands/hybrid.py

@ -24,19 +24,7 @@ DEALINGS IN THE SOFTWARE.
from __future__ import annotations from __future__ import annotations
from typing import ( from typing import TYPE_CHECKING, Any, Callable, ClassVar, Dict, List, Tuple, Type, TypeVar, Union, Optional, TypedDict
TYPE_CHECKING,
Any,
Callable,
ClassVar,
Dict,
List,
Tuple,
Type,
TypeVar,
Union,
Optional,
)
import discord import discord
import inspect import inspect
@ -51,7 +39,7 @@ from .cog import Cog
from .view import StringView from .view import StringView
if TYPE_CHECKING: if TYPE_CHECKING:
from typing_extensions import Self, ParamSpec, Concatenate from typing_extensions import Self, ParamSpec, Concatenate, Unpack, NotRequired
from ._types import ContextT, Coro, BotT from ._types import ContextT, Coro, BotT
from .bot import Bot from .bot import Bot
from .context import Context from .context import Context
@ -60,6 +48,31 @@ if TYPE_CHECKING:
AutocompleteCallback, AutocompleteCallback,
ChoiceT, ChoiceT,
) )
from .core import _CommandKwargs
class _HybridCommandKwargs(
_CommandKwargs,
):
guild_ids: NotRequired[Optional[List[int]]]
guild_only: NotRequired[bool]
default_permissions: NotRequired[Optional[bool]]
nsfw: NotRequired[bool]
with_app_command: NotRequired[bool]
class _HybridCommandDecoratorKwargs(_HybridCommandKwargs):
description: NotRequired[Union[str, app_commands.locale_str]]
class _HybridGroupKwargs(_HybridCommandDecoratorKwargs):
with_app_command: NotRequired[bool]
guild_ids: NotRequired[Optional[List[int]]]
guild_only: NotRequired[bool]
default_permissions: NotRequired[Optional[bool]]
nsfw: NotRequired[bool]
description: NotRequired[str]
class _HybridGroupDecoratorKwargs(_HybridGroupKwargs):
description: NotRequired[Union[str, app_commands.locale_str]]
fallback: NotRequired[Union[str, app_commands.locale_str]]
__all__ = ( __all__ = (
@ -501,7 +514,7 @@ class HybridCommand(Command[CogT, P, T]):
*, *,
name: Union[str, app_commands.locale_str] = MISSING, name: Union[str, app_commands.locale_str] = MISSING,
description: Union[str, app_commands.locale_str] = MISSING, description: Union[str, app_commands.locale_str] = MISSING,
**kwargs: Any, **kwargs: Unpack[_HybridCommandKwargs], # type: ignore # name, description
) -> None: ) -> None:
name, name_locale = (name.message, name) if isinstance(name, app_commands.locale_str) else (name, None) name, name_locale = (name.message, name) if isinstance(name, app_commands.locale_str) else (name, None)
if name is not MISSING: if name is not MISSING:
@ -621,7 +634,7 @@ class HybridGroup(Group[CogT, P, T]):
name: Union[str, app_commands.locale_str] = MISSING, name: Union[str, app_commands.locale_str] = MISSING,
description: Union[str, app_commands.locale_str] = MISSING, description: Union[str, app_commands.locale_str] = MISSING,
fallback: Optional[Union[str, app_commands.locale_str]] = None, fallback: Optional[Union[str, app_commands.locale_str]] = None,
**attrs: Any, **attrs: Unpack[_HybridGroupKwargs], # type: ignore # name, description
) -> None: ) -> None:
name, name_locale = (name.message, name) if isinstance(name, app_commands.locale_str) else (name, None) name, name_locale = (name.message, name) if isinstance(name, app_commands.locale_str) else (name, None)
if name is not MISSING: if name is not MISSING:
@ -825,7 +838,7 @@ class HybridGroup(Group[CogT, P, T]):
name: Union[str, app_commands.locale_str] = MISSING, name: Union[str, app_commands.locale_str] = MISSING,
*args: Any, *args: Any,
with_app_command: bool = True, with_app_command: bool = True,
**kwargs: Any, **kwargs: Unpack[_HybridCommandDecoratorKwargs], # type: ignore # name, with_app_command
) -> Callable[[CommandCallback[CogT, ContextT, P2, U]], HybridCommand[CogT, P2, U]]: ) -> Callable[[CommandCallback[CogT, ContextT, P2, U]], HybridCommand[CogT, P2, U]]:
"""A shortcut decorator that invokes :func:`~discord.ext.commands.hybrid_command` and adds it to """A shortcut decorator that invokes :func:`~discord.ext.commands.hybrid_command` and adds it to
the internal command list via :meth:`add_command`. the internal command list via :meth:`add_command`.
@ -837,8 +850,8 @@ class HybridGroup(Group[CogT, P, T]):
""" """
def decorator(func: CommandCallback[CogT, ContextT, P2, U]): def decorator(func: CommandCallback[CogT, ContextT, P2, U]):
kwargs.setdefault('parent', self) kwargs.setdefault('parent', self) # type: ignore # parent is not for users to set
result = hybrid_command(name=name, *args, with_app_command=with_app_command, **kwargs)(func) result = hybrid_command(name=name, *args, with_app_command=with_app_command, **kwargs)(func) # type: ignore # name, with_app_command
self.add_command(result) self.add_command(result)
return result return result
@ -849,7 +862,7 @@ class HybridGroup(Group[CogT, P, T]):
name: Union[str, app_commands.locale_str] = MISSING, name: Union[str, app_commands.locale_str] = MISSING,
*args: Any, *args: Any,
with_app_command: bool = True, with_app_command: bool = True,
**kwargs: Any, **kwargs: Unpack[_HybridGroupDecoratorKwargs], # type: ignore # name, with_app_command
) -> Callable[[CommandCallback[CogT, ContextT, P2, U]], HybridGroup[CogT, P2, U]]: ) -> Callable[[CommandCallback[CogT, ContextT, P2, U]], HybridGroup[CogT, P2, U]]:
"""A shortcut decorator that invokes :func:`~discord.ext.commands.hybrid_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`.
@ -861,8 +874,8 @@ class HybridGroup(Group[CogT, P, T]):
""" """
def decorator(func: CommandCallback[CogT, ContextT, P2, U]): def decorator(func: CommandCallback[CogT, ContextT, P2, U]):
kwargs.setdefault('parent', self) kwargs.setdefault('parent', self) # type: ignore # parent is not for users to set
result = hybrid_group(name=name, *args, with_app_command=with_app_command, **kwargs)(func) result = hybrid_group(name=name, *args, with_app_command=with_app_command, **kwargs)(func) # type: ignore # name, with_app_command
self.add_command(result) self.add_command(result)
return result return result
@ -873,7 +886,7 @@ def hybrid_command(
name: Union[str, app_commands.locale_str] = MISSING, name: Union[str, app_commands.locale_str] = MISSING,
*, *,
with_app_command: bool = True, with_app_command: bool = True,
**attrs: Any, **attrs: Unpack[_HybridCommandDecoratorKwargs], # type: ignore # name, with_app_command
) -> Callable[[CommandCallback[CogT, ContextT, P, T]], HybridCommand[CogT, P, T]]: ) -> Callable[[CommandCallback[CogT, ContextT, P, T]], HybridCommand[CogT, P, T]]:
r"""A decorator that transforms a function into a :class:`.HybridCommand`. r"""A decorator that transforms a function into a :class:`.HybridCommand`.
@ -916,7 +929,7 @@ def hybrid_command(
if isinstance(func, Command): if isinstance(func, Command):
raise TypeError('Callback is already a command.') raise TypeError('Callback is already a command.')
# Pyright does not allow Command[Any] to be assigned to Command[CogT] despite it being okay here # Pyright does not allow Command[Any] to be assigned to Command[CogT] despite it being okay here
return HybridCommand(func, name=name, with_app_command=with_app_command, **attrs) # type: ignore return HybridCommand(func, name=name, with_app_command=with_app_command, **attrs) # type: ignore # name, with_app_command
return decorator return decorator
@ -925,7 +938,7 @@ def hybrid_group(
name: Union[str, app_commands.locale_str] = MISSING, name: Union[str, app_commands.locale_str] = MISSING,
*, *,
with_app_command: bool = True, with_app_command: bool = True,
**attrs: Any, **attrs: Unpack[_HybridGroupDecoratorKwargs], # type: ignore # name, with_app_command
) -> Callable[[CommandCallback[CogT, ContextT, P, T]], HybridGroup[CogT, P, T]]: ) -> Callable[[CommandCallback[CogT, ContextT, P, T]], HybridGroup[CogT, P, T]]:
"""A decorator that transforms a function into a :class:`.HybridGroup`. """A decorator that transforms a function into a :class:`.HybridGroup`.
@ -949,6 +962,6 @@ def hybrid_group(
def decorator(func: CommandCallback[CogT, ContextT, P, T]) -> HybridGroup[CogT, P, T]: def decorator(func: CommandCallback[CogT, ContextT, P, T]) -> HybridGroup[CogT, P, T]:
if isinstance(func, Command): if isinstance(func, Command):
raise TypeError('Callback is already a command.') raise TypeError('Callback is already a command.')
return HybridGroup(func, name=name, with_app_command=with_app_command, **attrs) return HybridGroup(func, name=name, with_app_command=with_app_command, **attrs) # type: ignore # name, with_app_command
return decorator return decorator

Loading…
Cancel
Save