Discord.py deleting all messages in a text channel

I've been trying to clear messages (everything) in my Discord bot for a long time using the command: /clear, but it doesn't work. Please help me, please drain the cleanup code. One of my attempts:

@client.event
async def on_message(message):
    if message.content.startswith('/clear'):
        tmp = await client.send_message(message.channel, 'Clearing messages...')
        async for msg in client.logs_from(message.channel):
            await client.delete_message(msg)

What am I doing wrong? I just close the file (when opened). Please help me!

Author: Grey Cat, 2020-04-23

1 answers

I think we should start by reviewing the code. What exactly is wrong with the code you did not specify, so I will say the main things based on the code itself.

Given the rate limits of discord, it will take forever to delete even 100 messages from the channel in your way. Instead, you can use the {[1] method]} Example:

client.purge_from(message.channel)

Documentation: https://discordpy.readthedocs.io/en/async/api.html#discord.Client.purge_from

In fact, this will increase the deletion rate by 100 times. But is there in the does it even make sense? Why not just, for example, recreate the channel with identical parameters?

P.S. I recommend using the latest version of the library, not the old one from the async branch.

 1
Author: Iterator, 2020-05-03 22:11:25