mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-24 02:15:10 +02:00
SubredditCommand structure
This commit is contained in:
+8
-22
@@ -1,10 +1,9 @@
|
|||||||
const Command = require('../../structures/Command');
|
const SubredditCommand = require('../../structures/commands/Subreddit');
|
||||||
const request = require('node-superfetch');
|
|
||||||
const { stripIndents } = require('common-tags');
|
const { stripIndents } = require('common-tags');
|
||||||
const { list } = require('../../util/Util');
|
const { list } = require('../../util/Util');
|
||||||
const subreddits = require('../../assets/json/meme');
|
const subreddits = require('../../assets/json/meme');
|
||||||
|
|
||||||
module.exports = class MemeCommand extends Command {
|
module.exports = class MemeCommand extends SubredditCommand {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
super(client, {
|
super(client, {
|
||||||
name: 'meme',
|
name: 'meme',
|
||||||
@@ -13,6 +12,7 @@ module.exports = class MemeCommand extends Command {
|
|||||||
description: 'Responds with a random meme.',
|
description: 'Responds with a random meme.',
|
||||||
details: `**Subreddits:** ${subreddits.join(', ')}`,
|
details: `**Subreddits:** ${subreddits.join(', ')}`,
|
||||||
clientPermissions: ['ATTACH_FILES'],
|
clientPermissions: ['ATTACH_FILES'],
|
||||||
|
postType: 'image',
|
||||||
args: [
|
args: [
|
||||||
{
|
{
|
||||||
key: 'subreddit',
|
key: 'subreddit',
|
||||||
@@ -26,24 +26,10 @@ module.exports = class MemeCommand extends Command {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(msg, { subreddit }) {
|
generateText(post, subreddit) {
|
||||||
try {
|
return stripIndents`
|
||||||
const { body } = await request
|
**r/${subreddit}** ${post.title}
|
||||||
.get(`https://www.reddit.com/r/${subreddit}/top.json`)
|
${post.url}
|
||||||
.query({
|
`;
|
||||||
sort: 'top',
|
|
||||||
t: 'day',
|
|
||||||
limit: 100
|
|
||||||
});
|
|
||||||
const posts = body.data.children.filter(post => post.data && post.data.post_hint === 'image' && post.data.url);
|
|
||||||
if (!posts.length) return msg.reply(`I couldn't fetch any images from r/${subreddit}...`);
|
|
||||||
const post = posts[Math.floor(Math.random() * posts.length)];
|
|
||||||
return msg.say(stripIndents`
|
|
||||||
**r/${subreddit}** ${post.data.title}
|
|
||||||
${post.data.url}
|
|
||||||
`);
|
|
||||||
} catch (err) {
|
|
||||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
const Command = require('../../structures/Command');
|
const SubredditCommand = require('../../structures/commands/Subreddit');
|
||||||
const request = require('node-superfetch');
|
|
||||||
const { stripIndents } = require('common-tags');
|
const { stripIndents } = require('common-tags');
|
||||||
|
|
||||||
module.exports = class RedditCommand extends Command {
|
module.exports = class RedditCommand extends SubredditCommand {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
super(client, {
|
super(client, {
|
||||||
name: 'reddit',
|
name: 'reddit',
|
||||||
@@ -21,27 +20,12 @@ module.exports = class RedditCommand extends Command {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(msg, { subreddit }) {
|
generateText(post, subreddit) {
|
||||||
try {
|
return stripIndents`
|
||||||
const { body } = await request
|
**r/${subreddit}** ${post.title}
|
||||||
.get(`https://www.reddit.com/r/${subreddit}/new.json`)
|
${post.url}
|
||||||
.query({
|
|
||||||
sort: 'new',
|
|
||||||
limit: 100
|
|
||||||
});
|
|
||||||
const posts = body.data.children.filter(post => post.data && (msg.channel.nsfw ? true : !post.data.over_18));
|
|
||||||
if (!posts.length) return msg.say('Could not find any results.');
|
|
||||||
const post = posts[Math.floor(Math.random() * posts.length)].data;
|
|
||||||
return msg.say(stripIndents`
|
|
||||||
**${post.title}**
|
|
||||||
<https://www.reddit.com${post.permalink}>
|
|
||||||
|
|
||||||
⬆ ${post.ups} ⬇ ${post.downs}
|
⬆ ${post.ups} ⬇ ${post.downs}
|
||||||
`);
|
`;
|
||||||
} catch (err) {
|
|
||||||
if (err.status === 403) return msg.say('This subreddit is private.');
|
|
||||||
if (err.status === 404) return msg.say('Could not find any results.');
|
|
||||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,31 +1,18 @@
|
|||||||
const Command = require('../../structures/Command');
|
const SubredditCommand = require('../../structures/commands/Subreddit');
|
||||||
const request = require('node-superfetch');
|
|
||||||
|
|
||||||
module.exports = class ShowerThoughtCommand extends Command {
|
module.exports = class ShowerThoughtCommand extends SubredditCommand {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
super(client, {
|
super(client, {
|
||||||
name: 'shower-thought',
|
name: 'shower-thought',
|
||||||
aliases: ['shower-thoughts'],
|
aliases: ['shower-thoughts'],
|
||||||
group: 'random',
|
group: 'random',
|
||||||
memberName: 'shower-thought',
|
memberName: 'shower-thought',
|
||||||
description: 'Responds with a random shower thought, directly from r/Showerthoughts.'
|
description: 'Responds with a random shower thought, directly from r/Showerthoughts.',
|
||||||
|
subreddit: 'Showerthoughts'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async run(msg) {
|
generateText(post) {
|
||||||
try {
|
return post.title;
|
||||||
const { body } = await request
|
|
||||||
.get('https://www.reddit.com/r/Showerthoughts/top.json')
|
|
||||||
.query({
|
|
||||||
sort: 'top',
|
|
||||||
t: 'day',
|
|
||||||
limit: 100
|
|
||||||
});
|
|
||||||
const posts = body.data.children.filter(post => post.data && (msg.channel.nsfw ? true : !post.data.over_18));
|
|
||||||
if (!posts.length) return msg.say('Hmm... It seems the thoughts are all gone right now. Try again later!');
|
|
||||||
return msg.say(posts[Math.floor(Math.random() * posts.length)].data.title);
|
|
||||||
} catch (err) {
|
|
||||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiao",
|
"name": "xiao",
|
||||||
"version": "91.8.12",
|
"version": "91.8.13",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "Xiao.js",
|
"main": "Xiao.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -20,6 +20,10 @@ module.exports = class ImgurAlbumCommand extends Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
generateText() {
|
||||||
|
throw new Error('The generateText method is required.');
|
||||||
|
}
|
||||||
|
|
||||||
async random() {
|
async random() {
|
||||||
if (this.cache) return this.cache[Math.floor(Math.random() * this.cache.length)];
|
if (this.cache) return this.cache[Math.floor(Math.random() * this.cache.length)];
|
||||||
const { body } = await request
|
const { body } = await request
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
const request = require('node-superfetch');
|
||||||
|
const Command = require('../Command');
|
||||||
|
|
||||||
|
module.exports = class SubredditCommand extends Command {
|
||||||
|
constructor(client, info) {
|
||||||
|
super(client, info);
|
||||||
|
|
||||||
|
this.subreddit = info.subreddit;
|
||||||
|
this.postType = info.postType;
|
||||||
|
}
|
||||||
|
|
||||||
|
async run(msg, { subreddit }) {
|
||||||
|
if (!subreddit) subreddit = typeof this.subreddit === 'function' ? this.subreddit() : this.subreddit;
|
||||||
|
try {
|
||||||
|
const { post, subreddit } = await this.random(msg.channel.nsfw);
|
||||||
|
if (!post) return msg.reply(`I couldn't fetch anything from r/${subreddit}...`);
|
||||||
|
return msg.say(this.generateText(post, subreddit));
|
||||||
|
} catch (err) {
|
||||||
|
if (err.status === 403) return msg.say('This subreddit is private.');
|
||||||
|
if (err.status === 404) return msg.say('Could not find any results.');
|
||||||
|
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
generateText() {
|
||||||
|
throw new Error('The generateText method is required.');
|
||||||
|
}
|
||||||
|
|
||||||
|
async random(subreddit, nsfw) {
|
||||||
|
const { body } = await request
|
||||||
|
.get(`https://www.reddit.com/r/${subreddit}/top.json`)
|
||||||
|
.query({
|
||||||
|
sort: 'top',
|
||||||
|
t: 'day',
|
||||||
|
limit: 100
|
||||||
|
});
|
||||||
|
const posts = body.data.children.filter(post => {
|
||||||
|
if (!post.data) return false;
|
||||||
|
if (!nsfw && post.data.over_18) return false;
|
||||||
|
return (this.postType ? post.data.post_hint === this.postType : true) && post.data.url && post.data.title;
|
||||||
|
});
|
||||||
|
if (!posts.length) return null;
|
||||||
|
return {
|
||||||
|
subreddit,
|
||||||
|
post: posts[Math.floor(Math.random() * posts.length)].data
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user