You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Haha-Yes/commands/fun/reddit.js

55 lines
1.6 KiB
JavaScript

const { Command } = require('discord-akairo');
const { MessageEmbed } = require('discord.js');
const fetch = require('node-fetch');
class RedditCommand extends Command {
6 years ago
constructor() {
super('reddit', {
aliases: ['reddit'],
category: 'fun',
6 years ago
args: [
{
id: 'sub',
type: 'string',
match: 'rest'
6 years ago
}
],
description: {
content: 'Send random images from the subreddit you choose',
usage: '[subreddit]',
examples: ['2meirl4meirl']
}
6 years ago
});
}
6 years ago
async exec(message, args) {
let i = 0;
let a = 0;
if (!args.sub)
return;
6 years ago
fetch('https://www.reddit.com/r/' + args.sub + '.json?limit=100').then((response) => {
6 years ago
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');
}
if (response.data.children[i].data.over_18 == true && !message.channel.nsfw)
6 years ago
return message.channel.send('No nsfw');
const redditEmbed = new MessageEmbed()
.setColor('#ff9900')
6 years ago
.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(`/r/${args.sub} | ⬆ ${response.data.children[i].data.ups}${response.data.children[i].data.num_comments}`);
6 years ago
message.channel.send(redditEmbed);
});
}
}
module.exports = RedditCommand;