discord.js 尝试使用 2 个数组创建随机结果命令

discord.js trying to create a random outcome command with 2 arrays

我已经在我的 discord 机器人上工作了一段时间,它有一个地雷命令功能,但只有一个结果给用户 20 银币和一条简单的消息,但我想要机器人可以给出多个不同的答案银量。

我尝试在一个数组中使用 'dl.AddXp' 和消息,但它只是给出了一个错误。

if (command === "mine") {

  var rando_choice = [
    dl.AddXp(message.author.id, -20),
    dl.AddXp(message.author.id, 50),
    dl.AddXp(message.author.id, -10)
  ]

  var rando_choice2 = [
    "You broke your leg while mining and had to pay a doctor to help. **-20 Silver**",
    "You explored a new cave and find some new ores. **+50 Silver**",
    "You found nothing in the cave today."
  ]

  if(!message.member.roles.some(r=>["Pickaxe"].includes(r.name)) )
  return message.reply("You do not have a pickaxe!");
  (rando_choice[Math.floor(Math.random() * rando_choice.length)]),
  message.channel.send({embed: {
    color: `${message.member.displayColor}`,
    title: `${message.member.displayName}`,
    fields: [{
        name: "**MINE :pick: **",
        value:  (rando_choice2[Math.floor(Math.random() * rando_choice2.length)]),
      },
    ],
    timestamp: new Date(),
    footer: {
      icon_url: client.user.avatarURL,
    }
  }
});
}```


您可以将 xp 值和消息放在对象数组中,然后从中获取一个随机元素。看看下面的代码。有一个具有 2 个属性的对象数组。一个 XP 属性 和一条消息 属性。您可以根据需要扩展它。

if (command === "mine") {

  const choices = [
    {
      xp: -20,
      message: "You broke your leg while mining and had to pay a doctor to help. **-20 Silver**"
    },
    {
      xp: 50,
      message: "You explored a new cave and find some new ores. **+50 Silver**"
    },
    {
      xp: -10,
      message: "You found nothing in the cave today."
    }
    // Add more results as you see fit
  ];

  if(!message.member.roles.some(r=>["Pickaxe"].includes(r.name)))
    return message.reply("You do not have a pickaxe!");

  const randomOption = choices[Math.floor(Math.random() * choices.length)];

  dl.AddXp(message.author.id, randomOption.xp);

  message.channel.send({
    embed: {
      color: `${message.member.displayColor}`,
      title: `${message.member.displayName}`,
      fields: [
        {
          name: "**MINE :pick: **",
          value: randomOption.message
        }
      ],
      timestamp: new Date(),
      footer: {
        icon_url: client.user.avatarURL,
      }
    }
  });
}