How can I make discord js random choose over two pictures?

Viewed 55
const BaseCommand = require('../../utils/structures/BaseCommand');
const Discord = require("Discord.js");

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

 async run(client, message, args) {
  var rating = Math.floor(Math.random() * 100) + 1;
  let mention = message.mentions.users.first();

  if (!mention) return message.channel.send("Taguj usera prvo!");
  const mentionedMember = message.mentions.members.first();
  const messageToSay = args.join(" ");
  const sayEmbed = new Discord.MessageEmbed()


  message.channel.send(`Au brate ${mention} nisi passo vibe check `, {files: ["https://cdn.discordapp.com/attachments/821106910441898025/822208416906608671/you_out.jpg"]});

  message.channel.send(`MA MAN! VIBEEE ${mention} UPADAJ!`, {files: ["https://cdn.discordapp.com/attachments/821106910441898025/822208425319727114/you_in_.jpg"]});

  }
}

Is there to a way to discord randomly send one of those message channel send codes? What I mean is like when someone enter -vibecheck to it gives one of those two pictures randomly every time.

1 Answers

Well, this would be as simple as generating a random number between 0-2 using Math.floor(Math.random() * 2) and simply using either an if.. else statement (Assuming that you're only looking to differentiate by two messages judging by your code) or a switch statement (I prefer if.. else) to determine which message should be sent.

Final Code

const BaseCommand = require('../../utils/structures/BaseCommand');
const Discord = require("Discord.js");

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

 async run(client, message, args) {
  // var rating = Math.floor(Math.random() * 100) + 1;
  let mention = message.mentions.users.first();

  if (!mention) return message.channel.send("Taguj usera prvo!");
  const mentionedMember = message.mentions.members.first();
  const messageToSay = args.join(" ");
  const sayEmbed = new Discord.MessageEmbed();

  const random = Math.floor(Math.random() * 2);
  if (random === 0) message.channel.send(`Au brate ${mention} nisi passo vibe check `, {files: ["https://cdn.discordapp.com/attachments/821106910441898025/822208416906608671/you_out.jpg"]});

  else message.channel.send(`MA MAN! VIBEEE ${mention} UPADAJ!`, {files: ["https://cdn.discordapp.com/attachments/821106910441898025/822208425319727114/you_in_.jpg"]});

  }
}
Related