From c2ed1f341c3d47f4d6cd4b383cc75cf5ea68069d Mon Sep 17 00:00:00 2001 From: japandotorg Date: Tue, 16 Jul 2024 12:30:27 +0530 Subject: [PATCH] [Purge] add checks to all commands --- README.md | 2 +- purge/core.py | 42 ++++++++++++++++++++++++++++++++++++++---- purge/utils.py | 4 ++-- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index acf7e4a..1e26447 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ To add the cogs to your instance please do: `[p]repo add Seina-Cogs https://gith | RoleUtils | 1.4.0 |
Reaction roles, massroling, & role targetingReaction roles, massrolin & role targeting
| | Lock | 1.1.5 |
Lock channelsLock channels or the whole server
| | DisboardReminder | 1.3.7 |
Set a reminder to bump on disboardSet a reminder to bump on disboard
| -| Purge | 0.1.2 |
Advanced Purge/Cleanup.Purge (deleted) messages that meet a criteria.
| +| Purge | 0.1.3 |
Advanced Purge/Cleanup.Purge (deleted) messages that meet a criteria.
| | FreeloaderMode | 0.1.0 |
Ban users that leave your server right after an event or something.Ban freeloaders who leave your server right after an event or something.
| | ErrorHandler | 0.1.0 |
Allow custom error message.Adds ability to replace the output of the bots error handler when CommandInvokeError is raised, all other errors get handled by the old handler.
| | VoiceNoteLog | 0.1.0 |
Log the voice note sent by your server members.Log voice notes sent by your server members.
| diff --git a/purge/core.py b/purge/core.py index 743f6ec..31763a8 100644 --- a/purge/core.py +++ b/purge/core.py @@ -35,7 +35,7 @@ class Purge(commands.Cog): __doc__ = CleanupCog.__doc__ __author__: Final[List[str]] = ["inthedark.org"] - __version__: Final[str] = "0.1.2" + __version__: Final[str] = "0.1.3" def __init__(self, bot: Red) -> None: super().__init__() @@ -114,7 +114,7 @@ async def _purge( **Example:** - `[p]purge 10` - - `[p] purge 2000` + - `[p]purge 2000` """ if ctx.invoked_subcommand is None: @@ -123,6 +123,8 @@ def check(message: discord.Message) -> bool: await _cleanup(ctx, number, check, channel=channel) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="embeds", aliases=["embed"]) # type: ignore async def _embeds( self, @@ -145,6 +147,8 @@ async def _embeds( """ await _cleanup(ctx, number, lambda e: len(e.embeds), channel=channel) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="regex") # type: ignore async def _regex( self, @@ -177,6 +181,8 @@ def check(message: discord.Message) -> bool: await _cleanup(ctx, number, check, channel=channel) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="files", aliases=["file"]) # type: ignore async def _files( self, @@ -199,6 +205,8 @@ async def _files( """ await _cleanup(ctx, number, lambda e: len(e.attachments), channel=channel) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="images", aliases=["image"]) # type: ignore async def _images( self, @@ -221,6 +229,8 @@ async def _images( """ await _cleanup(ctx, number, lambda e: len(e.embeds) or len(e.attachments), channel=channel) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="user", aliases=["member"]) # type: ignore async def _user( self, @@ -245,6 +255,8 @@ async def _user( """ await _cleanup(ctx, number, lambda e: e.author == member, channel=channel) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="contains", aliases=["contain"]) # type: ignore async def _contains( self, @@ -276,6 +288,8 @@ async def _contains( else: await _cleanup(ctx, 100, lambda e: text in e.content, channel=channel) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="bot", aliases=["bots"]) # type: ignore async def _bot( self, @@ -307,6 +321,8 @@ def predicate(message: discord.Message) -> Union[Optional[bool], str]: await _cleanup(ctx, number, predicate, channel=channel) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="emoji", aliases=["emojis"]) # type: ignore async def _emoji( self, @@ -337,6 +353,8 @@ def predicate(message: discord.Message) -> bool: await _cleanup(ctx, number, predicate, channel=channel) # https://github.com/Rapptz/RoboDanny/blob/rewrite/cogs/mod.py#L1829 + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="reactions", aliases=["reaction"]) # type: ignore async def _reactions( self, @@ -372,6 +390,8 @@ async def _reactions( allowed_mentions=discord.AllowedMentions(replied_user=False), ) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="self") # type: ignore async def _self( self, @@ -394,6 +414,8 @@ async def _self( """ await _cleanup(ctx, number, lambda e: e.author == ctx.author, channel=channel) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="mine") # type: ignore async def _mine( self, @@ -416,6 +438,8 @@ async def _mine( """ await _cleanup(ctx, number, lambda e: e.author == ctx.guild.me, channel=channel) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="links", aliases=["link"]) # type: ignore async def _links( self, @@ -438,6 +462,8 @@ async def _links( """ await _cleanup(ctx, number, lambda m: LINKS_RE.search(m.content), channel=channel) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="after") # type: ignore async def _after( self, @@ -476,7 +502,7 @@ async def _after( if after is None: await ctx.send( - f"Could not find any messages to delete.", + "Could not find any messages to delete.", reference=ctx.message.to_reference(fail_if_not_exists=False), allowed_mentions=discord.AllowedMentions(replied_user=False), ) @@ -508,6 +534,8 @@ async def _after( allowed_mentions=discord.AllowedMentions(replied_user=False), ) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="before") # type: ignore async def _before( self, @@ -548,7 +576,7 @@ async def _before( if before is None: await ctx.send( - f"Could not find any messages to delete.", + "Could not find any messages to delete.", reference=ctx.message.to_reference(fail_if_not_exists=False), allowed_mentions=discord.AllowedMentions(replied_user=False), ) @@ -581,6 +609,8 @@ async def _before( allowed_mentions=discord.AllowedMentions(replied_user=False), ) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="between") # type: ignore async def _between( self, @@ -646,6 +676,8 @@ async def _between( allowed_mentions=discord.AllowedMentions(replied_user=False), ) + @commands.bot_has_permissions(manage_messages=True) + @has_hybrid_permissions(manage_messages=True, read_message_history=True) @_purge.command(name="duplicates", aliases=["duplicate", "spam"]) # type: ignore async def _duplicates(self, ctx: commands.GuildContext, number: commands.Range[int, 1, 2000]): """ @@ -694,6 +726,8 @@ def check(m: discord.Message): ) # https://github.com/Rapptz/RoboDanny/blob/rewrite/cogs/mod.py#L1704 + @has_hybrid_permissions(administrator=True) + @commands.bot_has_permissions(manage_messages=True) @_purge.command(name="custom") # type: ignore async def _custom( self, diff --git a/purge/utils.py b/purge/utils.py index 43a3649..5f2ceef 100644 --- a/purge/utils.py +++ b/purge/utils.py @@ -28,7 +28,7 @@ ) CUSTOM_EMOJI_RE: Pattern[str] = re.compile(r"") -LINKS_RE = re.compile( +LINKS_RE: Pattern[str] = re.compile( r"((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*", flags=re.IGNORECASE, ) @@ -70,7 +70,7 @@ async def _cleanup( channel: Optional[ # type: ignore Union[discord.Thread, discord.TextChannel, discord.VoiceChannel, discord.StageChannel] ] = None, -): +) -> None: channel: Union[ discord.Thread, discord.TextChannel, discord.VoiceChannel, discord.StageChannel ] = (channel if channel else ctx.channel)