Search code examples
pythonbotsdiscorddiscord.py

Discord.py The list of people and the number of deleted messages in purge command


Do you know how to do something like that? I mean the list of people and the number of deleted messages:

4 messages were removed.

Qbiczeg#9999: 3
Bot#2275: 1
    @bot.command(aliases=["purge"])
    @commands.has_permissions(manage_messages=True)
    async def clear(ctx, amount: int):
        await ctx.channel.purge(limit=amount + 1)
        embed = discord.Embed(title=f"`{amount}` messages were removed.", description="", color=0xff0000)
        await ctx.send(embed=embed, delete_after=2)

Solution

  • You could iterate instead of using purge e.g:

    @bot.command(aliases=["purge"])
    @commands.has_permissions(manage_messages=True)
    async def clear(ctx, amount: int):
        authors = {}
        async for message in ctx.channel.history(limit=amount):
            if message.author not in authors:
                authors[message.author] = 1
            else:
                authors[message.author] += 1
            message.delete()
    
        msg = "\n".join([f"{author}:{amount}" for author, amount in authors.items()])
        await ctx.channel.send(msg)