From b56e144a47e08bbb90572e7ba175b54baa46d72d Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Thu, 16 Jan 2020 00:23:08 -0500 Subject: [PATCH] Get Subreddit Icons in Meme and Subreddit Commands --- commands/random/meme.js | 3 ++- commands/random/subreddit.js | 3 ++- package.json | 2 +- structures/commands/Subreddit.js | 16 +++++++++++++--- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/commands/random/meme.js b/commands/random/meme.js index 89aee31c..0920076f 100644 --- a/commands/random/meme.js +++ b/commands/random/meme.js @@ -13,6 +13,7 @@ module.exports = class MemeCommand extends SubredditCommand { details: `**Subreddits:** ${subreddits.join(', ')}`, clientPermissions: ['ATTACH_FILES'], postType: 'image', + getIcon: true, args: [ { key: 'subreddit', @@ -29,7 +30,7 @@ module.exports = class MemeCommand extends SubredditCommand { generateText(post, subreddit) { return new MessageEmbed() .setColor(0xFF4500) - .setAuthor(`r/${subreddit}`, 'https://i.imgur.com/DSBOK0P.png', `https://www.reddit.com/r/${subreddit}/`) + .setAuthor(`r/${subreddit}`, post.icon, `https://www.reddit.com/r/${subreddit}/`) .setTitle(post.title) .setImage(post.post_hint === 'image' ? post.url : null) .setURL(`https://www.reddit.com${post.permalink}`) diff --git a/commands/random/subreddit.js b/commands/random/subreddit.js index 12c46e17..7f9b84a2 100644 --- a/commands/random/subreddit.js +++ b/commands/random/subreddit.js @@ -11,6 +11,7 @@ module.exports = class SubredditCommand extends SubredditCommandBase { memberName: 'subreddit', description: 'Responds with a random post from a subreddit.', clientPermissions: ['EMBED_LINKS'], + getIcon: true, args: [ { key: 'subreddit', @@ -25,7 +26,7 @@ module.exports = class SubredditCommand extends SubredditCommandBase { generateText(post, subreddit) { return new MessageEmbed() .setColor(0xFF4500) - .setAuthor(`r/${subreddit}`, 'https://i.imgur.com/DSBOK0P.png', `https://www.reddit.com/r/${subreddit}/`) + .setAuthor(`r/${subreddit}`, post.icon, `https://www.reddit.com/r/${subreddit}/`) .setTitle(post.title) .setImage(post.post_hint === 'image' ? post.url : null) .setURL(`https://www.reddit.com${post.permalink}`) diff --git a/package.json b/package.json index 88350ac6..5465772d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "110.0.1", + "version": "110.0.2", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/structures/commands/Subreddit.js b/structures/commands/Subreddit.js index 49e83559..a16d4757 100644 --- a/structures/commands/Subreddit.js +++ b/structures/commands/Subreddit.js @@ -7,6 +7,7 @@ module.exports = class SubredditCommand extends Command { this.subreddit = info.subreddit; this.postType = info.postType ? Array.isArray(info.postType) ? info.postType : [info.postType] : null; + this.getIcon = info.getIcon || false; this.credit.push({ name: 'Reddit', url: 'https://www.reddit.com/', @@ -18,7 +19,7 @@ module.exports = class SubredditCommand extends Command { async run(msg, { subreddit }) { if (!subreddit) subreddit = typeof this.subreddit === 'function' ? this.subreddit() : this.subreddit; try { - const post = await this.random(subreddit, msg.channel.nsfw); + const post = await this.random(subreddit, msg.channel.nsfw, this.getIcon); if (!post) return msg.reply('Could not find any results.'); return msg.say(this.generateText(post.post, post.origin)); } catch (err) { @@ -32,7 +33,8 @@ module.exports = class SubredditCommand extends Command { throw new Error('The generateText method is required.'); } - async random(subreddit, nsfw) { + async random(subreddit, nsfw, getIcon = false) { + let icon = null; const { body } = await request .get(`https://www.reddit.com/r/${subreddit}/hot.json`) .query({ limit: 100 }); @@ -43,9 +45,17 @@ module.exports = class SubredditCommand extends Command { return (this.postType ? this.postType.includes(post.data.post_hint) : true) && post.data.url && post.data.title; }); if (!posts.length) return null; + if (getIcon) icon = await this.getIcon(subreddit); return { origin: subreddit, - post: posts[Math.floor(Math.random() * posts.length)].data + post: posts[Math.floor(Math.random() * posts.length)].data, + icon: icon }; } + + async getIcon(subreddit) { + const { body } = await request.get(`https://www.reddit.com/r/${subreddit}/about.json`); + if (!body.data.icon_img) return 'https://i.imgur.com/DSBOK0P.png'; + return body.data.icon_img; + } };