简体   繁体   English

如何从我的本地文件中获取随机图像并将其放置在 Discord.js 中的嵌入中

[英]How do I get a Random Image from my Local file and place it on Embed in Discord.js

How do I make my attachment look from each files in ./src/ that has starts with pingping and would randomly choose an image for example: pingping2.gif I tried doing something like我如何从有开始与./src/每个文件我的执着外观pingping ,并会随机选择的图像。例如: pingping2.gif我试图做这样的事情

const rand = Math.floor(Math.random() * 5); //I got 5 because I have 5 pingping[].gif files. I dont know how it could count how many files startsWith('pingping')..
attachment = new Discord.MessageAttachment(`./src/pingping${rand}.gif`);

for some reason it only shows the last image from all the files instead of choosing randomly.. Here is the whole file -->出于某种原因,它只显示所有文件中的最后一个图像,而不是随机选择.. 这是整个文件 -->

 attachment = new Discord.MessageAttachment('./src/pingping1.gif');
const { MessageEmbed } = require("discord.js");
module.exports = {
    name: 'ping',
    cooldown: 5,
    description: 'Just a Ping Command',
    execute(message, args){
        let targetMember = message.mentions.members.first();        
        if(!args[0])
        return message.channel.send('Pong!');
        else{
            let pingEmbed = new MessageEmbed()
                .setDescription(`Ring Ring ${targetMember}!!, ${message.author.username} is Pinging you!!`)
                .setColor("#EB338B")
                .attachFiles(attachment)
                .setImage('attachment://pingping1.gif')
            message.channel.send(pingEmbed);
        }
    }
}

I have a command in my discord bot that sends random gifs the only downside is that you have to use direct links (honestly im not sure about that but didnt try it with files)我的 discord bot 中有一个命令可以发送随机 gif,唯一的缺点是您必须使用直接链接(老实说,我不确定这一点,但没有尝试使用文件)

const randomMessage = messages[Math.floor(Math.random() * messages.length)] and then add the direct links example: const messages = [`link 1,`link 2`,`link 3`] then it should send a random link const randomMessage = messages[Math.floor(Math.random() * messages.length)]然后添加直接链接示例: const messages = [`link 1,`link 2`,`link 3`]然后它应该发送随机链接

This is how i do it on a bot that sends an image every hour这就是我如何在每小时发送图像的机器人上执行此操作

var fs = require('fs');
var files = fs.readdirSync('./src');
let chosenFile = files[Math.floor(Math.random() * files.length)]
if (chosenFile.contains("pingping")) {
    message.channel.send({ files: [{ attachment: `./src/${chosenFile}` }] }
}

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

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