async def purge(self, ctx, num: int, *targets: User): """Bulk delete messages of a certain amount posted by some targeted Discord users, if not providedit just deletes all the messages which it encounters""" total_deleted = 0 messages = ctx.history(limit=None) await messages.__anext__() # includes the invoker's message, so skip that. # discord has a bulk-deletion method which has limits that, # messages cannot be deleted older than 14 days. # cannot delete more than 100 messages at once. async for chunk in chunked(map(lambda m: m[1], takewhile( lambda m: m[0] < num and (ctx.message.created_at - m[1].created_at).days < 14, filterfalse(lambda m: not(m[1].author in targets or not targets), aenumerate(messages)))), 100): chunk = list(chunk) await ctx.channel.delete_messages(chunk) # delete 100 messages at once. total_deleted += len(chunk) # for the rest follow the manual deletion way. async for msg in messages: if not total_deleted <= num: await ctx.send(f"Purged {num} messages in {ctx.channel.mention}.", delete_after=8) break if msg.author in targets or targets is None: await msg.delete() total_deleted += 1
async def test_filterfalse_coroutine_list(self): async def pred(x): return x % 2 == 0 it = ait.filterfalse(pred, srange) for k in [1, 3]: self.assertEqual(await ait.next(it), k) with self.assertRaises(StopAsyncIteration): await ait.next(it)