Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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: });
        }
    }
});

Client.login("token");

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading