随机嵌入消息discord.js的问题[已解决]

时间:2020-08-31 19:13:17

标签: javascript node.js discord.js

我正在尝试执行带有随机嵌入的命令,但是当我触发命令时。它总是发送相同的选项,并且如果我重新加载bot,它将选择一个新选项,但是在我重新加载之前,它将保持不变。

我的代码顶部的“变量”:

var facts = ["Item1", "Item2", "Item 3", "Item 4" ];
var fact = Math.floor(Math.random() * facts.length);

命令:

    if (message.content.startsWith (prefix + "random")){
      var embed = new Discord.MessageEmbed()
          .setTitle(":dvd: Sup stack overflow")
          .setDescription(facts[fact])
          .setColor("#f5980c")
          .addField(invisible, field)
          .setFooter(footer)
      message.channel.send(embed)
      console.log(`? ${message.author.tag} used saraiaiai`)
      
    }

1 个答案:

答案 0 :(得分:0)

每次收到消息时,您需要为fact生成一个新值:

if (message.content.startsWith (prefix + "random")){
    // notice this is here now
    var fact = Math.floor(Math.random() * facts.length);
    var embed = new Discord.MessageEmbed()
      .setTitle(":dvd: Sup stack overflow")
      .setDescription(facts[fact])
      .setColor("#f5980c")
      .addField(invisible, field)
      .setFooter(footer)
  message.channel.send(embed)
  console.log(`? ${message.author.tag} used saraiaiai`)   
}