【问题标题】:How do I enter multiple answers for a single command so that they are output randomly? (Discord)如何为单个命令输入多个答案以便随机输出? (不和谐)
【发布时间】:2018-07-01 10:53:02
【问题描述】:

这行代码使用用户编写的消息命令生成输出:

robot.on('message', (message) => {
    if (message.content == "%ex") {
        message.reply("Random message");
    } 
});

但我想要随机发送 3 条消息之一,例如 "Hello!",但我不知道如何更好地实现它。

【问题讨论】:

  • 我要做的第一件事是修复你的分组。您需要在条件之后立即结束 if 括号。其次,您可以尝试制作一组​​随机消息,然后使用Math.random() 和简单的min, max 等式选择一个:Math.floor(Math.random() * (max - min + 1)) + min。如果您无法使其正常工作,请尝试在完成后返回此处。

标签: javascript node.js discord discord.js


【解决方案1】:

您应该处理您的代码格式 :) 不确定我是否理解正确,但这会发回一条随机消息作为响应

function randomMessage(){
    var randomNumber = Math.round(Math.random()*2); // 0, 1 or 2
    switch(randomNumber){
        case 0: return 'Hello!';
        case 1: return 'Bye!';
        case 2: return 'Random';
    }
}

robot.on('message', (message)=>{ 
    if(message.content == "%ex"){
        message.reply(randomMessage());
    }
});

【讨论】:

    猜你喜欢
    • 2019-02-20
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    相关资源
    最近更新 更多