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:
@@ -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}`)
|
||||||
|
|||||||
@@ -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
@@ -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": {
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user