Clean up MemePoster code

This commit is contained in:
Dragon Fire
2020-03-01 09:42:00 -05:00
parent 7d9f272c0c
commit 51071ca0bb
+14 -9
View File
@@ -15,15 +15,8 @@ module.exports = class MemePoster {
async post() {
try {
const subreddit = subreddits[Math.floor(Math.random() * subreddits.length)];
const { body } = await request
.get(`https://www.reddit.com/r/${subreddit}/hot.json`)
.query({ limit: 100 });
const posts = body.data.children.filter(post => {
if (!post.data) return false;
return types.includes(post.data.post_hint) && post.data.url && post.data.title && !post.data.over_18;
});
if (!posts.length) return;
const post = posts[Math.floor(Math.random() * posts.length)].data;
const post = await this.fetchMeme(subreddit);
if (!post) return;
await request
.post(`https://discordapp.com/api/webhooks/${this.id}/${this.token}`)
.send({
@@ -33,4 +26,16 @@ module.exports = class MemePoster {
this.client.logger.error(err);
}
}
async fetchMeme(subreddit) {
const { body } = await request
.get(`https://www.reddit.com/r/${subreddit}/hot.json`)
.query({ limit: 100 });
const posts = body.data.children.filter(post => {
if (!post.data) return false;
return types.includes(post.data.post_hint) && post.data.url && post.data.title && !post.data.over_18;
});
if (!posts.length) return null;
return posts[Math.floor(Math.random() * posts.length)].data;
}
};