mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-07-13 21:35:30 +00:00
Refactor ContextMenu constructor to allow app command type inferring
This commit is contained in:
parent
cda1f0a4ef
commit
985f5732c0
@ -718,7 +718,8 @@ class ContextMenu:
|
|||||||
name: :class:`str`
|
name: :class:`str`
|
||||||
The name of the context menu.
|
The name of the context menu.
|
||||||
type: :class:`.AppCommandType`
|
type: :class:`.AppCommandType`
|
||||||
The type of context menu application command.
|
The type of context menu application command. By default, this is inferred
|
||||||
|
by the parameter of the callback.
|
||||||
checks
|
checks
|
||||||
A list of predicates that take a :class:`~discord.Interaction` parameter
|
A list of predicates that take a :class:`~discord.Interaction` parameter
|
||||||
to indicate whether the command callback should be executed. If an exception
|
to indicate whether the command callback should be executed. If an exception
|
||||||
@ -732,15 +733,19 @@ class ContextMenu:
|
|||||||
*,
|
*,
|
||||||
name: str,
|
name: str,
|
||||||
callback: ContextMenuCallback,
|
callback: ContextMenuCallback,
|
||||||
type: AppCommandType,
|
type: AppCommandType = MISSING,
|
||||||
guild_ids: Optional[List[int]] = None,
|
guild_ids: Optional[List[int]] = None,
|
||||||
):
|
):
|
||||||
self.name: str = validate_context_menu_name(name)
|
self.name: str = validate_context_menu_name(name)
|
||||||
self._callback: ContextMenuCallback = callback
|
self._callback: ContextMenuCallback = callback
|
||||||
self.type: AppCommandType = type
|
|
||||||
(param, annotation, actual_type) = _get_context_menu_parameter(callback)
|
(param, annotation, actual_type) = _get_context_menu_parameter(callback)
|
||||||
|
if type is MISSING:
|
||||||
|
type = actual_type
|
||||||
|
|
||||||
if actual_type != type:
|
if actual_type != type:
|
||||||
raise ValueError(f'context menu callback implies a type of {actual_type} but {type} was passed.')
|
raise ValueError(f'context menu callback implies a type of {actual_type} but {type} was passed.')
|
||||||
|
|
||||||
|
self.type: AppCommandType = type
|
||||||
self._param_name = param
|
self._param_name = param
|
||||||
self._annotation = annotation
|
self._annotation = annotation
|
||||||
self.module: Optional[str] = callback.__module__
|
self.module: Optional[str] = callback.__module__
|
||||||
@ -753,22 +758,6 @@ class ContextMenu:
|
|||||||
""":ref:`coroutine <coroutine>`: The coroutine that is executed when the context menu is called."""
|
""":ref:`coroutine <coroutine>`: The coroutine that is executed when the context menu is called."""
|
||||||
return self._callback
|
return self._callback
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _from_decorator(cls, callback: ContextMenuCallback, *, name: str = MISSING) -> ContextMenu:
|
|
||||||
(param, annotation, type) = _get_context_menu_parameter(callback)
|
|
||||||
|
|
||||||
self = cls.__new__(cls)
|
|
||||||
self.name = callback.__name__.title() if name is MISSING else name
|
|
||||||
self._callback = callback
|
|
||||||
self.type = type
|
|
||||||
self._param_name = param
|
|
||||||
self._annotation = annotation
|
|
||||||
self.module = callback.__module__
|
|
||||||
self._guild_ids = None
|
|
||||||
self.on_error = None
|
|
||||||
self.checks = getattr(callback, '__discord_app_commands_checks__', [])
|
|
||||||
return self
|
|
||||||
|
|
||||||
def to_dict(self) -> Dict[str, Any]:
|
def to_dict(self) -> Dict[str, Any]:
|
||||||
return {
|
return {
|
||||||
'name': self.name,
|
'name': self.name,
|
||||||
@ -1248,7 +1237,8 @@ def context_menu(*, name: str = MISSING) -> Callable[[ContextMenuCallback], Cont
|
|||||||
if not inspect.iscoroutinefunction(func):
|
if not inspect.iscoroutinefunction(func):
|
||||||
raise TypeError('context menu function must be a coroutine function')
|
raise TypeError('context menu function must be a coroutine function')
|
||||||
|
|
||||||
return ContextMenu._from_decorator(func, name=name)
|
actual_name = func.__name__.title() if name is MISSING else name
|
||||||
|
return ContextMenu(name=actual_name, callback=func)
|
||||||
|
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
@ -826,7 +826,8 @@ class CommandTree(Generic[ClientT]):
|
|||||||
if not inspect.iscoroutinefunction(func):
|
if not inspect.iscoroutinefunction(func):
|
||||||
raise TypeError('context menu function must be a coroutine function')
|
raise TypeError('context menu function must be a coroutine function')
|
||||||
|
|
||||||
context_menu = ContextMenu._from_decorator(func, name=name)
|
actual_name = func.__name__.title() if name is MISSING else name
|
||||||
|
context_menu = ContextMenu(name=actual_name, callback=func)
|
||||||
self.add_command(context_menu, guild=guild, guilds=guilds)
|
self.add_command(context_menu, guild=guild, guilds=guilds)
|
||||||
return context_menu
|
return context_menu
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user