【问题标题】:How do I get my bot to wait for reply from a mentioned user discord.js?如何让我的机器人等待提到的用户 discord.js 的回复?
【发布时间】:2021-04-19 17:15:09
【问题描述】:

我希望我的机器人在聊天中等待仅来自命令中提到的用户的响应。

它应该是这样工作的:

  1. 我发&tictactoe @someuser#1234
  2. 机器人响应:
nathen418 wants to play a game of Tic-Tac-Toe with you. Do you accept?
Yes or No
  1. @someuser#1234 类型 Yes 或 No
  2. 机器人以They want to play Tic-Tac-Toe with you! 或They did not want to play with you. :( 响应

如果我@提及自己(@nathen418#0002),这将正常工作。但是,如果我发送命令 &tictactoe @someuser#1234 和 @someuser#1234 尝试响应,机器人会忽略它们。我希望机器人只收听我提到的用户的Yes 或No

这是我的代码:

const exceptionHandler = require('../handlers/exceptionHandler');
const logToConsole = require('../events/logToConsole');
const Discord = require('discord.js');

module.exports = {
    tictactoeCMD: function (message, bot) {
        const member = message.mentions.members.first().id;
        message.channel.send(message.author.username + " wants to play a game of Tic-Tac-Toe with you. Do you accept?\n `Yes` or `No`")
        console.log(message.author.id)
        const collector = new Discord.MessageCollector(message.channel, m => m.author.id === message.author.id, { time: 10000 });
        collector.on('collect', message => {

                console.log(message.author.id)
                console.log("^ message author Below member id")
                console.log(member)
                if (message.author.id === member && message.content.toUpperCase() == "YES") {
                    message.reply(" they want to play Tic-Tac-Toe with you!");
                } else if (message.author.id === member && message.content.toUpperCase() == "NO") {
                    message.reply( " They did not want to play with you. :(");
                }
        })
        //logToConsole.command(message.guild, message);
    }
}

这是我运行机器人时发生的情况:

完整代码可在 github 的 alpha 分支上找到:https://github.com/Antares-Network/AntaresBot/tree/alpha

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    您目前正在做的是请求消息的作者提供输入。 鉴于您有兴趣获取提到的成员的输入,您可能希望将收集到的消息作者的 ID 与您之前定义为 member 的成员 ID 进行比较。

    最终代码:

    onst exceptionHandler = require('../handlers/exceptionHandler');
    const logToConsole = require('../events/logToConsole');
    
    const Discord = require('discord.js');
    
    module.exports = {
        tictactoeCMD: function (message, bot) {
            const member = message.mentions.members.first().id;
            message.channel.send(message.author.username + " wants to play a game of Tic-Tac-Toe with you. Do you accept?\n `Yes` or `No`")
            console.log(message.author.id)
            const collector = new Discord.MessageCollector(message.channel, m => m.author.id === member, { time: 10000 }); // <--- Line edited
            collector.on('collect', message => {
                console.log(message.author.id)
                console.log("^ message author Below member id")
                console.log(member)
                if (message.author.id === member && message.content.toUpperCase() == "YES") {
                    message.reply(" they want to play Tic-Tac-Toe with you!");
                } else if (message.author.id === member && message.content.toUpperCase() == "NO") {
                    message.reply( " They did not want to play with you. :(");
                }
        })
        //logToConsole.command(message.guild, message);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-24
      • 1970-01-01
      • 2021-08-18
      • 2019-03-21
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多