简体   繁体   中英

discord.js I keep getting what the bot says instead of user's message

I keep getting: "What is your suggestion" in DMs in the bot instead of the answer to the question the user gives

 else if(msg.content === 'suggest') { msg.delete(); msg.reply('What is your suggestion?'); msg.channel.awaitMessages(m => m.author.id == msg.author.id, {max: 1, time: 30000}).then(collected => { var answer = msg.content; bot.users.get('349463140229971989').send(answer.toLowerCase()); }) //bot.users.get('349463140229971989').send(msg.content); }

I've also tried this:

 else if(msg.content.startsWith('suggest ')) { var user = msg.mentions.users.first(); var answer = msg.content; bot.users.get('349463140229971989').send(answer.toLowerCase() + " from: " + msg.author); msg.delete(); msg.reply('Your suggestion has been forwarded'); }

I get " your suggestion has been forwarded ", and it sends me the bot's reply and not my message

The error is that you aren't taking the collected messages content but rather the command msg here:

var answer = msg.content;

change it to:

var answer = collection.first().content;

But what if the user doesn't send a message? Well you would probably want to send a message back that time is up, to do so you need to add time as one of the errors and add a catch statement:

else if(msg.content === 'suggest') {
    msg.delete();
    msg.reply('What is your suggestion?');
    
    const filter = m => m.author.id == msg.author.id;
    const options = {max: 1, time: 30000, errors: ["time"]};

    msg.channel.awaitMessages(filter, options)
        .then(collected => {
            const answer = collected.first().content;
            bot.users.get('349463140229971989').send(answer.toLowerCase());
            msg.reply("Suggestion forwarded");
        })
        .catch(err => msg.reply("Suggestion canceled"));
}

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