From 5e93396e1655715187d176d1cfdd32aa54616a1a Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Sat, 16 Jan 2021 18:03:38 +0100 Subject: [PATCH 1/3] Add an allow_moderation_roles argument to the wait_for_deletion() util The `allow_moderation_roles` bool can be specified to allow anyone with a role in `MODERATION_ROLES` to delete the message. --- bot/utils/messages.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bot/utils/messages.py b/bot/utils/messages.py index 42bde358d0..b0b6cbf828 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -11,7 +11,7 @@ from discord.ext.commands import Context import bot -from bot.constants import Emojis, NEGATIVE_REPLIES +from bot.constants import Emojis, NEGATIVE_REPLIES, MODERATION_ROLES log = logging.getLogger(__name__) @@ -22,12 +22,15 @@ async def wait_for_deletion( deletion_emojis: Sequence[str] = (Emojis.trashcan,), timeout: float = 60 * 5, attach_emojis: bool = True, + allow_moderation_roles: bool = True ) -> None: """ Wait for up to `timeout` seconds for a reaction by any of the specified `user_ids` to delete the message. An `attach_emojis` bool may be specified to determine whether to attach the given `deletion_emojis` to the message in the given `context`. + An `allow_moderation_roles` bool may also be specified to allow anyone with a role in `MODERATION_ROLES` to delete + the message. """ if message.guild is None: raise ValueError("Message must be sent on a guild") @@ -46,6 +49,7 @@ def check(reaction: discord.Reaction, user: discord.Member) -> bool: reaction.message.id == message.id and str(reaction.emoji) in deletion_emojis and user.id in user_ids + or allow_moderation_roles and any(role.id in MODERATION_ROLES for role in user.roles) ) with contextlib.suppress(asyncio.TimeoutError): From eb78b9c261ecbcea4b2ca5bb0d423f7b3bfb9a0f Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Sat, 16 Jan 2021 18:55:26 +0100 Subject: [PATCH 2/3] Restrict paginator usage to the author and moderators --- bot/pagination.py | 13 ++++++++++--- bot/utils/messages.py | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/bot/pagination.py b/bot/pagination.py index 182b2fa765..09dbad7b5e 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -8,6 +8,7 @@ from discord.ext.commands import Context, Paginator from bot import constants +from bot.constants import MODERATION_ROLES FIRST_EMOJI = "\u23EE" # [:track_previous:] LEFT_EMOJI = "\u2B05" # [:arrow_left:] @@ -210,6 +211,9 @@ async def paginate( Pagination will also be removed automatically if no reaction is added for five minutes (300 seconds). + The interaction will be limited to `restrict_to_user` (ctx.author by default) or + to any user with a moderation role. + Example: >>> embed = discord.Embed() >>> embed.set_author(name="Some Operation", url=url, icon_url=icon) @@ -218,10 +222,10 @@ async def paginate( def event_check(reaction_: discord.Reaction, user_: discord.Member) -> bool: """Make sure that this reaction is what we want to operate on.""" no_restrictions = ( - # Pagination is not restricted - not restrict_to_user # The reaction was by a whitelisted user - or user_.id == restrict_to_user.id + user_.id == restrict_to_user.id + # The reaction was by a moderator + or any(role.id in MODERATION_ROLES for role in user_.roles) ) return ( @@ -242,6 +246,9 @@ def event_check(reaction_: discord.Reaction, user_: discord.Member) -> bool: scale_to_size=scale_to_size) current_page = 0 + if not restrict_to_user: + restrict_to_user = ctx.author + if not lines: if exception_on_empty_embed: log.exception("Pagination asked for empty lines iterable") diff --git a/bot/utils/messages.py b/bot/utils/messages.py index b0b6cbf828..832ad4d554 100644 --- a/bot/utils/messages.py +++ b/bot/utils/messages.py @@ -11,7 +11,7 @@ from discord.ext.commands import Context import bot -from bot.constants import Emojis, NEGATIVE_REPLIES, MODERATION_ROLES +from bot.constants import Emojis, MODERATION_ROLES, NEGATIVE_REPLIES log = logging.getLogger(__name__) From 46ff6949ac583fb705c52431297e6c7a47ad231b Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Sun, 24 Jan 2021 17:39:36 +0100 Subject: [PATCH 3/3] Make sure that the paginator doesn't choke on DMs --- bot/pagination.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bot/pagination.py b/bot/pagination.py index 09dbad7b5e..3b16cc9ffd 100644 --- a/bot/pagination.py +++ b/bot/pagination.py @@ -4,6 +4,7 @@ from contextlib import suppress import discord +from discord import Member from discord.abc import User from discord.ext.commands import Context, Paginator @@ -225,7 +226,7 @@ def event_check(reaction_: discord.Reaction, user_: discord.Member) -> bool: # The reaction was by a whitelisted user user_.id == restrict_to_user.id # The reaction was by a moderator - or any(role.id in MODERATION_ROLES for role in user_.roles) + or isinstance(user_, Member) and any(role.id in MODERATION_ROLES for role in user_.roles) ) return (