mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-07-11 04:17:58 +00:00
[commands] Change guild_only check to set guild_only in hybrid commands
This commit is contained in:
parent
e2d0193531
commit
a47396b21e
@ -2280,14 +2280,46 @@ def guild_only() -> Check[Any]:
|
|||||||
|
|
||||||
This check raises a special exception, :exc:`.NoPrivateMessage`
|
This check raises a special exception, :exc:`.NoPrivateMessage`
|
||||||
that is inherited from :exc:`.CheckFailure`.
|
that is inherited from :exc:`.CheckFailure`.
|
||||||
|
|
||||||
|
If used on hybrid commands, this will be equivalent to the
|
||||||
|
:func:`discord.app_commands.guild_only` decorator.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
# Due to implementation quirks, this check has to be re-implemented completely
|
||||||
|
# to work with both app_commands and the command framework.
|
||||||
|
|
||||||
def predicate(ctx: Context[BotT]) -> bool:
|
def predicate(ctx: Context[BotT]) -> bool:
|
||||||
if ctx.guild is None:
|
if ctx.guild is None:
|
||||||
raise NoPrivateMessage()
|
raise NoPrivateMessage()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return check(predicate)
|
def decorator(func: Union[Command, CoroFunc]) -> Union[Command, CoroFunc]:
|
||||||
|
if isinstance(func, Command):
|
||||||
|
func.checks.append(predicate)
|
||||||
|
if hasattr(func, '__commands_is_hybrid__'):
|
||||||
|
app_command = getattr(func, 'app_command', None)
|
||||||
|
if app_command:
|
||||||
|
app_command.guild_only = True
|
||||||
|
else:
|
||||||
|
if not hasattr(func, '__commands_checks__'):
|
||||||
|
func.__commands_checks__ = []
|
||||||
|
|
||||||
|
func.__commands_checks__.append(predicate)
|
||||||
|
func.__discord_app_commands_guild_only__ = True
|
||||||
|
|
||||||
|
return func
|
||||||
|
|
||||||
|
if inspect.iscoroutinefunction(predicate):
|
||||||
|
decorator.predicate = predicate
|
||||||
|
else:
|
||||||
|
|
||||||
|
@functools.wraps(predicate)
|
||||||
|
async def wrapper(ctx: Context[BotT]):
|
||||||
|
return predicate(ctx)
|
||||||
|
|
||||||
|
decorator.predicate = wrapper
|
||||||
|
|
||||||
|
return decorator # type: ignore
|
||||||
|
|
||||||
|
|
||||||
def is_owner() -> Check[Any]:
|
def is_owner() -> Check[Any]:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user