Handle reddit better

This commit is contained in:
Daniel Odendahl Jr
2018-09-06 14:25:02 +00:00
parent 9c0f89b53e
commit 728a80db1e
4 changed files with 21 additions and 8 deletions
+7 -1
View File
@@ -17,7 +17,13 @@ module.exports = class MemeCommand extends Command {
async run(msg) {
const subreddit = subreddits[Math.floor(Math.random() * subreddits.length)];
try {
const { body } = await request.get(`https://www.reddit.com/r/${subreddit}/hot.json`);
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 => 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)];
+4 -1
View File
@@ -25,7 +25,10 @@ module.exports = class RedditCommand extends Command {
try {
const { body } = await request
.get(`https://www.reddit.com/r/${subreddit}/new.json`)
.query({ sort: 'new' });
.query({
sort: 'new',
limit: 100
});
const allowed = msg.channel.nsfw ? body.data.children : body.data.children.filter(post => !post.data.over_18);
if (!allowed.length) return msg.say('Could not find any results.');
const post = allowed[Math.floor(Math.random() * allowed.length)].data;
+9 -5
View File
@@ -15,11 +15,15 @@ module.exports = class ShowerThoughtCommand extends Command {
async run(msg) {
try {
const { body } = await request
.get('https://www.reddit.com/r/Showerthoughts.json')
.query({ limit: 1000 });
const allowed = msg.channel.nsfw ? body.data.children : body.data.children.filter(post => !post.data.over_18);
if (!allowed.length) return msg.say('Hmm... It seems the thoughts are all gone right now. Try again later!');
return msg.say(allowed[Math.floor(Math.random() * allowed.length)].data.title);
.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
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "90.2.0",
"version": "90.2.1",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {