diff --git a/Shard.js b/Shard.js index 42d2d815..17add350 100644 --- a/Shard.js +++ b/Shard.js @@ -1,17 +1,5 @@ const { ShardingManager } = require('discord.js'); const path = require('path'); -const { postStats } = require('./util/Util'); const { TOKEN } = process.env; const manager = new ShardingManager(path.join(__dirname, 'XiaoBot.js'), { token: TOKEN }); manager.spawn(undefined, 1000); - -setInterval(async () => { - if (!manager.shards.has(0)) return; - try { - const guilds = await manager.fetchClientValues('guilds.size'); - const count = guilds.reduce((prev, val) => prev + val, 0); - await postStats(count, await manager.shards.get(0).fetchClientValue('user.id')); - } catch (err) { - console.error(`[STATS] Failed to post stats. ${err}`); - } -}, 300000); diff --git a/XiaoBot.js b/XiaoBot.js index 8e909f18..16272da9 100644 --- a/XiaoBot.js +++ b/XiaoBot.js @@ -1,6 +1,6 @@ const { TOKEN, OWNERS, COMMAND_PREFIX, INVITE } = process.env; const path = require('path'); -const { CommandoClient } = require('discord.js-commando'); +const CommandoClient = require('./structures/CommandoClient'); const client = new CommandoClient({ commandPrefix: COMMAND_PREFIX, owner: OWNERS.split(','), @@ -59,10 +59,9 @@ client.on('ready', () => { if (whitelist.includes(guild.id)) continue; if (guild.members.filter(member => member.user.bot).size > 25) { try { - console.log(`[LEAVE] Leaving guild ${guild.name}. (${guild.id})`); await guild.leave(); } catch (err) { - console.error(`[LEAVE] Failed to leave guild ${guild.name}. (${guild.id}) ${err}`); + console.error(`[LEAVE] Failed to leave guild ${guild.name}. (${guild.id})`, err); } } } @@ -84,10 +83,9 @@ client.on('guildCreate', async guild => { if (whitelist.includes(guild.id)) return; if (guild.members.filter(member => member.user.bot).size > 25) { try { - console.log(`[LEAVE] Leaving guild ${guild.name}. (${guild.id})`); await guild.leave(); } catch (err) { - console.error(`[LEAVE] Failed to leave guild ${guild.name}. (${guild.id}) ${err}`); + console.error(`[LEAVE] Failed to leave guild ${guild.name}. (${guild.id})`, err); } } }); @@ -98,13 +96,10 @@ client.dispatcher.addInhibitor(msg => { return false; }); -client.setInterval(() => { - console.log(`[RESTART] Shard ${client.shard.id} restarted!`); - process.exit(0); -}, 8.64e+7); - client.login(TOKEN); +setInterval(() => process.exit(0), 8.64e+7); + process.on('unhandledRejection', err => { console.error('[FATAL] Unhandled Promise Rejection.', err); process.exit(1); diff --git a/structures/CommandoClient.js b/structures/CommandoClient.js new file mode 100644 index 00000000..d43d39d0 --- /dev/null +++ b/structures/CommandoClient.js @@ -0,0 +1,59 @@ +const { Client } = require('discord.js-commando'); +const snekfetch = require('snekfetch'); +const { DBOTS_KEY, DBOTSORG_KEY } = process.env; + +class CommandoClient extends Client { + constructor(options) { + super(options); + + Object.defineProperty(this, 'dBotsKey', { value: DBOTS_KEY }); + Object.defineProperty(this, 'dBotsOrgKey', { value: DBOTSORG_KEY }); + + this.on('guildCreate', () => { + this.dBots(); + this.dBotsOrg(); + }); + this.on('guildDelete', () => { + this.dBots(); + this.dBotsOrg(); + }); + } + + async dBots() { + if (!this.dBotsKey) return null; + try { + const { body } = await snekfetch + .post(`https://bots.discord.pw/api/bots/${this.user.id}/stats`) + .set({ Authorization: this.dBotsKey }) + .send({ + shard_id: this.shard ? this.shard.id : 0, + shard_count: this.client.options.shardCount || 1, + server_count: this.guilds.size + }); + return body; + } catch (err) { + this.emit('error', 'Failed to post to Discord Bots', err); + return null; + } + } + + async dBotsOrg() { + if (!this.dBotsOrgKey) return null; + try { + const { body } = await snekfetch + .post(`https://discordbots.org/api/bots/${this.user.id}/stats`) + .set({ Authorization: this.dBotsOrgKey }) + .send({ + shard_id: this.shard ? this.shard.id : 0, + shard_count: this.client.options.shardCount || 1, + server_count: this.guilds.size + }); + return body; + } catch (err) { + this.emit('error', 'Failed to post to Discord Bots Org', err); + return null; + } + } +} + +module.exports = CommandoClient; diff --git a/util/Util.js b/util/Util.js index 17aa73f5..d7af072e 100644 --- a/util/Util.js +++ b/util/Util.js @@ -1,21 +1,6 @@ -const snekfetch = require('snekfetch'); const { promisify } = require('util'); -const { DBOTS_KEY, DBOTSORG_KEY } = process.env; class Util { - static postStats(count, id) { - snekfetch - .post(`https://bots.discord.pw/api/bots/${id}/stats`) - .set({ Authorization: DBOTS_KEY }) - .send({ server_count: count }) - .catch(err => console.error(`[DBOTS] Failed to post to Discord Bots. ${err}`)); - snekfetch - .post(`https://discordbots.org/api/bots/${id}/stats`) - .set({ Authorization: DBOTSORG_KEY }) - .send({ server_count: count }) - .catch(err => console.error(`[DBOTSORG] Failed to post to Discord Bots Org. ${err}`)); - } - static wait(time) { return promisify(setTimeout)(time); }