SubredditCommand structure

This commit is contained in:
Daniel Odendahl Jr
2018-09-21 11:06:07 +00:00
parent 92b9ea1fff
commit f58f677e12
6 changed files with 75 additions and 66 deletions
+8 -24
View File
@@ -1,8 +1,7 @@
const Command = require('../../structures/Command');
const request = require('node-superfetch');
const SubredditCommand = require('../../structures/commands/Subreddit');
const { stripIndents } = require('common-tags');
module.exports = class RedditCommand extends Command {
module.exports = class RedditCommand extends SubredditCommand {
constructor(client) {
super(client, {
name: 'reddit',
@@ -21,27 +20,12 @@ module.exports = class RedditCommand extends Command {
});
}
async run(msg, { subreddit }) {
try {
const { body } = await request
.get(`https://www.reddit.com/r/${subreddit}/new.json`)
.query({
sort: 'new',
limit: 100
});
const posts = body.data.children.filter(post => post.data && (msg.channel.nsfw ? true : !post.data.over_18));
if (!posts.length) return msg.say('Could not find any results.');
const post = posts[Math.floor(Math.random() * posts.length)].data;
return msg.say(stripIndents`
**${post.title}**
<https://www.reddit.com${post.permalink}>
generateText(post, subreddit) {
return stripIndents`
**r/${subreddit}** ${post.title}
${post.url}
${post.ups}${post.downs}
`);
} catch (err) {
if (err.status === 403) return msg.say('This subreddit is private.');
if (err.status === 404) return msg.say('Could not find any results.');
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
${post.ups}${post.downs}
`;
}
};