Search code examples
discorddiscord.js

message.mentions returns undefined when message is not undefined


I appreciate you looking into my problem and trying to fix it!

The problem The problem is that whenever I try to use message.mentions inside of my code, it always returns as undefined.

What have I tried? I have tried going into the discord.js server and asking people there to help me and they didn't help me at all.

It would be great if someone could help!

The code where the issue occurs is:

interaction.channel.awaitMessages({ messagefilter, max: 1, time: 20000, errors: ['time'] }).then(async message => {
    console.log(message.mentions) // logs undefined
    if (message.mentions) { // if statement fails
        // this part doesn't run
    } else {
        // this part does run
    }
}).

Solution

  • The TextChannel#awaitMessages() method returns a Collection of messages, not a single Message object. Simply defining it as message does not make a Collection a singular Message object, you have to get the first() property of the collection and check the mentions in that! Like so:

    interaction.channel.awaitMessages({ messagefilter, max: 1, time: 20000, errors: ['time'] }).then(async message => { 
    console.log(message.first().mentions);
    ...
    })
    

    You're trying to log Collection#mentions in your console right now which won't be present.