else if (parts[0] === Prefix + 'number') {
message.channel.send(message.author.username + ' What is the minimum number');
if (parts[0] === Math.int) {
var MinNum = (Discord.Message);
console.log("minimum number is " + MinNum);
message.channel.send(message.author.username + ' what is the maximum number');
} if (parts[0] === Math.int) {
var MaxNum = (Discord.Message);
console.log("Maximum number is " + MaxNum);
const RandomNum = Random.int(MinNum, MaxNum);
message.channel.send(Message.author.username + " number is " + RandomNum);
} else if (parts[0] === Math.int == false) {
message.channel.send("Sorry " + message.author.username + " that is an invalid number");
}
}
这是随机数生成器的一些代码,当有人说 -number
时,我的机器人询问用户最小数字是多少,当用户输入一个数字时,该数字设置为 {{1} } 变量,最小数量记录在我的控制台中。在此之后,它询问用户的最大数是多少,接下来假设用最大数做同样的事情,在用户输入最大数后,随机数生成器在这两个值之间吐出一个随机数,并且如果用户没有输入号码,我的机器人会说抱歉(用户名),这是一个无效号码。
答案 0 :(得分:0)
我不确定 Math.int
是什么,以及为什么您认为 if (parts[0] === Prefix + 'number')
也会是 (parts[0] === Math.int)
。不幸的是,它不会像这样工作。您不能接受命令并接受任何后续消息作为对机器人问题的答复。
然而,这对于消息收集器来说是一个很好的用例。首先,您可以发送第一个问题并要求第一个限制。发送此消息后,您可以使用 channel.createMessageCollector
在频道中设置消息收集器。
它接受一个 filter
和一个 options
对象。使用过滤器,您可以检查传入消息是否来自输入命令的作者,以及他们的响应是否为有效数字。
createMessageCollector
返回 MessageCollector
,因此您可以订阅 collect
和 end
事件。
每当收集到消息时都会发出 collect
事件。如果这是第一个响应,您可以将其存储在一个数组 (limits
) 中并发送一条新消息,要求最大。
一旦用户发送第二个有效数字,end
事件就会触发,因此您可以生成一个随机数并将其发送给用户。 end
事件也会在达到最大时间时触发,因此您可以检查原因是否是超时,如果是,则发送错误消息。
您还可以创建一个辅助函数来生成两个数字之间的随机整数:
function randomInt([min, max]) {
if (min > max) {
[min, max] = [max, min];
}
return Math.floor(Math.random() * (max - min + 1) + min);
}
这里是完整的代码:
if (command === 'number') {
const embedMin = new MessageEmbed()
.setTitle(`? Get a random number ?`)
.setColor('GREEN')
.setDescription('What is the minimum number?');
await message.channel.send(embedMin);
// filter checks if the response is from the author who typed the command
// and if the response is a valid number
const filter = (response) =>
response.author.id === message.author.id && !isNaN(response.content.trim());
const collector = message.channel.createMessageCollector(filter, {
// set up the max wait time the collector runs
time: 60000, // ms
// set up the max responses to collect
max: 2,
});
// it stores the user responses
const limits = [];
collector.on('collect', async (response) => {
const num = parseInt(response.content.trim(), 10);
limits.push(num);
// if this is the first number sent
if (limits.length === 1) {
const embedMax = new MessageEmbed()
.setTitle(`? Get a random number ?`)
.setColor('GREEN')
.addField('Minimum', limits[0])
.setDescription('What is the maximum number?');
// send the next question
message.channel.send(embedMax);
}
});
// runs when either the max limit is reached or the max time
collector.on('end', (collected, reason) => {
console.log({ collected });
if (reason === 'time') {
const embedTimeout = new MessageEmbed()
.setTitle(`? Get a random number ?`)
.setColor('RED')
.setDescription(
`I'm sorry, I haven't received a response in a minute, so I'm off now. If you need a new random number again, type \`${prefix}number\``,
);
message.channel.send(embedTimeout);
}
if (limits.length === 2) {
// get a random number
const embedRandom = new MessageEmbed()
.setTitle(`? Get a random number ?`)
.setColor('GREEN')
.addField('Minimum', limits[0], true)
.addField('Maximum', limits[1], true)
.setDescription(`Your random number is: ${randomInt(limits)}`);
message.channel.send(embedRandom);
}
});
}