I'm trying to make random meme command that sends meme's from r/reddit, but i have an error
const { MessageEmbed } = require("discord.js");
const fetch = require("node-fetch");
module.exports = {
name: "meme",
description: "This command sends you random memes",
category: "fun",
example: ["x!meme"],
callback: async ({ message }) => {
try {
const url = await fetch("https://www.reddit.com/r/memes/random/.json");
const random = await url.json();
const embed = new MessageEmbed()
.setTitle(`Random Meme | ${random[0].data.children.data.title}`)
.setImage(random[0].data.children.data.url)
.setColor("#FF00A6");
.setFooter("Prefix x! | Random Meme Generator")
await message.channel.send({ embeds: [embed] });
} catch (err) {
console.log(err);
}
},
};
I don't know why but I get this error
On the older version (discord.js v12) it worked normally
TypeError: Cannot read properties of undefined (reading 'title')
After visiting the link, it seems like random[0].data.children is an array. Get the first element from it:
.setTitle(`Random Meme | ${random[0].data.children[0].data.title}`)
You will need to do the same with the image:
.setImage(random[0].data.children[0].data.url)