Guacamole15
Guacamole15

Reputation: 39

Problem with Reddit command on discord.js bot

I am trying to put a cursed image command in my discord.js bot but it keeps coming up with this error

got('https://www.reddit.com/r/madlads/random/.json').then(response => {
^

ReferenceError: got is not defined

Here is the code

if(command === "cursed") {

        got('https://www.reddit.com/r/cursedimages/random/.json').then(response => {
                let content = JSON.parse(response.body);
            var image = content[0].data.children[0].data.url;
            let permalink = content[0].data.children[0].data.permalink;
            let memeUrl = `https://reddit.com${permalink}`;
            let memeImage = content[0].data.children[0].data.url;
            let memeTitle = content[0].data.children[0].data.title;
            let memeUpvotes = content[0].data.children[0].data.ups;
            let memeNumComments = content[0].data.children[0].data.num_comments;
            const cursedembed = new Discord.MessageEmbed()
                .setColor('RANDOM')
                .setTitle(content[0].data.children[0].data.title)
                .setURL(`${memeUrl}`)
                .setImage(image)
                .setFooter(`👍 ${memeUpvotes} 💬 ${memeNumComments}`)
                .setTimestamp()

            message.channel.send(cursedembed);
        });
}

Upvotes: 0

Views: 179

Answers (1)

Axiumin_
Axiumin_

Reputation: 2145

Since you don't have node-fetch as one of your packages, you won't be able to use the fetch function. In order to use this package, you'll have to follow three steps.

  1. Install node-fetch. This can be done by running npm i node-fetch in your project directory.
  2. Import node-fetch into your code. This can be done by having const fetch = require('node-fetch'); at the top of your code file, similar to your discord.js import.
  3. Replace got with fetch.

After that, you should be able to use fetch in order to download web content and post reddit content on discord.

Upvotes: 1

Related Questions