[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:
Michael H 2018-05-13 19:49:45 -04:00 committed by palmtree5
parent 35001107e0
commit 2b35d9f012

View File

@ -40,7 +40,8 @@ class Cleanup:
@staticmethod @staticmethod
async def get_messages_for_deletion( async def get_messages_for_deletion(
ctx: commands.Context, channel: discord.TextChannel, number, 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: ) -> list:
""" """
Gets a list of messages meeting the requirements to be deleted. 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, - The message passes a provided check (if no check is provided,
this is automatically true) this is automatically true)
- The message is less than 14 days old - The message is less than 14 days old
- The message is not pinned
""" """
to_delete = [] to_delete = []
too_old = False too_old = False
@ -59,8 +61,12 @@ class Cleanup:
async for message in channel.history(limit=limit, async for message in channel.history(limit=limit,
before=before, before=before,
after=after): after=after):
if (not number or len(to_delete) - 1 < number) and check(message) \ if (
and (ctx.message.created_at - message.created_at).days < 14: (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) to_delete.append(message)
elif (ctx.message.created_at - message.created_at).days >= 14: elif (ctx.message.created_at - message.created_at).days >= 14:
too_old = True too_old = True
@ -83,7 +89,7 @@ class Cleanup:
@cleanup.command() @cleanup.command()
@commands.guild_only() @commands.guild_only()
@commands.bot_has_permissions(manage_messages=True) @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. """Deletes last X messages matching the specified text.
Example: Example:
@ -109,7 +115,8 @@ class Cleanup:
return False return False
to_delete = await self.get_messages_for_deletion( 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 "\ reason = "{}({}) deleted {} messages "\
" containing '{}' in channel {}.".format(author.name, " containing '{}' in channel {}.".format(author.name,
@ -124,7 +131,7 @@ class Cleanup:
@cleanup.command() @cleanup.command()
@commands.guild_only() @commands.guild_only()
@commands.bot_has_permissions(manage_messages=True) @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. """Deletes last X messages from specified user.
Examples: Examples:
@ -159,7 +166,8 @@ class Cleanup:
return False return False
to_delete = await self.get_messages_for_deletion( 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 "\ reason = "{}({}) deleted {} messages "\
" made by {}({}) in channel {}."\ " made by {}({}) in channel {}."\
@ -176,7 +184,7 @@ class Cleanup:
@cleanup.command() @cleanup.command()
@commands.guild_only() @commands.guild_only()
@commands.bot_has_permissions(manage_messages=True) @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. """Deletes all messages after specified message.
To get a message id, enable developer mode in Discord's To get a message id, enable developer mode in Discord's
@ -202,7 +210,7 @@ class Cleanup:
return return
to_delete = await self.get_messages_for_deletion( 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 {}."\ reason = "{}({}) deleted {} messages in channel {}."\
@ -215,7 +223,7 @@ class Cleanup:
@cleanup.command() @cleanup.command()
@commands.guild_only() @commands.guild_only()
@commands.bot_has_permissions(manage_messages=True) @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. """Deletes last X messages.
Example: Example:
@ -232,7 +240,8 @@ class Cleanup:
return return
to_delete = await self.get_messages_for_deletion( 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 {}."\ reason = "{}({}) deleted {} messages in channel {}."\
@ -248,7 +257,7 @@ class Cleanup:
@cleanup.command(name='bot') @cleanup.command(name='bot')
@commands.guild_only() @commands.guild_only()
@commands.bot_has_permissions(manage_messages=True) @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.""" """Cleans up command messages and messages from the bot."""
channel = ctx.message.channel channel = ctx.message.channel
@ -280,7 +289,8 @@ class Cleanup:
return False return False
to_delete = await self.get_messages_for_deletion( 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 {} "\ reason = "{}({}) deleted {} "\
@ -295,7 +305,9 @@ class Cleanup:
await slow_deletion(to_delete) await slow_deletion(to_delete)
@cleanup.command(name='self') @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. """Cleans up messages owned by the bot.
By default, all messages are cleaned. If a third argument is specified, By default, all messages are cleaned. If a third argument is specified,
@ -345,7 +357,8 @@ class Cleanup:
return False return False
to_delete = await self.get_messages_for_deletion( 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 # Selfbot convenience, delete trigger message