Reputation: 1
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
Reputation: 14098
restRequestTimeout
to allow time for the file to send.file
in Discord.js' MessageOptions
was deprecated in v11 and was removed in v12. You should use files
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