cute
cute

Reputation: 345

Is there a way to check if message.content on Discord contains a file?

On my Discord server, I have a #selfies channel where people share photos and chat about them. Every now and then, I would like to somehow prune all messages that do not contain files/images. I have tried checking the documentation, but I could see not see any way of doing this. Is it not possible?

Upvotes: 6

Views: 13319

Answers (3)

Dhrumil Patel
Dhrumil Patel

Reputation: 31

Following up on @ADug's answer, message.attachments is a list of discord.Files in a message. But, discordpy has methods to help you purge messages!

You can use channel.purge on the #selfies channel.

def has_no_messages(message):
    return len(message.attachments) == 0
await channel.purge(limit=100, check=has_no_messages) # Specify an appropriate limit

You can also do this in a single line with lambda expressions:

await channel.purge(limit=100, check=lambda x: len(x.attachments) == 0)

Upvotes: 0

Raymond Zhang
Raymond Zhang

Reputation: 730

I'm not too familiar with discord.py (since I use discord.js), but if this is similar to discord.js there should be a message event. This event will run on every message, which is ideal for what you are searching for now. Now for the pseudocode...

//written in javascript
Message event:
On each message {
    if(message.attachments.size <= 0) {
        message.delete(); 
    }
}

The general idea is that using the message event, (so on each message), you check if the message's attachments list is greater than 0 (meaning it contains a file or image) and if it is not, call the delete() function in python to delete the message.

Upvotes: 1

ADug
ADug

Reputation: 314

You can iterate through every message and do:

if not message.attachments: ...

message.attachments returns a list and you can check if it is empty using if not

Upvotes: 4

Related Questions