From e2bc1e650b6374e2e869aee2ff438121ad438a4b Mon Sep 17 00:00:00 2001 From: Myned Date: Sun, 14 Jan 2018 01:28:27 -0500 Subject: [PATCH] Add purge user from channel command --- src/cogs/management.py | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/cogs/management.py b/src/cogs/management.py index 97aa933..bbcdd55 100644 --- a/src/cogs/management.py +++ b/src/cogs/management.py @@ -38,6 +38,57 @@ class Administration: async def _prune_user(self, ctx): pass + @_prune_user.command(name='channel', aliases=['channels', 'chans', 'chan', 'ch', 'c']) + async def _prune_user_channel(self, ctx, user: d.User, *channels: d.TextChannel): + def confirm(r, u): + if u is ctx.author: + if r.emoji == '\N{OCTAGONAL SIGN}': + raise exc.Abort + if r.emoji == '\N{THUMBS UP SIGN}': + return True + return False + + if not channels: + channels = [ctx.channel] + + try: + pruning = await ctx.send(f'\N{HOURGLASS} **Pruning** {user.mention}**\'s messages from** {"**,** ".join([channel.mention for channel in channels])} **might take some time.** Proceed, {ctx.author.mention}?') + await pruning.add_reaction('\N{THUMBS UP SIGN}') + await pruning.add_reaction('\N{OCTAGONAL SIGN}') + await asyncio.sleep(1) + + await self.bot.wait_for('reaction_add', check=confirm, timeout=10 * 60) + + deleting = await ctx.send(f'\N{WASTEBASKET} **Deleting** {user.mention}**\'s messages...**') + await asyncio.sleep(1) + + c = 0 + for channel in channels: + await deleting.edit(content=f'\N{WASTEBASKET} **Deleting** {user.mention}**\'s messages from** {channel.mention}') + + deleted = await channel.purge(check=lambda m: m.author.id == user.id, before=pruning, limit=None) + c += len(deleted) + + await asyncio.sleep(1) + + for channel in channels: + missed = 0 + async for message in channel.history(before=pruning, limit=None): + if message.author.id == user.id: + missed += 1 + + if missed > 0: + await ctx.send(f'\N{DOUBLE EXCLAMATION MARK} `{missed}` **messages were not deleted in** {channel.mention}') + + await ctx.send(f'\N{WHITE HEAVY CHECK MARK} **Finished deleting** `{c}` **of** {user.mention}**\'s messages**') + + except exc.Abort: + await ctx.send('**Deletion aborted**', delete_after=7) + await ctx.message.add_reaction('\N{CROSS MARK}') + except TimeoutError: + await ctx.send('**Deletion timed out**', delete_after=7) + await ctx.message.add_reaction('\N{CROSS MARK}') + @_prune_user.command(name='all', aliases=['a'], brief='Prune a user\'s messages from the guild', description='about flag centers on message 50 of 101 messages\n\npfg \{user id\} [before|after|about] [\{message id\}]\n\nExample:\npfg \{user id\} before \{message id\}', hidden=True) @cmds.is_owner() async def _prune_user_all(self, ctx, user, when=None, reference=None):