mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
[V3 cleanup] Respect pinned messages by default (#1596)
* This sets the default behavior for `get_messages_for_deletetion()` to not include pinned messages, while providing a way to override that resolves #1589 * actually make commands parse for pinned deletion * fix capitalization
This commit is contained in:
parent
35001107e0
commit
2b35d9f012
@ -40,7 +40,8 @@ class Cleanup:
|
||||
@staticmethod
|
||||
async def get_messages_for_deletion(
|
||||
ctx: commands.Context, channel: discord.TextChannel, number,
|
||||
check=lambda x: True, limit=100, before=None, after=None
|
||||
check=lambda x: True, limit=100, before=None, after=None,
|
||||
delete_pinned=False
|
||||
) -> list:
|
||||
"""
|
||||
Gets a list of messages meeting the requirements to be deleted.
|
||||
@ -50,6 +51,7 @@ class Cleanup:
|
||||
- The message passes a provided check (if no check is provided,
|
||||
this is automatically true)
|
||||
- The message is less than 14 days old
|
||||
- The message is not pinned
|
||||
"""
|
||||
to_delete = []
|
||||
too_old = False
|
||||
@ -59,8 +61,12 @@ class Cleanup:
|
||||
async for message in channel.history(limit=limit,
|
||||
before=before,
|
||||
after=after):
|
||||
if (not number or len(to_delete) - 1 < number) and check(message) \
|
||||
and (ctx.message.created_at - message.created_at).days < 14:
|
||||
if (
|
||||
(not number or len(to_delete) - 1 < number)
|
||||
and check(message)
|
||||
and (ctx.message.created_at - message.created_at).days < 14
|
||||
and (delete_pinned or not message.pinned)
|
||||
):
|
||||
to_delete.append(message)
|
||||
elif (ctx.message.created_at - message.created_at).days >= 14:
|
||||
too_old = True
|
||||
@ -83,7 +89,7 @@ class Cleanup:
|
||||
@cleanup.command()
|
||||
@commands.guild_only()
|
||||
@commands.bot_has_permissions(manage_messages=True)
|
||||
async def text(self, ctx: commands.Context, text: str, number: int):
|
||||
async def text(self, ctx: commands.Context, text: str, number: int, delete_pinned: bool=False):
|
||||
"""Deletes last X messages matching the specified text.
|
||||
|
||||
Example:
|
||||
@ -109,7 +115,8 @@ class Cleanup:
|
||||
return False
|
||||
|
||||
to_delete = await self.get_messages_for_deletion(
|
||||
ctx, channel, number, check=check, limit=1000, before=ctx.message)
|
||||
ctx, channel, number, check=check, limit=1000, before=ctx.message,
|
||||
delete_pinned=delete_pinned)
|
||||
|
||||
reason = "{}({}) deleted {} messages "\
|
||||
" containing '{}' in channel {}.".format(author.name,
|
||||
@ -124,7 +131,7 @@ class Cleanup:
|
||||
@cleanup.command()
|
||||
@commands.guild_only()
|
||||
@commands.bot_has_permissions(manage_messages=True)
|
||||
async def user(self, ctx: commands.Context, user: str, number: int):
|
||||
async def user(self, ctx: commands.Context, user: str, number: int, delete_pinned: bool=False):
|
||||
"""Deletes last X messages from specified user.
|
||||
|
||||
Examples:
|
||||
@ -159,7 +166,8 @@ class Cleanup:
|
||||
return False
|
||||
|
||||
to_delete = await self.get_messages_for_deletion(
|
||||
ctx, channel, number, check=check, limit=1000, before=ctx.message
|
||||
ctx, channel, number, check=check, limit=1000, before=ctx.message,
|
||||
delete_pinned=delete_pinned
|
||||
)
|
||||
reason = "{}({}) deleted {} messages "\
|
||||
" made by {}({}) in channel {}."\
|
||||
@ -176,7 +184,7 @@ class Cleanup:
|
||||
@cleanup.command()
|
||||
@commands.guild_only()
|
||||
@commands.bot_has_permissions(manage_messages=True)
|
||||
async def after(self, ctx: commands.Context, message_id: int):
|
||||
async def after(self, ctx: commands.Context, message_id: int, delete_pinned: bool=False):
|
||||
"""Deletes all messages after specified message.
|
||||
|
||||
To get a message id, enable developer mode in Discord's
|
||||
@ -202,7 +210,7 @@ class Cleanup:
|
||||
return
|
||||
|
||||
to_delete = await self.get_messages_for_deletion(
|
||||
ctx, channel, 0, limit=None, after=after
|
||||
ctx, channel, 0, limit=None, after=after, delete_pinned=delete_pinned
|
||||
)
|
||||
|
||||
reason = "{}({}) deleted {} messages in channel {}."\
|
||||
@ -215,7 +223,7 @@ class Cleanup:
|
||||
@cleanup.command()
|
||||
@commands.guild_only()
|
||||
@commands.bot_has_permissions(manage_messages=True)
|
||||
async def messages(self, ctx: commands.Context, number: int):
|
||||
async def messages(self, ctx: commands.Context, number: int, delete_pinned: bool=False):
|
||||
"""Deletes last X messages.
|
||||
|
||||
Example:
|
||||
@ -232,7 +240,8 @@ class Cleanup:
|
||||
return
|
||||
|
||||
to_delete = await self.get_messages_for_deletion(
|
||||
ctx, channel, number, limit=1000, before=ctx.message
|
||||
ctx, channel, number, limit=1000, before=ctx.message,
|
||||
delete_pinned=delete_pinned
|
||||
)
|
||||
|
||||
reason = "{}({}) deleted {} messages in channel {}."\
|
||||
@ -248,7 +257,7 @@ class Cleanup:
|
||||
@cleanup.command(name='bot')
|
||||
@commands.guild_only()
|
||||
@commands.bot_has_permissions(manage_messages=True)
|
||||
async def cleanup_bot(self, ctx: commands.Context, number: int):
|
||||
async def cleanup_bot(self, ctx: commands.Context, number: int, delete_pinned: bool=False):
|
||||
"""Cleans up command messages and messages from the bot."""
|
||||
|
||||
channel = ctx.message.channel
|
||||
@ -280,7 +289,8 @@ class Cleanup:
|
||||
return False
|
||||
|
||||
to_delete = await self.get_messages_for_deletion(
|
||||
ctx, channel, number, check=check, limit=1000, before=ctx.message
|
||||
ctx, channel, number, check=check, limit=1000, before=ctx.message,
|
||||
delete_pinned=delete_pinned
|
||||
)
|
||||
|
||||
reason = "{}({}) deleted {} "\
|
||||
@ -295,7 +305,9 @@ class Cleanup:
|
||||
await slow_deletion(to_delete)
|
||||
|
||||
@cleanup.command(name='self')
|
||||
async def cleanup_self(self, ctx: commands.Context, number: int, match_pattern: str = None):
|
||||
async def cleanup_self(
|
||||
self, ctx: commands.Context, number: int,
|
||||
match_pattern: str = None, delete_pinned: bool=False):
|
||||
"""Cleans up messages owned by the bot.
|
||||
|
||||
By default, all messages are cleaned. If a third argument is specified,
|
||||
@ -345,7 +357,8 @@ class Cleanup:
|
||||
return False
|
||||
|
||||
to_delete = await self.get_messages_for_deletion(
|
||||
ctx, channel, number, check=check, limit=1000, before=ctx.message
|
||||
ctx, channel, number, check=check, limit=1000, before=ctx.message,
|
||||
delete_pinned=delete_pinned
|
||||
)
|
||||
|
||||
# Selfbot convenience, delete trigger message
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user