T
T
Teabrew-py2021-09-08 19:45:45
Python
Teabrew-py, 2021-09-08 19:45:45

How to delete messages of a specified Discord user Python?

I'm writing a bot and I ran into a problem: I need a clear command that will delete messages. It should delete only those messages that the user himself wrote, without touching others. And this is how it really works:

@bot.command(pass_contex=True)
async def clear(ctx, arg=None):
    arg =  arg + 1
    await ctx. channel.purge(limit=arg, check=lambda message: message.author == ctx.author)
    await ctx.send(f'Удалено ` {arg - 1} ` сообщений', delete_after=5)


Unfortunately, things don't work the way we would like. Really needed messages are deleted, but when checking for deletion, those messages that do not need to be deleted are considered deleted. It's easier to put it this way:

[My message] bla-bla
[My message] bla-bla
[Another user's message] bla-bla [Another user's message] bla-bla [Another
user's
message] bla-bla
[My message] bla-bla

In order to delete only my messages, you need to write .clear, then only my messages should be deleted, but in reality you need to write .clear 6, since not only my messages, but also messages of other users are taken into account.

How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-09-08
@Vindicar

Or accept that the team is looking for your messages among the N last ones, and not looking for your N last ones.
Or download the chat history and find how many messages have been since the Nth time of your message.
I must say right away that the latter can be dreary in implementation and slowed down in practice. What if 100,500 others have written in the chat since your old overwritten message?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question