Haha-Yes/commands/fun/reddit.js

48 lines
2 KiB
JavaScript
Raw Normal View History

2018-09-27 19:04:42 +02:00
const { Command } = require('discord.js-commando');
const Discord = require('discord.js');
2018-09-28 17:57:00 +02:00
const fetch = require('node-fetch');
2018-09-27 19:06:41 +02:00
module.exports = class redditCommand extends Command {
2018-09-27 19:04:42 +02:00
constructor(client) {
super(client, {
name: 'reddit',
group: 'fun',
memberName: 'reddit',
description: `Show a random images from the subreddit you choose`,
args: [
{
key: 'sub',
prompt: 'Wich subreddit would you like to see?',
type: 'string',
}
]
});
}
async run(message, { sub }) {
let /* the bodies hit the floor */ i = Math.floor((Math.random() * 10) + 1);
let a = 0
2018-09-28 17:57:00 +02:00
fetch('https://www.reddit.com/r/' + sub + '.json?limit=100').then((response) => {
return response.json();
2018-09-28 18:04:31 +02:00
}).then((response) => {
2018-09-28 17:57:00 +02:00
if (!response.data)
return message.say('Not a valid subreddit')
while (response.data.children[i].data.post_hint !== 'image') {
2018-09-28 18:04:31 +02:00
i = Math.floor((Math.random() * response.data.children.length));
2018-09-28 17:57:00 +02:00
a++
if (a == 5)
return message.say("Could not find any images")
}
if (response.data.children[i].data.over_18 == true)
return message.say("No nsfw ( if you want a nsfw version of this commands use the feedback commands \"haha feedback <your feedback>\")")
const redditEmbed = new Discord.RichEmbed()
.setColor("#ff9900")
.setTitle(response.data.children[i].data.title)
.setImage(response.data.children[i].data.url)
.setURL('https://reddit.com' + response.data.children[i].data.permalink)
.setFooter(`${response.data.children[i].data.ups} 💬 ${response.data.children[i].data.num_comments}`)
message.say(redditEmbed);
}
)}}