#Problem with Reddit API

10 messages · Page 1 of 1 (latest)

west wave
#

When I run the /meme command, the console replies
Unexpected token '<', "<!doctype "... is not a valid JSON
(PS: I use a hosting company)

Here's my meme.js

const { EmbedBuilder } = require('discord.js');

module.exports = {
name: 'meme',
description: 'Donne un meme!',
callback: async (client, interaction) => {

    async function meme() {
        try {
            const response = await fetch('https://www.reddit.com/r/memes/random/.json');
            const memeData = await response.json();

            const title = memeData[0].data.children[0].data.title;
            const image = memeData[0].data.children[0].data.url;
            const author = memeData[0].data.children[0].data.author;

            const embed = new EmbedBuilder()
                .setColor("Random")
                .setTitle(title)
                .setImage(image)
                .setURL(image)
                .setFooter(author);

            await interaction.reply({ embeds: [embed] });
        } catch (error) {
            console.error('Erreur lors de la récupération du meme:', error.message);
            await interaction.reply("Une erreur s'est produite lors de la récupération du meme.");
        }
    }

    meme();
},

};

whole hareBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
wraith stirrup
#

random/.json seems like a typo…

#

So you probably get a 404 HTML page as result instead of actual json

west wave
#

And do you know how to fix that

wraith stirrup
#

By fixing the typo in the filename? Not djs related at all…

west wave
#

there's no typo in the filename it's just the apostrophe at the end

rare sinew
#

inspect the response body

#

you probably need to be auth'd

#

but yeah it's not really a djs thing