This commit is contained in:
Dragon Fire
2020-01-16 00:31:49 -05:00
parent 4c59c31319
commit b18dbee1e9
3 changed files with 9 additions and 9 deletions
+2 -2
View File
@@ -27,10 +27,10 @@ module.exports = class MemeCommand extends SubredditCommand {
}); });
} }
generateText(post, subreddit) { generateText(post, subreddit, icon) {
return new MessageEmbed() return new MessageEmbed()
.setColor(0xFF4500) .setColor(0xFF4500)
.setAuthor(`r/${subreddit}`, post.icon, `https://www.reddit.com/r/${subreddit}/`) .setAuthor(`r/${subreddit}`, icon, `https://www.reddit.com/r/${subreddit}/`)
.setTitle(post.title) .setTitle(post.title)
.setImage(post.post_hint === 'image' ? post.url : null) .setImage(post.post_hint === 'image' ? post.url : null)
.setURL(`https://www.reddit.com${post.permalink}`) .setURL(`https://www.reddit.com${post.permalink}`)
+2 -2
View File
@@ -23,10 +23,10 @@ module.exports = class SubredditCommand extends SubredditCommandBase {
}); });
} }
generateText(post, subreddit) { generateText(post, subreddit, icon) {
return new MessageEmbed() return new MessageEmbed()
.setColor(0xFF4500) .setColor(0xFF4500)
.setAuthor(`r/${subreddit}`, post.icon, `https://www.reddit.com/r/${subreddit}/`) .setAuthor(`r/${subreddit}`, icon, `https://www.reddit.com/r/${subreddit}/`)
.setTitle(post.title) .setTitle(post.title)
.setImage(post.post_hint === 'image' ? post.url : null) .setImage(post.post_hint === 'image' ? post.url : null)
.setURL(`https://www.reddit.com${post.permalink}`) .setURL(`https://www.reddit.com${post.permalink}`)
+5 -5
View File
@@ -19,9 +19,9 @@ module.exports = class SubredditCommand extends Command {
async run(msg, { subreddit }) { async run(msg, { subreddit }) {
if (!subreddit) subreddit = typeof this.subreddit === 'function' ? this.subreddit() : this.subreddit; if (!subreddit) subreddit = typeof this.subreddit === 'function' ? this.subreddit() : this.subreddit;
try { try {
const post = await this.random(subreddit, msg.channel.nsfw, this.getIcon); const post = await this.random(subreddit, msg.channel.nsfw);
if (!post) return msg.reply('Could not find any results.'); if (!post) return msg.reply('Could not find any results.');
return msg.say(this.generateText(post.post, post.origin)); return msg.say(this.generateText(post.post, post.origin, post.icon));
} catch (err) { } catch (err) {
if (err.status === 403) return msg.say('This subreddit is private.'); if (err.status === 403) return msg.say('This subreddit is private.');
if (err.status === 404) return msg.say('Could not find any results.'); if (err.status === 404) return msg.say('Could not find any results.');
@@ -33,7 +33,7 @@ module.exports = class SubredditCommand extends Command {
throw new Error('The generateText method is required.'); throw new Error('The generateText method is required.');
} }
async random(subreddit, nsfw, getIcon = false) { async random(subreddit, nsfw) {
let icon = null; let icon = null;
const { body } = await request const { body } = await request
.get(`https://www.reddit.com/r/${subreddit}/hot.json`) .get(`https://www.reddit.com/r/${subreddit}/hot.json`)
@@ -45,11 +45,11 @@ module.exports = class SubredditCommand extends Command {
return (this.postType ? this.postType.includes(post.data.post_hint) : true) && post.data.url && post.data.title; return (this.postType ? this.postType.includes(post.data.post_hint) : true) && post.data.url && post.data.title;
}); });
if (!posts.length) return null; if (!posts.length) return null;
if (getIcon) icon = await this.fetchIcon(subreddit); if (this.getIcon) icon = await this.fetchIcon(subreddit);
return { return {
origin: subreddit, origin: subreddit,
post: posts[Math.floor(Math.random() * posts.length)].data, post: posts[Math.floor(Math.random() * posts.length)].data,
icon: icon icon
}; };
} }