繁体   English   中英

如何让 discord js 随机选择两张图片?

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

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

  }
}

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

好吧,这就像使用Math.floor(Math.random() * 2)生成 0-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"]});

  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM