問(wèn)題描述
我一直在嘗試隨機(jī)回復(fù),但我發(fā)現(xiàn)的其他答案和教程并不完全有效.大多數(shù)教程都有
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)];
完成并發(fā)送消息
module.exports = {
name: 'random',
description: 'random?',
execute(message, args){
message.channel.send(randomMessage);
}
}
但是,答案并不是真正隨機(jī)的.當(dāng)我從命令提示符/終端運(yùn)行機(jī)器人時(shí),機(jī)器人會(huì)得到一個(gè)隨機(jī)答案,但當(dāng)用戶實(shí)際運(yùn)行它時(shí),它只會(huì)給出一個(gè)答案.
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.當(dāng)我運(yùn)行機(jī)器人時(shí),會(huì)隨機(jī)選擇一個(gè)答案;假設(shè)2.那么無(wú)論所有用戶說(shuō)什么,它都只會(huì)給出2的答案作為回復(fù).如果我再次運(yùn)行機(jī)器人并得到,比如說(shuō) 3,那么回復(fù)將只有 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 所說(shuō),您需要將 randomMessage
常量放在執(zhí)行函數(shù)中.如果你不這樣做,隨機(jī)消息只會(huì)在你啟動(dòng)機(jī)器人時(shí)被評(píng)估一次.
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);
}
}
這篇關(guān)于隨機(jī)消息回復(fù) Discord.js 2020的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!