随机消息回复Discord.js 2020

时间:2020-09-28 07:39:51

标签: javascript node.js discord.js

我一直在尝试随机回答,但是我发现的其他答案和教程并没有完全起作用。 大多数教程都有

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);
    }
}

但是,答案并不是真正随机的。当我从命令提示符/终端运行该机器人时,该机器人会得到一个随机答案,但是当用户实际运行它时,它只会给出一个答案。

例如,答案可以是1、2或3。当我运行漫游器时,会随机选择一个答案。假设2。那么无论所有用户怎么说,它只会给出2的答案作为答复。如果我再次运行该机器人并得到3,那么答复将仅为3。

1 个答案:

答案 0 :(得分:1)

正如加布里埃尔·安德拉德(Gabriel Andrade)所说,您需要将randomMessage常量放入execute函数中。如果您不这样做,则启动机器人时,随机消息只会被评估一次。

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);
    }
}
相关问题