Haha-Yes/commands/fun/reddit.js

61 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2018-12-30 01:20:24 +01:00
const { Command } = require('discord-akairo');
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-11-09 12:04:01 +01:00
clientPermissions: ['SEND_MESSAGES', 'EMBED_LINKS'],
2019-01-02 08:09:45 +01:00
args: [
{
id: 'sub',
2019-01-02 18:45:53 +01:00
type: 'string',
2019-06-23 03:41:59 +02:00
prompt: {
start: 'What subreddit do you want to browse?',
optional: true
},
2019-06-16 19:16:16 +02:00
default: 'random',
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) {
//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) => {
console.log(response);
if (response.error == 404)
return message.reply('Not a valid subreddit');
if (response.data.dist == 0)
return message.reply('Not a valid subreddit');
let i = Math.floor((Math.random() * response.data.children.length));
2019-02-10 19:00:00 +01:00
if (response.data.children[i].data.over_18 == true && !message.channel.nsfw)
return message.reply('No nsfw');
2019-11-22 12:30:20 +01:00
const redditEmbed = this.client.util.embed()
2020-03-22 21:54:19 +01:00
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
2019-01-02 08:09:45 +01:00
.setTitle(response.data.children[i].data.title)
.setDescription(response.data.children[i].data.selftext)
2019-01-02 08:09:45 +01:00
.setURL('https://reddit.com' + response.data.children[i].data.permalink)
2019-07-24 02:30:25 +02:00
.setFooter(`/r/${response.data.children[i].data.subreddit} | ⬆ ${response.data.children[i].data.ups} 🗨 ${response.data.children[i].data.num_comments}`);
2019-01-02 08:09:45 +01:00
message.reply(redditEmbed);
message.reply(response.data.children[i].data.url);
2019-01-02 08:09:45 +01:00
});
}
2018-12-30 01:20:24 +01:00
}
module.exports = RedditCommand;