简体   繁体   中英

Discord.js await messages from users other than the author

The title basically says it all, Is there a way for me to await for a message from a user other than the author. Like if a user @s someone in the command can I wait for the @ed person to respond to a certain question?

You can use TextChannel#awaitMessages() to listen for new messages in a channel. The first parameter of this function is a filter method, which can look something like this:

const filter = (message) => message.content === 'COWS';

message.channel.awaitMessages(filter, { max: 1 }).then((collected) => {
  // someone just said COWS in the channel
});

Just as I compared the message content, you can also compare the message author. This way you can listen for a specific person to respond to a question, collect their input, and then do something with it.

Basically just set the filter so it checks if the user who sent a message is the pinged/mentioned member.

Ex.

//just get a user from the message
let mentionedMember = message.mentions.users.first(); 
//checks if the user who send the message is the mentioned member
const filter = (message) => message.author == mentionedMember;

message.channel.awaitMessages(filter, { max: 1 }).then((collected) => {
    //Whatever the mentioned member sent
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM