bluebeargreen
bluebeargreen

Reputation: 1

Randompuppys wont get images off of reddit on discord.js

I am trying to get a Discord.js bot to grab images from Reddit and post them in a channel but It keeps saying that there is no content to send, I was wondering if someone could point out what I did wrong.

(I am using a command handler)

Code:

const randomPuppy = require('random-puppy');
const snekfetch = require('snekfetch');

module.exports = {
    name: "reddit",
    category: "info",
    description: "Sends subreddit images",
    run: async (client, Message, args, subreddit) => {
        let reddit = [
            "dankmemes",
            "meme"
          ]

          randomPuppy(subreddit).then(url => {
              snekfetch.get(url).then(async res => {
                  await Message.channel.send({
                      file: [{
                          attachment: res.body,
                          name: 'image.png'
                      }]
                  });
              }).catch(err => console.error(err));
          });
    }
}

Upvotes: 0

Views: 906

Answers (1)

Lauren Yim
Lauren Yim

Reputation: 14098

  • When I tried using random-puppy to get images from r/dankmemes, a lot of them were GIFs or videos. Make sure you're using the right extension and increasing the restRequestTimeout to allow time for the file to send.
  • The property file in Discord.js' MessageOptions was deprecated in v11 and was removed in v12. You should use files instead.
  • snekfetch is deprecated and you should use node-fetch instead.

Use this to initialise your client which will set the timeout to 1 minute instead of the default 15 seconds:

const client = new Client({restRequestTimeout: 60000})

In your command file:

// returns .jpg, .png, .gif, .mp4 etc
// for more info see https://nodejs.org/api/path.html#path_path_extname_path
const {extname} = require('path')
const fetch = require('node-fetch')

/* ... */

// in your run function
// you can use thens if you want but I find this easier to read
try {
  const url = await randomPuppy(subreddit)
  const res = await fetch(url)
  await Message.channel.send({
    files: [{
      attachment: res.body,
      name: `image${extname(url)}`
    }]
  })
} catch (err) {
  console.error(err)
}

Upvotes: 1

Related Questions