I'm trying to make a discord meme bot (in this case, cute animal pictures bot). The code is this;

But when I use the command in discord, the reply only shows the description, not the color or image.
What am I doing wrong?
here's the code of the command file for copy paste;
const { MessageEmbed } = require('discord.js');
const randomPuppy = require('random-puppy');
module.exports = {
name: 'cute',
description: 'Embeds pictures pulled from listed subreddits',
execute(message, args, Discord){
let reddit = [
"aww",
"puppies",
"toebeans"
]
let subreddit = reddit[Math.floor(Math.random()*reddit.length -1)];
const cuteEmbed = new MessageEmbed()
.setDescription("Some cute animals to blow away your anxieties!");
randomPuppy(subreddit).then(url => {
console.log(url);
const cuteurl = url;
cuteEmbed.setColor('#91B2C7');
cuteEmbed.setImage('${cuteurl}');
});
message.channel.send(cuteEmbed);
}
}
please help :'(
Edit: BACK TICKS. GODDAMN BACK TICKS. I'm using a new code so idk if using back ticks would've fixed it, but that's one mistake in the code; I didn't use backticks for interpolation.
The method randomPuppy() is asynchronous. Meaning that you need to wait for the promise to send the embed message. In your current code, you send the embed without waiting for the request to complete.
You have to change your code from:
randomPuppy(subreddit).then(url => {
cuteEmbed.setColor('#91B2C7');
cuteEmbed.setImage('${url}');
});
message.channel.send(cuteEmbed);
To:
randomPuppy(subreddit).then(url => {
cuteEmbed.setColor('#91B2C7');
cuteEmbed.setImage('${url}');
message.channel.send(cuteEmbed); //Send the embed once the request is completed.
});
randomPuppy(subreddit).then(url => {
console.log(url);
const cuteurl = url;
cuteEmbed.setColor('#91B2C7');
cuteEmbed.setImage('${cuteurl}');
});
message.channel.send(cuteEmbed);
}
You are trying to send the embed even the request is not finished, all you need to do is to put the message.channel.send() inside of .then
randomPuppy(subreddit).then(url => {
console.log(url);
const cuteurl = url;
cuteEmbed.setColor('#91B2C7');
cuteEmbed.setImage('${cuteurl}');
message.channel.send(cuteEmbed);
});
}
And I noticed something that you missed type one of the keypress you should type backquote instead of quote, so after editing your code. You just need to edit the quote to backquote:
randomPuppy(subreddit).then(url => {
console.log(url);
const cuteurl = url;
cuteEmbed.setColor('#91B2C7');
cuteEmbed.setImage('${cuteurl}'); //This is the part you need to change it
message.channel.send(cuteEmbed);
});
}
To this:
randomPuppy(subreddit).then(url => {
console.log(url);
const cuteurl = url; //image should call this after changing it
cuteEmbed.setColor('#91B2C7');
cuteEmbed.setImage(`${cuteurl}`); //To this, to call your cuteurl
message.channel.send(cuteEmbed);
});
}