Haha-Yes/commands/fun/reddit.js

43 lines
1.8 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');
const snekfetch = require('snekfetch');
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',
default: 'random',
}
]
});
}
async run(message, { sub }) {
const { body } = await snekfetch.get('https://www.reddit.com/r/' + sub + '.json');
let /* the bodies hit the floor */ i = Math.floor((Math.random() * 10) + 1);
if (!body.data.children[1]) {
2018-09-27 20:27:34 +02:00
return message.say('Not a valid subreddit')
}
2018-09-27 19:04:42 +02:00
while (body.data.children[i].data.post_hint !== 'image') {
2018-09-27 20:27:34 +02:00
i = Math.floor((Math.random() * 20) + 1);
2018-09-27 19:04:42 +02:00
}
2018-09-27 19:06:28 +02:00
if (body.data.children[i].data.over_18 == true) {
2018-09-27 22:49:48 +02:00
return message.say("No nsfw ( if you want a nsfw version of this commands use the feedback commands \"haha feedback <your feedback>\")")
2018-09-27 19:04:42 +02:00
}
2018-09-27 19:11:14 +02:00
const redditEmbed = new Discord.RichEmbed()
2018-09-27 19:04:42 +02:00
.setColor("#ff9900")
.setTitle(body.data.children[i].data.title)
2018-09-27 22:58:16 +02:00
.setImage(body.data.children[i].data.url)
.setURL('https://reddit.com' + body.data.children[i].data.permalink)
.setFooter(`${body.data.children[i].data.ups} 💬 ${body.data.children[i].data.num_comments}`)
2018-09-27 19:04:42 +02:00
2018-09-27 19:11:14 +02:00
message.say(redditEmbed);
2018-09-27 19:04:42 +02:00
}
}