From ac35c8e87a6094d8c5add6c0084d110486ac746e Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Fri, 8 May 2020 16:06:57 -0400 Subject: [PATCH] Make MemePoster class extend webhook client --- Xiao.js | 11 ++++++-- package.json | 2 +- structures/Client.js | 12 ++++++--- structures/MemePoster.js | 54 +++++++++++++++++++++++----------------- 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/Xiao.js b/Xiao.js index 624afc17..56a1d60e 100644 --- a/Xiao.js +++ b/Xiao.js @@ -59,8 +59,15 @@ client.on('ready', () => { const text = typeof activity.text === 'function' ? activity.text() : activity.text; client.user.setActivity(text, { type: activity.type }); }, 60000); - if (client.memePoster.id && client.memePoster.token) { - client.setInterval(() => client.memePoster.post(), client.memePoster.time); + if (client.memePoster) { + client.setInterval(async () => { + try { + const post = await client.memePoster.fetchRandomPost(false); + await client.memePoster.post(post); + } catch (err) { + client.logger.error(err); + } + }, client.memePoster.postInterval); } }); diff --git a/package.json b/package.json index ec7732ab..e18dad9d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "114.8.4", + "version": "114.8.5", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/structures/Client.js b/structures/Client.js index a3096032..acf9d44b 100644 --- a/structures/Client.js +++ b/structures/Client.js @@ -3,10 +3,11 @@ const { WebhookClient } = require('discord.js'); const Collection = require('@discordjs/collection'); const winston = require('winston'); const PokemonStore = require('./pokemon/PokemonStore'); -const MemePoster = require('./MemePoster'); +const MemePosterClient = require('./MemePoster'); const activities = require('../assets/json/activity'); const leaveMsgs = require('../assets/json/leave-messages'); -const { XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN } = process.env; +const subreddits = require('../assets/json/meme'); +const { XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN, POSTER_ID, POSTER_TOKEN, POSTER_TIME } = process.env; module.exports = class XiaoClient extends CommandoClient { constructor(options) { @@ -21,7 +22,12 @@ module.exports = class XiaoClient extends CommandoClient { }); this.webhook = new WebhookClient(XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN, { disableMentions: 'everyone' }); this.pokemon = new PokemonStore(); - this.memePoster = new MemePoster(this); + this.memePoster = POSTER_ID && POSTER_TOKEN ? new MemePosterClient(POSTER_ID, POSTER_TOKEN, { + subreddits, + postTypes: ['image', 'rich:video'], + postInterval: POSTER_TIME, + disableMentions: 'everyone' + }) : null; this.games = new Collection(); this.phone = new Collection(); this.activities = activities; diff --git a/structures/MemePoster.js b/structures/MemePoster.js index 20752ebc..e146e9fb 100644 --- a/structures/MemePoster.js +++ b/structures/MemePoster.js @@ -1,41 +1,49 @@ -const { POSTER_ID, POSTER_TOKEN, POSTER_TIME } = process.env; +const { WebhookClient } = require('discord.js'); const request = require('node-superfetch'); const { embedURL } = require('../util/Util'); -const subreddits = require('../assets/json/meme'); -const types = ['image', 'rich:video']; -module.exports = class MemePoster { - constructor(client) { - Object.defineProperty(this, 'client', { value: client }); +module.exports = class MemePosterClient extends WebhookClient { + constructor(id, token, options) { + super(id, token, options); - this.id = POSTER_ID; - this.token = POSTER_TOKEN; - this.time = Number.parseFloat(POSTER_TIME) || 3.6e+6; + this.subreddits = options.subreddits; + this.postTypes = options.postTypes; + this.postInterval = options.postInterval ? Number.parseFloat(options.postInterval) : 3.6e+6; } - async post() { - try { - const subreddit = subreddits[Math.floor(Math.random() * subreddits.length)]; - const post = await this.fetchMeme(subreddit); - if (!post) return; - const url = embedURL(post.title, ``); - await request - .post(`https://discordapp.com/api/webhooks/${this.id}/${this.token}`) - .send({ content: `**r/${subreddit}** ${url}\n${post.url}` }); - } catch (err) { - this.client.logger.error(err); - } + post(post) { + const url = embedURL(post.title, ``); + return this.send(`**r/${subreddit}** ${url}\n${post.url}`); } - async fetchMeme(subreddit) { + async fetchRandomPost(nsfw) { + const subreddit = this.randomSubreddit(); + const post = await this.fetchPost(subreddit, nsfw); + return { + subreddit, + title: post.title, + url: post.permalink, + type: post.post_hint, + nsfw: post.over_18 || false + }; + } + + async fetchPost(subreddit, nsfw) { 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; + return this.postTypes.includes(post.data.post_hint) + && post.data.url + && post.data.title + && nsfw ? true : !post.data.over_18; }); if (!posts.length) return null; return posts[Math.floor(Math.random() * posts.length)].data; } + + randomSubreddit() { + return this.subreddits[Math.floor(Math.random() * this.subreddits.length)]; + } };