Haha-Yes/commands/fun/reddit.js

55 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-12-30 01:20:24 +01:00
const { Command } = require('discord-akairo');
const { MessageEmbed } = require('discord.js');
2018-12-30 01:20:24 +01:00
const fetch = require('node-fetch');
class RedditCommand extends Command {
2019-01-02 08:09:45 +01:00
constructor() {
super('reddit', {
aliases: ['reddit'],
2019-03-30 04:43:44 +01:00
category: 'fun',
2019-01-02 08:09:45 +01:00
args: [
{
id: 'sub',
2019-01-02 18:45:53 +01:00
type: 'string',
2019-01-10 20:45:29 +01:00
match: 'rest'
2019-01-02 08:09:45 +01:00
}
],
description: {
2018-12-30 01:20:24 +01:00
content: 'Send random images from the subreddit you choose',
usage: '[subreddit]',
examples: ['2meirl4meirl']
}
2019-01-02 08:09:45 +01:00
});
}
2018-12-30 01:20:24 +01:00
2019-01-02 08:09:45 +01:00
async exec(message, args) {
2019-01-15 21:44:45 +01:00
let i = 0;
let a = 0;
2019-05-04 12:42:21 +02:00
if (!args.sub)
return;
2019-01-02 08:09:45 +01:00
2019-05-04 12:42:21 +02:00
fetch('https://www.reddit.com/r/' + args.sub + '.json?limit=100').then((response) => {
2019-01-02 08:09:45 +01:00
return response.json();
}).then((response) => {
if (!response.data)
return message.channel.send('Not a valid subreddit');
while (response.data.children[i].data.post_hint !== 'image') {
i = Math.floor((Math.random() * response.data.children.length));
a++;
if (a == 5)
return message.channel.send('Could not find any images');
}
2019-02-10 19:00:00 +01:00
if (response.data.children[i].data.over_18 == true && !message.channel.nsfw)
2019-01-02 08:09:45 +01:00
return message.channel.send('No nsfw');
2019-01-16 00:29:25 +01:00
const redditEmbed = new MessageEmbed()
.setColor('#ff9900')
2019-01-02 08:09:45 +01:00
.setTitle(response.data.children[i].data.title)
.setImage(response.data.children[i].data.url)
.setURL('https://reddit.com' + response.data.children[i].data.permalink)
2019-05-03 22:43:23 +02:00
.setFooter(`/r/${args.sub} | ⬆ ${response.data.children[i].data.ups}${response.data.children[i].data.num_comments}`);
2019-01-02 08:09:45 +01:00
message.channel.send(redditEmbed);
});
}
2018-12-30 01:20:24 +01:00
}
module.exports = RedditCommand;