来自单个用户的Discord.js上的输入

时间:2020-10-06 05:07:42

标签: node.js discord discord.js

我希望能够获得用户的回复。问题是我不知道如何确保得到特定用户而不是其他人的响应。我将如何做到这一点? 例如,要求最小和最大数字,然后从它们之间获取一个随机数。

这是我尝试过的:

const Discord = require('discord.js');


const client = new Discord.Client();

const prefix = '-';

client.on('message', message => {
    if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();


if(command === random) {
    message.channel.send('Please enter your starting number')
    channel.fetchMessages({ limit: 1 }).then(messages => {
        let min = messages.first();
    })
    message.channel.send('Please enter your limit')
    channel.fetchMessages({ limit: 1 }).then(messages => {
        let max = messages.first();
    })

    getRndInteger(min,max)

    function getRndInteger(min, max) {
        console.log(Math.floor(Math.random() * (max - min + 1) ) + min); 
      }

    
}

});

我希望它一直等到激活它的用户说出一个数字(它也可能会超时)。

1 个答案:

答案 0 :(得分:0)

您可以使用Channel.createMessageCollector()等到您的目标用户回复后,这是一个简单的示例:

let min, max;
const filter = (msg) => msg.author.id === message.author.id; // only match messages from whoever sent the command
const collector = message.channel.createMessageCollector(filter, { max: 2 });
collector.on("collect", (m) => {
  if(!min) { // if min hasn't already been set, set it
    min = parseInt(m.content);
  } else { // otherwise set max
    max = parseInt(m.content);
  }
});
collector.on("end", () => {
  getRndInteger(min, max);
});

还有其他解决方法,但是我认为这是最简单的方法,因为否则您将需要多个侦听器。