i was making meme.js file/command and i got this error idk how to resolve this problem, Can you help me?
so this is my code:
const fetch = require('node-fetch');
const randomPuppy = require("random-puppy");
module.exports = {
name: "meme",
description: "Sends an epic meme",
execute(message) {
const subReddits = ["dankmeme", "meme", "me_irl"];
const random = subReddits[Math.floor(Math.random() * subReddits.length)];
const img = await randomPuppy(random);
console.log(img)
const memeEmbed = new MessageEmbed()
.setColor("#fa0557")
// .setImage(`${img}`)
.setTitle(`From /r/${random}`)
.setURL(`https://reddit.com/r/${random}`);
message.channel.send({ embeds: [memeEmbed] });
}
}
And this is the log i got:
Promise { <pending> }
CodePudding user response:
The posted code throws a syntax error that await
can only be used inside an async function - which `module.exports.execute' has not been declared as.
Try declaring execute
as an async function
module.exports = {
name: "meme",
description: "Sends an epic meme",
async execute(message) { . . .
If the log in the post does show
Promise { <pending> }
on the console, please update the post with necessary code to reproduce the problem.
CodePudding user response:
Your code as shown will not run. You show await randomPuppy(random)
, but await
is NOT allowed there because it's not inside an async
function. So, I suspect that your real code does not have the await
and that's why you show Promise { <pending> }
in your console. You have to use either await
or .then()
on the promise that randomPuppy(random)
returns.
If I fix your code so that it will run by adding async
to the execute()
definition, then the console.log(img)
actually works just fine:
const randomPuppy = require("random-puppy");
module.exports = {
name: "meme",
description: "Sends an epic meme",
async execute(message) {
const subReddits = ["dankmeme", "meme", "me_irl"];
const random = subReddits[Math.floor(Math.random() * subReddits.length)];
const img = await randomPuppy(random);
console.log(img)
}
}
module.exports.execute().then(result => {
console.log(result);
}).catch(err => {
console.log(err);
});
And, I get in my console this:
http://imgur.com/FFkL628.gif
done
So, my conclusion is that the code you posted in your question is not the code you're actually running. This code shown here will work just fine.