From 4327781154b041928ee87604a027d26242bcae47 Mon Sep 17 00:00:00 2001 From: dragonfire535 Date: Mon, 16 Oct 2017 14:51:32 -0400 Subject: [PATCH] Move more stuff into the client --- XiaoBot.js | 35 +++++--------------------- assets/json/whitelist.json | 4 --- structures/CommandoClient.js | 48 ++++++++++++++++++++++++++++++------ 3 files changed, 46 insertions(+), 41 deletions(-) delete mode 100644 assets/json/whitelist.json diff --git a/XiaoBot.js b/XiaoBot.js index 16272da9..697b3255 100644 --- a/XiaoBot.js +++ b/XiaoBot.js @@ -1,4 +1,4 @@ -const { TOKEN, OWNERS, COMMAND_PREFIX, INVITE } = process.env; +const { TOKEN, OWNERS, COMMAND_PREFIX, INVITE, DBOTS_KEY, DBOTSORG_KEY } = process.env; const path = require('path'); const CommandoClient = require('./structures/CommandoClient'); const client = new CommandoClient({ @@ -9,10 +9,12 @@ const client = new CommandoClient({ unknownCommandResponse: false, disabledEvents: ['TYPING_START'], messageCacheLifetime: 600, - messageSweepInterval: 120 + messageSweepInterval: 120, + dBotsToken: DBOTS_KEY, + dBotsOrgToken: DBOTSORG_KEY, + guildWhitelist: ['110373943822540800', '264445053596991498'], + guildPruneLevel: 25 }); -const { version } = require('./package'); -const whitelist = require('./assets/json/whitelist'); client.registry .registerDefaultTypes() @@ -45,27 +47,13 @@ client.on('ready', () => { client.setInterval(() => { const activities = [ `${COMMAND_PREFIX}help for commands`, - `Shard ${client.shard.id}`, 'with dragonfire535', client.options.invite, `with ${client.registry.commands.size} commands`, - `v${version}`, 'Rune Factory 4' ]; client.user.setActivity(activities[Math.floor(Math.random() * activities.length)]); }, 60000); - client.setInterval(async () => { - for (const guild of client.guilds.values()) { - if (whitelist.includes(guild.id)) continue; - if (guild.members.filter(member => member.user.bot).size > 25) { - try { - await guild.leave(); - } catch (err) { - console.error(`[LEAVE] Failed to leave guild ${guild.name}. (${guild.id})`, err); - } - } - } - }, 900000); }); client.on('disconnect', event => { @@ -79,17 +67,6 @@ client.on('warn', console.warn); client.on('commandError', (command, err) => console.error(command.name, err)); -client.on('guildCreate', async guild => { - if (whitelist.includes(guild.id)) return; - if (guild.members.filter(member => member.user.bot).size > 25) { - try { - await guild.leave(); - } catch (err) { - console.error(`[LEAVE] Failed to leave guild ${guild.name}. (${guild.id})`, err); - } - } -}); - client.dispatcher.addInhibitor(msg => { if (msg.channel.type !== 'text' || !msg.channel.topic) return false; if (msg.channel.topic.includes('')) return 'topic blocked'; diff --git a/assets/json/whitelist.json b/assets/json/whitelist.json deleted file mode 100644 index 7180b73b..00000000 --- a/assets/json/whitelist.json +++ /dev/null @@ -1,4 +0,0 @@ -[ - "110373943822540800", - "264445053596991498" -] diff --git a/structures/CommandoClient.js b/structures/CommandoClient.js index df5dddc6..bf8f9c3b 100644 --- a/structures/CommandoClient.js +++ b/structures/CommandoClient.js @@ -1,15 +1,18 @@ const { Client } = require('discord.js-commando'); const snekfetch = require('snekfetch'); -const { DBOTS_KEY, DBOTSORG_KEY } = process.env; +const { Collection } = require('discord.js'); class CommandoClient extends Client { constructor(options) { super(options); - Object.defineProperty(this, 'dBotsKey', { value: DBOTS_KEY }); - Object.defineProperty(this, 'dBotsOrgKey', { value: DBOTSORG_KEY }); + this.guildPruneLevel = options.guildPruneLevel; + this.guildWhitelist = options.guildWhitelist; + Object.defineProperty(this, 'dBotsToken', { value: options.dBotsToken }); + Object.defineProperty(this, 'dBotsOrgToken', { value: options.dBotsOrgToken }); - this.on('guildCreate', () => { + this.on('guildCreate', guild => { + this.pruneGuilds(guild); this.dBots(); this.dBotsOrg(); }); @@ -17,14 +20,15 @@ class CommandoClient extends Client { this.dBots(); this.dBotsOrg(); }); + this.setInterval(() => this.pruneGuilds(), 600000); } async dBots() { - if (!this.dBotsKey) return null; + if (!this.dBotsToken) return null; try { const { body } = await snekfetch .post(`https://bots.discord.pw/api/bots/${this.user.id}/stats`) - .set({ Authorization: this.dBotsKey }) + .set({ Authorization: this.dBotsToken }) .send({ shard_id: this.shard ? this.shard.id : 0, shard_count: this.options.shardCount || 1, @@ -38,11 +42,11 @@ class CommandoClient extends Client { } async dBotsOrg() { - if (!this.dBotsOrgKey) return null; + if (!this.dBotsOrgToken) return null; try { const { body } = await snekfetch .post(`https://discordbots.org/api/bots/${this.user.id}/stats`) - .set({ Authorization: this.dBotsOrgKey }) + .set({ Authorization: this.dBotsOrgToken }) .send({ shard_id: this.shard ? this.shard.id : 0, shard_count: this.options.shardCount || 1, @@ -54,6 +58,34 @@ class CommandoClient extends Client { return null; } } + + async pruneGuilds(guild) { + let guilds = new Collection(); + if (typeof guild === 'undefined') { + for (const g of this.guilds.values()) { + if (this.guildWhitelist.includes(g.id)) continue; + if (g.members.filter(member => member.user.bot).size > this.guildPruneLevel) { + try { + guilds.set(g.id, g); + await g.leave(); + } catch (err) { + this.emit('error', `Failed to leave guild ${g.name}. (${g.id})`, err); + } + } + } + } else { + if (this.guildWhitelist.includes(guild.id)) return guilds; + if (guild.members.filter(member => member.user.bot).size > this.guildPruneLevel) { + try { + guilds.set(guild.id, guild); + await guild.leave(); + } catch (err) { + this.emit('error', `Failed to leave guild ${g.name}. (${g.id})`, err); + } + } + } + return guilds; + } } module.exports = CommandoClient;