Get Subreddit Icons in Meme and Subreddit Commands

This commit is contained in:
Dragon Fire
2020-01-16 00:23:08 -05:00
parent fb9ec3619b
commit b56e144a47
4 changed files with 18 additions and 6 deletions
+2 -1
View File
@@ -13,6 +13,7 @@ module.exports = class MemeCommand extends SubredditCommand {
details: `**Subreddits:** ${subreddits.join(', ')}`, details: `**Subreddits:** ${subreddits.join(', ')}`,
clientPermissions: ['ATTACH_FILES'], clientPermissions: ['ATTACH_FILES'],
postType: 'image', postType: 'image',
getIcon: true,
args: [ args: [
{ {
key: 'subreddit', key: 'subreddit',
@@ -29,7 +30,7 @@ module.exports = class MemeCommand extends SubredditCommand {
generateText(post, subreddit) { generateText(post, subreddit) {
return new MessageEmbed() return new MessageEmbed()
.setColor(0xFF4500) .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) .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 -1
View File
@@ -11,6 +11,7 @@ module.exports = class SubredditCommand extends SubredditCommandBase {
memberName: 'subreddit', memberName: 'subreddit',
description: 'Responds with a random post from a subreddit.', description: 'Responds with a random post from a subreddit.',
clientPermissions: ['EMBED_LINKS'], clientPermissions: ['EMBED_LINKS'],
getIcon: true,
args: [ args: [
{ {
key: 'subreddit', key: 'subreddit',
@@ -25,7 +26,7 @@ module.exports = class SubredditCommand extends SubredditCommandBase {
generateText(post, subreddit) { generateText(post, subreddit) {
return new MessageEmbed() return new MessageEmbed()
.setColor(0xFF4500) .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) .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}`)
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "xiao", "name": "xiao",
"version": "110.0.1", "version": "110.0.2",
"description": "Your personal server companion.", "description": "Your personal server companion.",
"main": "Xiao.js", "main": "Xiao.js",
"scripts": { "scripts": {
+13 -3
View File
@@ -7,6 +7,7 @@ module.exports = class SubredditCommand extends Command {
this.subreddit = info.subreddit; this.subreddit = info.subreddit;
this.postType = info.postType ? Array.isArray(info.postType) ? info.postType : [info.postType] : null; this.postType = info.postType ? Array.isArray(info.postType) ? info.postType : [info.postType] : null;
this.getIcon = info.getIcon || false;
this.credit.push({ this.credit.push({
name: 'Reddit', name: 'Reddit',
url: 'https://www.reddit.com/', url: 'https://www.reddit.com/',
@@ -18,7 +19,7 @@ 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); const post = await this.random(subreddit, msg.channel.nsfw, this.getIcon);
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));
} catch (err) { } catch (err) {
@@ -32,7 +33,8 @@ 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) { async random(subreddit, nsfw, getIcon = false) {
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`)
.query({ limit: 100 }); .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; 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.getIcon(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
}; };
} }
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;
}
}; };