mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-08 15:19:14 +02:00
Clean up MemePoster code
This commit is contained in:
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user