Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

javascript - discord.js random image is always the same

I'm making a grogu discord bot and want to send a random comic picture in an embed, when someone uses g!comic.

So far I have this:

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'ping'){
        client.commands.get('ping').execute(message, args);
    }else if (command === 'comic') {    
        const random = new Discord.MessageEmbed()
          .setTitle('Here is your random grogu comic')
          .setImage(image)
    
        message.channel.send(random);
    }
});

The problem is, when I test it, it always sends the same picture/link and I don't know how to fix it.

This is where the images are stored:

const images = ["https://i.pinimg.com/originals/4d/2e/f3/4d2ef3052fab7b613733f56cd224118b.jpg", "https://red.elbenwald.de/media/image/a5/f8/e9/E1064005_1_600x600.jpg", "https://i.pinimg.com/736x/29/43/39/2943397229b5fb42cf12e8a1302d1821.jpg", "https://i.kym-cdn.com/photos/images/original/001/755/387/09f.jpg" ];
const image = images[Math.floor(Math.random() * images.length)];

*update: after restarting the bot, it shows a different picture, but it posts always the same until I restart the bot

question from:https://stackoverflow.com/questions/65936266/discord-js-random-image-is-always-the-same

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Make sure you're generating a new random image inside if (command === 'comic'). If you declare it outside the on('message' listener, it's always the same. You can/should have the images array outside of it, but you need to generate a random one on every incoming command.

const images = [
  'https://i.pinimg.com/originals/4d/2e/f3/4d2ef3052fab7b613733f56cd224118b.jpg',
  'https://red.elbenwald.de/media/image/a5/f8/e9/E1064005_1_600x600.jpg',
  'https://i.pinimg.com/736x/29/43/39/2943397229b5fb42cf12e8a1302d1821.jpg',
  'https://i.kym-cdn.com/photos/images/original/001/755/387/09f.jpg',
];

client.on('message', (message) => {
  if (!message.content.startsWith(prefix) || message.author.bot) return;

  const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();

  if (command === 'ping') {
    client.commands.get('ping').execute(message, args);
  } else if (command === 'comic') {
    const image = images[Math.floor(Math.random() * images.length)];
    const random = new Discord.MessageEmbed()
      .setTitle('Here is your random grogu comic')
      .setImage(image);

    message.channel.send(random);
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...