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

61 lines
1.8 KiB
JavaScript

const { Command } = require('discord-akairo');
const fetch = require('node-fetch');
class RedditCommand extends Command {
5 years ago
constructor() {
super('reddit', {
aliases: ['reddit'],
category: 'fun',
5 years ago
clientPermissions: ['SEND_MESSAGES', 'EMBED_LINKS'],
5 years ago
args: [
{
id: 'sub',
type: 'string',
5 years ago
prompt: {
start: 'What subreddit do you want to browse?',
optional: true
},
default: 'random',
match: 'rest'
5 years ago
}
],
description: {
content: 'Send random images from the subreddit you choose',
usage: '[subreddit]',
examples: ['2meirl4meirl']
}
5 years ago
});
}
5 years ago
async exec(message, args) {
//let i = 0;
//let a = 0;
if (!args.sub)
return;
5 years ago
fetch('https://www.reddit.com/r/' + args.sub + '.json?limit=100').then((response) => {
5 years ago
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));
if (response.data.children[i].data.over_18 == true && !message.channel.nsfw)
return message.reply('No nsfw');
const redditEmbed = this.client.util.embed()
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
5 years ago
.setTitle(response.data.children[i].data.title)
.setDescription(response.data.children[i].data.selftext)
5 years ago
.setURL('https://reddit.com' + response.data.children[i].data.permalink)
.setFooter(`/r/${response.data.children[i].data.subreddit} | ⬆ ${response.data.children[i].data.ups} 🗨 ${response.data.children[i].data.num_comments}`);
5 years ago
message.reply(redditEmbed);
message.reply(response.data.children[i].data.url);
5 years ago
});
}
}
module.exports = RedditCommand;