【问题标题】:How to make a a random response in discord.js?如何在 discord.js 中做出随机响应?
【发布时间】:2020-12-22 04:10:24
【问题描述】:

我只是想发出一个命令,你问一个问题,它会回答是或否 i didn’t try anything

【问题讨论】:

  • 只是说我的命令是设置的。 case “8ball”: Msg.channel.send(“SomeText”); Break;

标签: javascript node.js discord discord.js bots


【解决方案1】:

如果你只有选项,你可以使用Math.random()ternary operator

Math.random() < 0.5 ? message.channel.send('yes') : message.channel.send('no');

基本上,Math.random() 会生成一个介于 1 和 0 之间的(伪)随机数。Math.random() &lt; 0.5 本质上是一个 550 的机会。

【讨论】:

    【解决方案2】:

    只要有一个数组来存储你的答案。

    let answers = [
        'answer 1'
        'answer 2'
    ];
    

    然后让它从中随机选择一个使用

    let index = Math.floor(Math.random() * Math.floor(answers.length));
    message.reply(answer[index]);
    

    【讨论】:

    • 好的,这行得通。太感谢了!好的,我看到Math.random() &lt; 0.5 ? message.channel.send('yes') :message.channel.send('no');
    • 如何在 10 条不同的消息中进行选择?
    • 将10条消息放入数组
    猜你喜欢
    • 2021-01-01
    • 2020-12-28
    • 2020-08-17
    • 2021-10-07
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    相关资源
    最近更新 更多