Search code examples
randomdiscord.jsbotsembed

How to send a discord.js random embed?


I'm looking for a code that would let me send an random embed that I created when someone types a command. For the moment with the code that I have, all embeds are being sent but I want the bot to only send a random one, not all 4. Is this possible ?

module.exports = class PizzaTest extends BaseCommand {
  constructor() {
    super('pizzatest', 'fun', []);
  }

  async run(client, message, args) {
      const capreseEmbed = new Discord.MessageEmbed()
      .setTitle('‱ Attention, pizza en livraison!  🚀')
      .setDescription(`<@${message.author.id}>, il semblerait que ta commande soit prĂȘte. Tu viens de recevoir une magnique pizza **Caprese** ! \n\n *Les ingrĂ©dients sont: Mozarella, Olives, Tomates SĂ©chĂ©es & Basilic.*`)
      .setThumbnail('https://i.imgur.com/McSXASC.png')
      .setFooter(message.author.username, message.author.displayAvatarURL())
      .setTimestamp()
      .setColor("#baff58");

      const reineEmbed = new Discord.MessageEmbed()
      .setTitle('‱ Attention, pizza en livraison!  🚀')
      .setDescription(`<@${message.author.id}>, il semblerait que ta commande soit prĂȘte. Tu viens de recevoir une magnique pizza **Reine** ! \n\n *Les ingrĂ©dients sont: Mozarella, Jambon, Champignons & Basilic.*`)
      .setThumbnail('https://i.imgur.com/AKStODY.png')
      .setFooter(message.author.username, message.author.displayAvatarURL())
      .setTimestamp()
      .setColor("#baff58");

      const vegeEmbed = new Discord.MessageEmbed()
      .setTitle('‱ Attention, pizza en livraison!  🚀')
      .setDescription(`<@${message.author.id}>, il semblerait que ta commande soit prĂȘte. Tu viens de recevoir une magnique pizza **VĂ©gĂ©tarienne** ! \n\n *Les ingrĂ©dients sont: Mozarella, Olives, Poivrons, Champignons & Basilic.*`)
      .setThumbnail('https://i.imgur.com/U0qrSk9.png')
      .setFooter(message.author.username, message.author.displayAvatarURL())
      .setTimestamp()
      .setColor("#baff58");

      const andalouseEmbed = new Discord.MessageEmbed()
      .setTitle('‱ Attention, pizza en livraison!  🚀')
      .setDescription(`<@${message.author.id}>, il semblerait que ta commande soit prĂȘte. Tu viens de recevoir une magnique pizza **Andalouse** ! \n\n *Les ingrĂ©dients sont: Mozarella, Poivrons, Boulette de Boeuf & Sauce Andalouse.*`)
      .setThumbnail('https://i.imgur.com/dvgkC4K.png')
      .setFooter(message.author.username, message.author.displayAvatarURL())
      .setTimestamp()
      .setColor("#baff58");


  message.channel.send(capreseEmbed).catch(err => console.log(err));
  message.channel.send(reineEmbed).catch(err => console.log(err));
  message.channel.send(vegeEmbed).catch(err => console.log(err));
  message.channel.send(andalouseEmbed).catch(err => console.log(err));
  }
}

I know that I can use something like .setDescription(description) with all of them stated at the beginning but I need the description and thumbnail to match so that wouldn't work I guess ?

Thank you!


Solution

  • Yes this is possible and very simple to do:

          const capreseEmbed = new Discord.MessageEmbed()
          .setTitle('‱ Attention, pizza en livraison!  🚀')
          .setDescription(`<@${message.author.id}>, il semblerait que ta commande soit prĂȘte. Tu viens de recevoir une magnique pizza **Caprese** ! \n\n *Les ingrĂ©dients sont: Mozarella, Olives, Tomates SĂ©chĂ©es & Basilic.*`)
          .setThumbnail('https://i.imgur.com/McSXASC.png')
          .setFooter(message.author.username, message.author.displayAvatarURL())
          .setTimestamp()
          .setColor("#baff58")
    
          const reineEmbed = new Discord.MessageEmbed()
          .setTitle('‱ Attention, pizza en livraison!  🚀')
          .setDescription(`<@${message.author.id}>, il semblerait que ta commande soit prĂȘte. Tu viens de recevoir une magnique pizza **Reine** ! \n\n *Les ingrĂ©dients sont: Mozarella, Jambon, Champignons & Basilic.*`)
          .setThumbnail('https://i.imgur.com/AKStODY.png')
          .setFooter(message.author.username, message.author.displayAvatarURL())
          .setTimestamp()
          .setColor("#baff58")
    
          const vegeEmbed = new Discord.MessageEmbed()
          .setTitle('‱ Attention, pizza en livraison!  🚀')
          .setDescription(`<@${message.author.id}>, il semblerait que ta commande soit prĂȘte. Tu viens de recevoir une magnique pizza **VĂ©gĂ©tarienne** ! \n\n *Les ingrĂ©dients sont: Mozarella, Olives, Poivrons, Champignons & Basilic.*`)
          .setThumbnail('https://i.imgur.com/U0qrSk9.png')
          .setFooter(message.author.username, message.author.displayAvatarURL())
          .setTimestamp()
          .setColor("#baff58")
    
          const andalouseEmbed = new Discord.MessageEmbed()
          .setTitle('‱ Attention, pizza en livraison!  🚀')
          .setDescription(`<@${message.author.id}>, il semblerait que ta commande soit prĂȘte. Tu viens de recevoir une magnique pizza **Andalouse** ! \n\n *Les ingrĂ©dients sont: Mozarella, Poivrons, Boulette de Boeuf & Sauce Andalouse.*`)
          .setThumbnail('https://i.imgur.com/dvgkC4K.png')
          .setFooter(message.author.username, message.author.displayAvatarURL())
          .setTimestamp()
          .setColor("#baff58")
    
          var embedArr = [capreseEmbed, reineEmbed, vegeEmbed, andalouseEmbed];
          let randomEmbed = embedArr[Math.floor(Math.random() * embedArr.length)];
          message.channel.send(randomEmbed);
    

    At first we define all embeds, like you already did. Then we store them in an array. From that array we want to get a value from a random index. That is done in randomEmbed. Then we send the random embed into the channel.