Browse Source

[commands] Force the predicate attribute in checks to be a coroutine

pull/2505/head
Rapptz 5 years ago
parent
commit
e5870903f5
  1. 65
      discord/ext/commands/core.py

65
discord/ext/commands/core.py

@ -1321,12 +1321,13 @@ def check(predicate):
def extended_check(ctx): def extended_check(ctx):
if ctx.guild is None: if ctx.guild is None:
return False return False
return ctx.guild.owner_id == ctx.author.id or original(ctx) return ctx.guild.owner_id == ctx.author.id or await original(ctx)
return commands.check(extended_check) return commands.check(extended_check)
.. note:: .. note::
These functions can either be regular functions or coroutines. The function returned by ``predicate`` is **always** a coroutine,
even if the original function was not a coroutine.
.. versionchanged:: 1.3.0 .. versionchanged:: 1.3.0
The ``predicate`` attribute was added. The ``predicate`` attribute was added.
@ -1377,7 +1378,14 @@ def check(predicate):
return func return func
decorator.predicate = predicate if inspect.iscoroutinefunction(predicate):
decorator.predicate = predicate
else:
@functools.wraps(predicate)
async def wrapper(ctx):
return predicate(ctx)
decorator.predicate = wrapper
return decorator return decorator
def check_any(*checks): def check_any(*checks):
@ -1435,10 +1443,9 @@ def check_any(*checks):
async def predicate(ctx): async def predicate(ctx):
errors = [] errors = []
maybe = discord.utils.maybe_coroutine
for func in unwrapped: for func in unwrapped:
try: try:
value = await maybe(func, ctx) value = await func(ctx)
except CheckFailure as e: except CheckFailure as e:
errors.append(e) errors.append(e)
else: else:
@ -1465,10 +1472,6 @@ def has_role(item):
is missing a role, or :exc:`.NoPrivateMessage` if it is used in a private message. is missing a role, or :exc:`.NoPrivateMessage` if it is used in a private message.
Both inherit from :exc:`.CheckFailure`. Both inherit from :exc:`.CheckFailure`.
.. note::
The ``predicate`` attribute for this function is **not** a coroutine.
.. versionchanged:: 1.1.0 .. versionchanged:: 1.1.0
Raise :exc:`.MissingRole` or :exc:`.NoPrivateMessage` Raise :exc:`.MissingRole` or :exc:`.NoPrivateMessage`
@ -1505,10 +1508,6 @@ def has_any_role(*items):
is missing all roles, or :exc:`.NoPrivateMessage` if it is used in a private message. is missing all roles, or :exc:`.NoPrivateMessage` if it is used in a private message.
Both inherit from :exc:`.CheckFailure`. Both inherit from :exc:`.CheckFailure`.
.. note::
The ``predicate`` attribute for this function is **not** a coroutine.
.. versionchanged:: 1.1.0 .. versionchanged:: 1.1.0
Raise :exc:`.MissingAnyRole` or :exc:`.NoPrivateMessage` Raise :exc:`.MissingAnyRole` or :exc:`.NoPrivateMessage`
@ -1548,10 +1547,6 @@ def bot_has_role(item):
is missing the role, or :exc:`.NoPrivateMessage` if it is used in a private message. is missing the role, or :exc:`.NoPrivateMessage` if it is used in a private message.
Both inherit from :exc:`.CheckFailure`. Both inherit from :exc:`.CheckFailure`.
.. note::
The ``predicate`` attribute for this function is **not** a coroutine.
.. versionchanged:: 1.1.0 .. versionchanged:: 1.1.0
Raise :exc:`.BotMissingRole` or :exc:`.NoPrivateMessage` Raise :exc:`.BotMissingRole` or :exc:`.NoPrivateMessage`
@ -1581,10 +1576,6 @@ def bot_has_any_role(*items):
is missing all roles, or :exc:`.NoPrivateMessage` if it is used in a private message. is missing all roles, or :exc:`.NoPrivateMessage` if it is used in a private message.
Both inherit from :exc:`.CheckFailure`. Both inherit from :exc:`.CheckFailure`.
.. note::
The ``predicate`` attribute for this function is **not** a coroutine.
.. versionchanged:: 1.1.0 .. versionchanged:: 1.1.0
Raise :exc:`.BotMissingAnyRole` or :exc:`.NoPrivateMessage` Raise :exc:`.BotMissingAnyRole` or :exc:`.NoPrivateMessage`
@ -1615,10 +1606,6 @@ def has_permissions(**perms):
This check raises a special exception, :exc:`.MissingPermissions` This check raises a special exception, :exc:`.MissingPermissions`
that is inherited from :exc:`.CheckFailure`. that is inherited from :exc:`.CheckFailure`.
.. note::
The ``predicate`` attribute for this function is **not** a coroutine.
Parameters Parameters
------------ ------------
perms perms
@ -1654,10 +1641,6 @@ def bot_has_permissions(**perms):
This check raises a special exception, :exc:`.BotMissingPermissions` This check raises a special exception, :exc:`.BotMissingPermissions`
that is inherited from :exc:`.CheckFailure`. that is inherited from :exc:`.CheckFailure`.
.. note::
The ``predicate`` attribute for this function is **not** a coroutine.
""" """
def predicate(ctx): def predicate(ctx):
guild = ctx.guild guild = ctx.guild
@ -1680,10 +1663,6 @@ def has_guild_permissions(**perms):
If this check is called in a DM context, it will raise an If this check is called in a DM context, it will raise an
exception, :exc:`.NoPrivateMessage`. exception, :exc:`.NoPrivateMessage`.
.. note::
The ``predicate`` attribute for this function is **not** a coroutine.
.. versionadded:: 1.3.0 .. versionadded:: 1.3.0
""" """
def predicate(ctx): def predicate(ctx):
@ -1704,10 +1683,6 @@ def bot_has_guild_permissions(**perms):
"""Similar to :func:`.has_guild_permissions`, but checks the bot """Similar to :func:`.has_guild_permissions`, but checks the bot
members guild permissions. members guild permissions.
.. note::
The ``predicate`` attribute for this function is **not** a coroutine.
.. versionadded:: 1.3.0 .. versionadded:: 1.3.0
""" """
def predicate(ctx): def predicate(ctx):
@ -1732,10 +1707,6 @@ def dm_only():
This check raises a special exception, :exc:`.PrivateMessageOnly` This check raises a special exception, :exc:`.PrivateMessageOnly`
that is inherited from :exc:`.CheckFailure`. that is inherited from :exc:`.CheckFailure`.
.. note::
The ``predicate`` attribute for this function is **not** a coroutine.
.. versionadded:: 1.1.0 .. versionadded:: 1.1.0
""" """
@ -1753,10 +1724,6 @@ def guild_only():
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`.
.. note::
The ``predicate`` attribute for this function is **not** a coroutine.
""" """
def predicate(ctx): def predicate(ctx):
@ -1774,10 +1741,6 @@ def is_owner():
This check raises a special exception, :exc:`.NotOwner` that is derived This check raises a special exception, :exc:`.NotOwner` that is derived
from :exc:`.CheckFailure`. from :exc:`.CheckFailure`.
.. note::
The ``predicate`` attribute for this function **is** a coroutine.
""" """
async def predicate(ctx): async def predicate(ctx):
@ -1793,10 +1756,6 @@ def is_nsfw():
This check raises a special exception, :exc:`.NSFWChannelRequired` This check raises a special exception, :exc:`.NSFWChannelRequired`
that is derived from :exc:`.CheckFailure`. that is derived from :exc:`.CheckFailure`.
.. note::
The ``predicate`` attribute for this function is **not** a coroutine.
.. versionchanged:: 1.1.0 .. versionchanged:: 1.1.0
Raise :exc:`.NSFWChannelRequired` instead of generic :exc:`.CheckFailure`. Raise :exc:`.NSFWChannelRequired` instead of generic :exc:`.CheckFailure`.

Loading…
Cancel
Save