[英]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
.我正在制作一个 grogu discord 机器人,当有人使用
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 *更新:重新启动机器人后,它显示不同的图片,但在我重新启动机器人之前它总是一样的
Make sure you're generating a new random image inside if (command === 'comic')
.确保在
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.如果你在
on('message'
侦听器之外声明它,它总是一样的。你可以/应该在它之外有images
数组,但是你需要在每个传入命令上生成一个随机的。
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);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.