discord.js尝试创建具有2个数组的随机结果命令

时间:2019-02-10 23:37:49

标签: discord discord.js

我已经在我的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,
    }
  }
});
}```


1 个答案:

答案 0 :(得分:0)

您只能在对象数组中同时获取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,
      }
    }
  });
}