M
M
Mem13882021-05-08 19:38:50
Python
Mem1388, 2021-05-08 19:38:50

How to delete messages of a specific user?

There is such a function:

async def clr(self, ctx, member: discord.User, *, amount = 100):
    await ctx.message.delete()
    await ctx.channel.purge(limit = amount, check = lambda message: message.author == member)

It works, but with a minus: in order for the bot to delete messages, provided that there are messages from other users in the history, you also need to take into account their number. How can I ignore messages from other users?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-05-10
@Mem1388

async def remove_messages(ctx, author, count):
    total_messages = 0
    author_messages = 0

    async for message in channel.history(limit=None):
        total_messages += 1
        if message.author == author:
            author_messages += 1
       
        if author_messages == count:
            break

    await ctx.channel.purge(limit = total_messages, check = lambda message: message.author == member)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question