随机消息回复 Discord.js 2020
我一直在尝试随机回复,但我发现的其他答案和教程并不完全有效.大多数教程都有
I've been trying to put a random reply but the other answers and tutorials I found didn't exactly work. Most of the tutorials have
const messages = [
"seriously?! You thought I would reply",
"hm, yeh thats a pretty random question - Don't ya think?",
"Ok I'm actually running out of options now",
"Please stop asking me",
"Ok, im done!",
"⛔"
];
const randomMessage = messages[Math.floor(Math.random() * messages.length)];
完成并发送消息
module.exports = {
name: 'random',
description: 'random?',
execute(message, args){
message.channel.send(randomMessage);
}
}
但是,答案并不是真正随机的.当我从命令提示符/终端运行机器人时,机器人会得到一个随机答案,但当用户实际运行它时,它只会给出一个答案.
However, the answer isn't really randomized. When I run the bot from the command prompt/terminal, the bot gets a random answer, but then when the user actually runs it, it only gives one answer.
例如,答案可以是 1、2 或 3.当我运行机器人时,会随机选择一个答案;假设2.那么无论所有用户说什么,它都只会给出2的答案作为回复.如果我再次运行机器人并得到,比如说 3,那么回复将只有 3.
For example, the answers can be 1, 2, or 3. When I run the bot one answer gets picked randomly; let's say 2. Then no matter what all users say, it will only give the answer of 2 as the reply. If I run the bot again and get, let's say 3, then the reply will only be 3.
推荐答案
正如 Gabriel Andrade 所说,您需要将 randomMessage
常量放在执行函数中.如果你不这样做,随机消息只会在你启动机器人时被评估一次.
As Gabriel Andrade said, you need to put the randomMessage
constant inside of the execute function. If you don't, the random message will only be evaluated once when you start the bot.
module.exports = {
name: 'random',
description: 'random?',
execute(message, args){
// Now the randomMessage will be recalculated every time the command is run
const randomMessage = messages[Math.floor(Math.random() * messages.length)];
message.channel.send(randomMessage);
}
}
相关文章