Discord机器人随机生成报价

时间:2020-08-25 15:28:51

标签: javascript node.js discord discord.js

我正在尝试使不和谐的机器人产生随机响应。

这是我当前的代码:

client.on('message', (msg) => {
 if (msg.content === 'ping') {
  var roll = Math.floor(Math.random() * 2) + 1;

  if (roll == 1) msg.reply('pong');
 } else {
  msg.reply('prong');
 }
});

我将如何修改它以便给出三个随机响应?

2 个答案:

答案 0 :(得分:1)

您可以尝试以下方法:

const array = ['first message', 'second message', 'poggers message'];
message.channel.send(array[Math.floor(Math.random() * array.length)]);

console示例:

function getRandomResponse() {
  const array = ['hello', 'goodbye', 'how was your day?', 'ping', 'pong', 'test'];
  console.log(array[Math.floor(Math.random() * array.length)]);
};
.button {
  width: 100%;
}
<button onclick="getRandomResponse()" class=button>Reroll Response</button>

答案 1 :(得分:0)

这是一个简单的8ball命令。

bot.on("message", (message) => {
    const arguments = message.content.slice(prefix.length).trim().split(/ +/g);
    const commandName = arguments.shift().toLowerCase();
    if (message.content.startsWith(prefix) && commandName == "8ball") {
        var facts = [`Yes`, `No`, `Maybe`];
var fact = Math.floor(Math.random() * facts.length);
message.channel.send(facts[fact]);
    }
})

希望这行得通!

相关问题