首页 > 解决方案 > 我怎样才能让discord js随机选择两张图片?

问题描述

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"]});

  }
}

有没有办法随机发送其中一个消息通道发送代码?我的意思是当有人进入-vibecheck它时,每次都会随机给出这两张照片中的一张。

标签: javascriptnode.jsdiscord.js

解决方案


好吧,这就像生成 0-2 之间的随机数一样简单,使用Math.floor(Math.random() * 2)并简单地使用if.. else语句(假设您只想通过代码判断的两条消息来区分)或switch语句(我更喜欢if.. else)来确定应该发送哪个消息。

最终代码

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"]});

  }
}

推荐阅读