Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

389
Views
How to delete text messages in an image-only channel?

I made a discord bot called YES, and I have a text channel for images only, and I am trying to make it that if there is a text message the bot will delete it and reply "You cannot send text messages". I also made a whitelist for the bot because I want him to be able to send text messages.

client.on("message", async message => {
    var usernames = ["YES"]
    if (usernames.includes(message.author.username)) {
        if (message.attachments.size > 0) {
            message.delete();
            client.channels.cache.get("841260660960395287").send(message.author.username + ", you cannot send text")
        }
    }
})     

This doesn't work at all and it doesn't show any errors.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your if statement if (message.attachments.size > 0) is checking, if the attachments collection's size is bigger than 0. So it will only execute the code in it, if the message has at least one attachment.

You can apply this:

if (message.attachments.size)

If the message has no attachments, the collection "attachments" should be empty, and in this case, you want to delete this message.¹

Also

You should await the <message>.delete()> function, since it returns a promise:

await message.delete();
// Further code

OR²

message.delete()
   .then(msg => console.log(`Deleted message from ${msg.author.username}`))
   .catch(error => console.log(error))

Nice to know:

You don't have to use all the '+' if you want to use variables inside a string. A simpler and way faster solution would be replacing ' with ` and then wrap your variable in ${}. In your code it would look like this:

client.channels.cache.get("841260660960395287").send(`${message.author.username}, you cannot send text`)

Your fixed code should now look like this:

client.on('message', async message => {
    var usernames = ['YES'];
    if (usernames.includes(message.author.username)) {
        if (message.attachments.size) {
            await message.delete();
            client.channels.cache.get('841260660960395287').send(`${message.author.username}, you cannot send text`);
        }
    }
});    

References

  • ¹Expressions and operators
  • ².then()
over 4 years ago · Santiago Trujillo Report

0

The issue here is with your if statement logic. It's checking if the message has more than one attachment, then deletes it. That's the issue, so by checking if the size of the collection is 0 (=== 0), your program will run successfully.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!