user6374210
user6374210

Reputation:

Python: Discord API

Here's the bit from the docs I don't understand.

purge_from(channel, *, limit=100, check=None, before=None, after=None, around=None)

This function is a coroutine.

Purges a list of messages that meet the criteria given by the predicate check. If a check is not provided then all messages are deleted without discrimination.

You must have Manage Messages permission to delete messages even if they are your own. The Read Message History permission is also needed to retrieve message history.

Usable only by bot accounts.

Parameters: channel (Channel) – The channel to purge from. limit (int) – The number of messages to search through. This is not the number of messages that will be deleted, though it can be. check (predicate) – The function used to check if a message should be deleted. It must take a Message as its sole parameter.

Examples

Deleting bot’s messages

def is_me(m):
    return m.author == client.user

deleted = await client.purge_from(channel, limit=100, check=is_me)
await client.send_message(channel, 'Deleted {} message(s)'.format(len(deleted)))

I understand everything here up until the check parameter. I've tried to use the example as well as I can but I can't. What I'm trying to achieve is to purge every message that has an attachment. Message.Attachments returns an empty list if there is none. Can anyone explain the example as best as possible, or provide code? Thanks.

Upvotes: 0

Views: 1586

Answers (2)

Mapleroot
Mapleroot

Reputation: 51

Remember: Don't directly copy from the API docs as it most likely won't work. I've had this issue before, and my answer was to use @bot.command(pass_context=True) above the command, then use async def clear(ctx, msglimit : int): deleted = await bot.purge_from(ctx.message.channel, limit=msglimit) await bot.say("Cleared **{}** Messages".format(len(deleted))) below it. Remember to thourougly read the docs and FAQ, and set a message limit to the amount it can clear. Good luck coding your bot!

Upvotes: 0

Unlocked
Unlocked

Reputation: 648

I've used discord.py, but I haven't really used purge_from(). Here goes:

check appears to be an argument asking for a predicate-type function with one parameter of the Message type. Messages that return True when used as arguments for that function will be deleted. This could also be rewritten with a lambda for slightly more compact code.

client.purge_from(channel, limit=100, check=lambda m: m.author == client.user)

Upvotes: 1

Related Questions