Move more stuff into the client

This commit is contained in:
dragonfire535
2017-10-16 14:51:32 -04:00
parent ecaaeac9d2
commit 4327781154
3 changed files with 46 additions and 41 deletions
+40 -8
View File
@@ -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;