Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

397
Visualizações
Discord bot : random images in an embed message

I'm beginning programming a discord bot, and I did my first command which is to hug another member. The answer is along with an anime hug gif. But now, what I would like to do is, instead of only setting one gif, to put on some others which would be selected randomly. However, I don't know how to proceed to a random 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");
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

So this would be selecting a random element from an array of gifs,

const hugs = ['gif1.gif', 'gif2.gif'];

const randomHug = hugs[Math.floor(Math.random() * hugs.length)];

You would then of course use the variable randomHug as the gif. .setImage(randomHug);

about 4 years ago · Juan Pablo Isaza Relatório

0

@Michel Mano answer is perfect.

Also, if you have a large number of gifs and you don't want to add all on the list, you can save them as gif{num}.gif on a folder, and then select a random gif just using:


const fs = require('fs')
 
const randomHug = 'gif' + Math.floor((Math.random() * fs.readdirSync('gifs_directory').length) + 1) + '.gif'
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda