mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
Get Subreddit Icons in Meme and Subreddit Commands
This commit is contained in:
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user