diff options
| author | 2020-09-05 20:08:59 -0700 | |
|---|---|---|
| committer | 2020-09-06 14:46:14 -0700 | |
| commit | b0a2ebb87986856240b5e20967424d035998ac77 (patch) | |
| tree | 3aa2fb89668742c51631a162157f83e8ca190fb2 | |
| parent | Implement the without_role decorator by negating has_any_role (diff) | |
Use has_any_role's predicate directly
In some places, it's more appropriate than using with_role_check since
it will raise CheckFailures. This applies to `cog_check`s and other
things which effectively act as command checks.
| -rw-r--r-- | bot/cogs/dm_relay.py | 6 | ||||
| -rw-r--r-- | bot/cogs/extensions.py | 5 | ||||
| -rw-r--r-- | bot/cogs/filter_lists.py | 7 | ||||
| -rw-r--r-- | bot/cogs/help_channels.py | 7 | ||||
| -rw-r--r-- | bot/cogs/moderation/infractions.py | 5 | ||||
| -rw-r--r-- | bot/cogs/moderation/management.py | 6 | ||||
| -rw-r--r-- | bot/cogs/moderation/silence.py | 5 | ||||
| -rw-r--r-- | bot/cogs/moderation/slowmode.py | 7 | ||||
| -rw-r--r-- | bot/cogs/moderation/superstarify.py | 7 |
9 files changed, 24 insertions, 31 deletions
diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py index 0d8f340b4..7a3fe49bb 100644 --- a/bot/cogs/dm_relay.py +++ b/bot/cogs/dm_relay.py @@ -10,7 +10,7 @@ from bot import constants from bot.bot import Bot from bot.converters import UserMentionOrID from bot.utils import RedisCache -from bot.utils.checks import in_whitelist_check, with_role_check +from bot.utils.checks import in_whitelist_check from bot.utils.messages import send_attachments from bot.utils.webhooks import send_webhook @@ -105,10 +105,10 @@ class DMRelay(Cog): except discord.HTTPException: log.exception("Failed to send an attachment to the webhook") - def cog_check(self, ctx: commands.Context) -> bool: + async def cog_check(self, ctx: commands.Context) -> bool: """Only allow moderators to invoke the commands in this cog.""" checks = [ - with_role_check(ctx, *constants.MODERATION_ROLES), + await commands.has_any_role(*constants.MODERATION_ROLES).predicate(ctx), in_whitelist_check( ctx, channels=[constants.Channels.dm_log], diff --git a/bot/cogs/extensions.py b/bot/cogs/extensions.py index 396e406b0..5977e6f3c 100644 --- a/bot/cogs/extensions.py +++ b/bot/cogs/extensions.py @@ -11,7 +11,6 @@ from discord.ext.commands import Context, group from bot.bot import Bot from bot.constants import Emojis, MODERATION_ROLES, Roles, URLs from bot.pagination import LinePaginator -from bot.utils.checks import with_role_check log = logging.getLogger(__name__) @@ -219,9 +218,9 @@ class Extensions(commands.Cog): return msg, error_msg # This cannot be static (must have a __func__ attribute). - def cog_check(self, ctx: Context) -> bool: + async def cog_check(self, ctx: Context) -> bool: """Only allow moderators and core developers to invoke the commands in this cog.""" - return with_role_check(ctx, *MODERATION_ROLES, Roles.core_developers) + return await commands.has_any_role(*MODERATION_ROLES, Roles.core_developers).predicate(ctx) # This cannot be static (must have a __func__ attribute). async def cog_command_error(self, ctx: Context, error: Exception) -> None: diff --git a/bot/cogs/filter_lists.py b/bot/cogs/filter_lists.py index c15adc461..232c1e48b 100644 --- a/bot/cogs/filter_lists.py +++ b/bot/cogs/filter_lists.py @@ -2,14 +2,13 @@ import logging from typing import Optional from discord import Colour, Embed -from discord.ext.commands import BadArgument, Cog, Context, IDConverter, group +from discord.ext.commands import BadArgument, Cog, Context, IDConverter, group, has_any_role from bot import constants from bot.api import ResponseCodeError from bot.bot import Bot from bot.converters import ValidDiscordServerInvite, ValidFilterListType from bot.pagination import LinePaginator -from bot.utils.checks import with_role_check log = logging.getLogger(__name__) @@ -263,9 +262,9 @@ class FilterLists(Cog): """Syncs both allowlists and denylists with the API.""" await self._sync_data(ctx) - def cog_check(self, ctx: Context) -> bool: + async def cog_check(self, ctx: Context) -> bool: """Only allow moderators to invoke the commands in this cog.""" - return with_role_check(ctx, *constants.MODERATION_ROLES) + return await has_any_role(*constants.MODERATION_ROLES).predicate(ctx) def setup(bot: Bot) -> None: diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index 0f9cac89e..17142071f 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -14,7 +14,6 @@ from discord.ext import commands from bot import constants from bot.bot import Bot from bot.utils import RedisCache -from bot.utils.checks import with_role_check from bot.utils.scheduling import Scheduler log = logging.getLogger(__name__) @@ -196,12 +195,12 @@ class HelpChannels(commands.Cog): return True log.trace(f"{ctx.author} is not the help channel claimant, checking roles.") - role_check = with_role_check(ctx, *constants.HelpChannels.cmd_whitelist) + has_role = await commands.has_any_role(*constants.HelpChannels.cmd_whitelist).predicate(ctx) - if role_check: + if has_role: self.bot.stats.incr("help.dormant_invoke.staff") - return role_check + return has_role @commands.command(name="close", aliases=["dormant", "solved"], enabled=False) async def close_command(self, ctx: commands.Context) -> None: diff --git a/bot/cogs/moderation/infractions.py b/bot/cogs/moderation/infractions.py index 8df642428..8f0def2bc 100644 --- a/bot/cogs/moderation/infractions.py +++ b/bot/cogs/moderation/infractions.py @@ -12,7 +12,6 @@ from bot.bot import Bot from bot.constants import Event from bot.converters import Expiry, FetchedMember from bot.decorators import respect_role_hierarchy -from bot.utils.checks import with_role_check from . import utils from .scheduler import InfractionScheduler from .utils import UserSnowflake @@ -357,9 +356,9 @@ class Infractions(InfractionScheduler, commands.Cog): # endregion # This cannot be static (must have a __func__ attribute). - def cog_check(self, ctx: Context) -> bool: + async def cog_check(self, ctx: Context) -> bool: """Only allow moderators to invoke the commands in this cog.""" - return with_role_check(ctx, *constants.MODERATION_ROLES) + return await commands.has_any_role(*constants.MODERATION_ROLES).predicate(ctx) # This cannot be static (must have a __func__ attribute). async def cog_command_error(self, ctx: Context, error: Exception) -> None: diff --git a/bot/cogs/moderation/management.py b/bot/cogs/moderation/management.py index 672bb0e9c..83342ac90 100644 --- a/bot/cogs/moderation/management.py +++ b/bot/cogs/moderation/management.py @@ -12,7 +12,7 @@ from bot.bot import Bot from bot.converters import Expiry, InfractionSearchQuery, allowed_strings, proxy_user from bot.pagination import LinePaginator from bot.utils import time -from bot.utils.checks import in_whitelist_check, with_role_check +from bot.utils.checks import in_whitelist_check from . import utils from .infractions import Infractions from .modlog import ModLog @@ -282,10 +282,10 @@ class ModManagement(commands.Cog): # endregion # This cannot be static (must have a __func__ attribute). - def cog_check(self, ctx: Context) -> bool: + async def cog_check(self, ctx: Context) -> bool: """Only allow moderators inside moderator channels to invoke the commands in this cog.""" checks = [ - with_role_check(ctx, *constants.MODERATION_ROLES), + await commands.has_any_role(*constants.MODERATION_ROLES).predicate(ctx), in_whitelist_check( ctx, channels=constants.MODERATION_CHANNELS, diff --git a/bot/cogs/moderation/silence.py b/bot/cogs/moderation/silence.py index f8a6592bc..ecc9f8d22 100644 --- a/bot/cogs/moderation/silence.py +++ b/bot/cogs/moderation/silence.py @@ -10,7 +10,6 @@ from discord.ext.commands import Context from bot.bot import Bot from bot.constants import Channels, Emojis, Guild, MODERATION_ROLES, Roles from bot.converters import HushDurationConverter -from bot.utils.checks import with_role_check from bot.utils.scheduling import Scheduler log = logging.getLogger(__name__) @@ -160,6 +159,6 @@ class Silence(commands.Cog): asyncio.create_task(self._mod_alerts_channel.send(message)) # This cannot be static (must have a __func__ attribute). - def cog_check(self, ctx: Context) -> bool: + async def cog_check(self, ctx: Context) -> bool: """Only allow moderators to invoke the commands in this cog.""" - return with_role_check(ctx, *MODERATION_ROLES) + return await commands.has_any_role(*MODERATION_ROLES).predicate(ctx) diff --git a/bot/cogs/moderation/slowmode.py b/bot/cogs/moderation/slowmode.py index 1d055afac..efd862aa5 100644 --- a/bot/cogs/moderation/slowmode.py +++ b/bot/cogs/moderation/slowmode.py @@ -4,12 +4,11 @@ from typing import Optional from dateutil.relativedelta import relativedelta from discord import TextChannel -from discord.ext.commands import Cog, Context, group +from discord.ext.commands import Cog, Context, group, has_any_role from bot.bot import Bot from bot.constants import Emojis, MODERATION_ROLES from bot.converters import DurationDelta -from bot.decorators import with_role_check from bot.utils import time log = logging.getLogger(__name__) @@ -87,9 +86,9 @@ class Slowmode(Cog): f'{Emojis.check_mark} The slowmode delay for {channel.mention} has been reset to 0 seconds.' ) - def cog_check(self, ctx: Context) -> bool: + async def cog_check(self, ctx: Context) -> bool: """Only allow moderators to invoke the commands in this cog.""" - return with_role_check(ctx, *MODERATION_ROLES) + return await has_any_role(*MODERATION_ROLES).predicate(ctx) def setup(bot: Bot) -> None: diff --git a/bot/cogs/moderation/superstarify.py b/bot/cogs/moderation/superstarify.py index 867de815a..081c2d0b9 100644 --- a/bot/cogs/moderation/superstarify.py +++ b/bot/cogs/moderation/superstarify.py @@ -6,12 +6,11 @@ import typing as t from pathlib import Path from discord import Colour, Embed, Member -from discord.ext.commands import Cog, Context, command +from discord.ext.commands import Cog, Context, command, has_any_role from bot import constants from bot.bot import Bot from bot.converters import Expiry -from bot.utils.checks import with_role_check from bot.utils.time import format_infraction from . import utils from .scheduler import InfractionScheduler @@ -234,6 +233,6 @@ class Superstarify(InfractionScheduler, Cog): return rng.choice(STAR_NAMES) # This cannot be static (must have a __func__ attribute). - def cog_check(self, ctx: Context) -> bool: + async def cog_check(self, ctx: Context) -> bool: """Only allow moderators to invoke the commands in this cog.""" - return with_role_check(ctx, *constants.MODERATION_ROLES) + return await has_any_role(*constants.MODERATION_ROLES).predicate(ctx) |