Ajay Shah
Ajay Shah

Reputation: 111

Discord.py delete all messages except pin messages

Currently my discord bot has a feature when a person types !delete it deletes most text in the channel. However, it deletes all messages including pinned one. How can I enhance my code so that it does not delete pin messages?

if message.content.startswith('!delete'):
   tmp = await client.send_message(message.channel, 'Clearing messages...')
   async for msg in client.logs_from(message.channel):
     await client.delete_message(msg)

Upvotes: 2

Views: 4595

Answers (2)

iElden
iElden

Reputation: 1280

Or you can use purge_from function and usecheck parameter for only delete non-pinned messages.

dicord.py :

if message.content.startswith('!delete'):
    await client.purge_from(channel, limit=None, check=lambda msg: not msg.pinned)

discord.py-rewrite:

if message.content.startswith('!delete'):
    await channe.purge(limit=None, check=lambda msg: not msg.pinned)

Upvotes: 3

Benjin
Benjin

Reputation: 3497

You can check if the message is pinned using message.pinned.

if message.content.startswith('!delete'):
    tmp = await client.send_message(message.channel, 'Clearing messages...')
    async for msg in client.logs_from(message.channel):
        if not msg.pinned:
            await client.delete_message(msg)

Upvotes: 0

Related Questions