From 61ba69e6aee2c6fe66b0e8310fd50da8e671de8c Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Sun, 3 Nov 2019 00:08:44 -0400 Subject: [PATCH] Integrate meme-poster into Xiao --- .env.example | 3 +++ README.md | 1 - Xiao.js | 1 + package.json | 2 +- structures/Client.js | 2 ++ structures/MemePoster.js | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 structures/MemePoster.js diff --git a/.env.example b/.env.example index 6660faa0..e9a86354 100644 --- a/.env.example +++ b/.env.example @@ -6,6 +6,9 @@ XIAO_PREFIX= INVITE= XIAO_WEBHOOK_ID= XIAO_WEBHOOK_TOKEN= +POSTER_ID= +POSTER_TOKEN= +POSTER_TIME= # Emoji IDs GOLD_FISH_EMOJI_ID= diff --git a/README.md b/README.md index a63e48cd..dfe4febf 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,6 @@ Xiao is a Discord bot coded in JavaScript with * [Rando Cardrissian](https://github.com/dragonfire535/rando-cardrissian) is a Cards Against Humanity bot, whose features were originally built into Xiao. * [Storyteller](https://github.com/dragonfire535/storyteller) is a Mafia bot made for Discord's 2019 Hack Week, whose features were originally built into Xiao. -* [Meme Poster](https://github.com/dragonfire535/meme-poster) is a meme-posting webhook, inspired by an idea that started as part of Xiao. ## Commands (348) ### Utility: diff --git a/Xiao.js b/Xiao.js index b640019d..4d027e73 100644 --- a/Xiao.js +++ b/Xiao.js @@ -46,6 +46,7 @@ client.on('ready', () => { const activity = activities[Math.floor(Math.random() * activities.length)]; client.user.setActivity(activity.text, { type: activity.type }); }, 60000); + client.setInterval(() => client.memePoster.post(), client.memePoster.time); }); client.on('guildMemberRemove', async member => { diff --git a/package.json b/package.json index bd89b6a1..87a6dd7c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "107.7.5", + "version": "107.8.0", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/structures/Client.js b/structures/Client.js index 99a280fc..0d933933 100644 --- a/structures/Client.js +++ b/structures/Client.js @@ -2,6 +2,7 @@ const { CommandoClient } = require('discord.js-commando'); const { WebhookClient } = require('discord.js'); const winston = require('winston'); const PokemonStore = require('./pokemon/PokemonStore'); +const MemePoster = require('./MemePoster'); const { XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN } = process.env; module.exports = class XiaoClient extends CommandoClient { @@ -17,6 +18,7 @@ module.exports = class XiaoClient extends CommandoClient { }); this.webhook = new WebhookClient(XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN, { disableEveryone: true }); this.pokemon = new PokemonStore(); + this.memePoster = new MemePoster(this); this.games = new Map(); } }; diff --git a/structures/MemePoster.js b/structures/MemePoster.js new file mode 100644 index 00000000..af78c2e7 --- /dev/null +++ b/structures/MemePoster.js @@ -0,0 +1,36 @@ +const { POSTER_ID, POSTER_TOKEN, POSTER_TIME } = process.env; +const request = require('node-superfetch'); +const subreddits = require('../assets/json/meme'); +const types = ['image', 'rich:video']; + +module.exports = class MemePoster { + constructor(client) { + Object.defineProperty(this, 'client', { value: client }); + + this.id = POSTER_ID; + this.token = POSTER_TOKEN; + this.time = Number.parseFloat(POSTER_TIME) || 3.6e+6; + } + + 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; + await request + .post(`https://discordapp.com/api/webhooks/${this.id}/${this.token}`) + .send({ + content: `**r/${subreddit}** [${post.title}]()\n${post.url}` + }); + } catch (err) { + this.client.logger.error(err); + } + } +};