我開始撰寫一個不和諧的機器人,我做了我的第一個命令,那就是擁抱另一個成員。答案是一個動漫擁抱gif。但是現在,我想做的是,不要只設定一個 gif,而是放一些隨機選擇的其他 gif。但是,我不知道如何進行隨機 gif。
const Discord = require("discord.js");
const { UserAgent } = require("discord.js/src/util/Constants");
const ytdl = require("ytdl-core");
const Client = new Discord.Client({
intents : [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES
]
});
const prefix ="<";
Client.on("ready", () => {
console.log("bot opérationnel")
});
Client.on("message", message => {
if (message.author.bot) return;
if (message.content.startsWith (prefix "hug")){
let mention = message.mentions.members.first();
if(mention === undefined){
message.channel.send("u have to hug someone who actually exists");
}
else {
const embed = new Discord.MessageEmbed()
.setColor("DARK_RED")
.setTitle(":hugging: " mention.displayName " recieved a hug from " message.author.username " !")
.setImage("gif.gif");
message.channel.send({embeds: [embed]});
}
}
});
Client.login("token");
uj5u.com熱心網友回復:
所以這將是從一組 gif 影像中選擇一個隨機元素,
const hugs = ['gif1.gif', 'gif2.gif'];
const randomHug = hugs[Math.floor(Math.random() * hugs.length)];
然后你當然會使用變數 randomHug 作為 gif。 .setImage(randomHug);
uj5u.com熱心網友回復:
@Michel Mano 的回答是完美的。
此外,如果您有大量 gif 并且您不想將所有 gif 添加到串列中,您可以將它們另存為gif{num}.gif一個檔案夾,然后僅使用以下命令選擇一個隨機 gif:
const fs = require('fs')
const randomHug = 'gif' Math.floor((Math.random() * fs.readdirSync('gifs_directory').length) 1) '.gif'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/353715.html
