Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

386
Views
Discord bot: imágenes aleatorias en un mensaje incrustado

Estoy comenzando a programar un bot de discordia e hice mi primer comando, que es abrazar a otro miembro. La respuesta está junto con un gif de abrazo de anime. Pero ahora, lo que me gustaría hacer es, en lugar de solo configurar un gif, poner algunos otros que se seleccionarían al azar. Sin embargo, no sé cómo proceder con un gif aleatorio.

 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 answers
Answer question

0

Así que esto sería seleccionar un elemento aleatorio de una serie de gifs,

 const hugs = ['gif1.gif', 'gif2.gif']; const randomHug = hugs[Math.floor(Math.random() * hugs.length)];

Entonces, por supuesto, usaría la variable randomHug como gif. .setImage(randomHug);

about 4 years ago · Juan Pablo Isaza Report

0

La respuesta de @Michel Mano es perfecta.

Además, si tiene una gran cantidad de gifs y no desea agregarlos todos a la lista, puede guardarlos como gif{num}.gif en una carpeta y luego seleccionar un gif aleatorio simplemente usando:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!