Skip to content

Commit

Permalink
[Purge] add checks to all commands
Browse files Browse the repository at this point in the history
  • Loading branch information
japandotorg committed Jul 16, 2024
1 parent 324b495 commit c2ed1f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ To add the cogs to your instance please do: `[p]repo add Seina-Cogs https://gith
| RoleUtils | 1.4.0 | <details><summary>Reaction roles, massroling, & role targeting</summary>Reaction roles, massrolin & role targeting</details> |
| Lock | 1.1.5 | <details><summary>Lock channels</summary>Lock channels or the whole server</details> |
| DisboardReminder | 1.3.7 | <details><summary>Set a reminder to bump on disboard</summary>Set a reminder to bump on disboard</details> |
| Purge | 0.1.2 | <details><summary>Advanced Purge/Cleanup.</summary>Purge (deleted) messages that meet a criteria.</details> |
| Purge | 0.1.3 | <details><summary>Advanced Purge/Cleanup.</summary>Purge (deleted) messages that meet a criteria.</details> |
| FreeloaderMode | 0.1.0 | <details><summary>Ban users that leave your server right after an event or something.</summary>Ban freeloaders who leave your server right after an event or something.</details> |
| ErrorHandler | 0.1.0 | <details><summary>Allow custom error message.</summary>Adds ability to replace the output of the bots error handler when CommandInvokeError is raised, all other errors get handled by the old handler.</details> |
| VoiceNoteLog | 0.1.0 | <details><summary>Log the voice note sent by your server members.</summary>Log voice notes sent by your server members.</details> |
Expand Down
42 changes: 38 additions & 4 deletions purge/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Expand Down Expand Up @@ -114,7 +114,7 @@ async def _purge(
**Example:**
- `[p]purge 10`
- `[p] purge 2000`
- `[p]purge 2000`
"""
if ctx.invoked_subcommand is None:

Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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),
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]):
"""
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions purge/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)

CUSTOM_EMOJI_RE: Pattern[str] = re.compile(r"<a?:[a-zA-Z0-9\_]+:([0-9]+)>")
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,
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c2ed1f3

Please sign in to comment.