javascript - 我怎样才能让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
它时,每次都会随机给出这两张照片中的一张。
解决方案
好吧,这就像生成 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"]});
}
}
推荐阅读
- r - R:列表到数据框映射功能
- java - 即使指定的模式是点,DecimalFormat 也使用逗号分隔符
- javascript - 如何使用 Angular 9 将多个图像文件和 PDF 文件合并为单个 PDF
- qt - 一次取消选中/选中所有复选框
- java - 如何在 Java 中制作 Material Exposed Drop Down List 必填字段
- salesforce - 内部查询的 SOQL 记录数
- azure - 如何将资源组从禁用的订阅迁移到 Azure 中的工作目录
- react-native - 使用 渲染 iframe 时,Android 在导航回堆栈屏幕时崩溃
- python - 将纯文本从 qTextEdit 复制到 LibreOffice Writer (和)时添加了 html 标签
- angular - Angular-消耗一个再端点,然后在结果中调用另一个端点