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

佐手、 提交于 2021-02-08 09:55:46

问题


I've been working on my discord bot for a while now and it has a mine command feature but only has one outcome gives the user 20 silver and a simple message but I want multiple answers the bot can give and different amounts of silver.

I've tried to use the 'dl.AddXp' and the message in one array but it just gives off a error.

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:


You can but both the xp value and the message in an array of objects and get a random element from there. Take a look at the code below. There is an array of objects which have 2 properties. A XP property and a message property. You can expand this as you see fit.

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,
      }
    }
  });
}


来源:https://stackoverflow.com/questions/54622251/discord-js-trying-to-create-a-random-outcome-command-with-2-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!