From b929e8d8f719fd73ade8e5988c9a49a8f4b06759 Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Fri, 20 Oct 2017 19:08:55 +0000 Subject: [PATCH] This might be the last commit --- XiaoBot.js | 15 +++++---- package.json | 2 +- structures/CommandoClient.js | 59 ------------------------------------ util/Util.js | 24 +++++++++++++++ 4 files changed, 34 insertions(+), 66 deletions(-) delete mode 100644 structures/CommandoClient.js diff --git a/XiaoBot.js b/XiaoBot.js index 45d56de7..fa6dde45 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('./structures/CommandoClient'); +const { CommandoClient } = require('discord.js-commando'); const client = new CommandoClient({ commandPrefix: COMMAND_PREFIX, owner: OWNERS.split(','), @@ -11,6 +11,7 @@ const client = new CommandoClient({ messageCacheLifetime: 600, messageSweepInterval: 120 }); +const { postStats } = require('./util/Util'); client.registry .registerDefaultTypes() @@ -52,16 +53,20 @@ client.on('ready', () => { }, 60000); }); +client.on('guildCreate', () => postStats(client)); + +client.on('guildDelete', () => postStats(client)); + client.on('disconnect', event => { console.error(`[DISCONNECT] Shard ${client.shard.id} disconnected with code ${event.code}.`); process.exit(0); }); -client.on('error', console.error); +client.on('error', err => console.error('[ERROR]', err)); -client.on('warn', console.warn); +client.on('warn', err => console.warn('[WARNING]', err)); -client.on('commandError', (command, err) => console.error(command.name, err)); +client.on('commandError', (command, err) => console.error('[COMMAND ERROR]', command.name, err)); client.dispatcher.addInhibitor(msg => { if (msg.channel.type !== 'text' || !msg.channel.topic) return false; @@ -71,8 +76,6 @@ client.dispatcher.addInhibitor(msg => { client.login(TOKEN); -// Uncomment if memory is low: setInterval(() => process.exit(0), 8.64e+7); - process.on('unhandledRejection', err => { console.error('[FATAL] Unhandled Promise Rejection.', err); process.exit(1); diff --git a/package.json b/package.json index de15dbc0..cd051e79 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiaobot", - "version": "48.1.0", + "version": "48.1.1", "description": "Your personal server companion.", "main": "Shard.js", "scripts": { diff --git a/structures/CommandoClient.js b/structures/CommandoClient.js deleted file mode 100644 index 13f326db..00000000 --- a/structures/CommandoClient.js +++ /dev/null @@ -1,59 +0,0 @@ -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, 'dBotsToken', { value: DBOTS_KEY }); - Object.defineProperty(this, 'dBotsOrgToken', { value: DBOTSORG_KEY }); - - this.on('guildCreate', () => { - this.dBots(); - this.dBotsOrg(); - }); - this.on('guildDelete', () => { - this.dBots(); - this.dBotsOrg(); - }); - } - - async dBots() { - if (!this.dBotsToken) return null; - try { - const { body } = await snekfetch - .post(`https://bots.discord.pw/api/bots/${this.user.id}/stats`) - .set({ Authorization: this.dBotsToken }) - .send({ - shard_id: this.shard ? this.shard.id : 0, - shard_count: this.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.dBotsOrgToken) return null; - try { - const { body } = await snekfetch - .post(`https://discordbots.org/api/bots/${this.user.id}/stats`) - .set({ Authorization: this.dBotsOrgToken }) - .send({ - shard_id: this.shard ? this.shard.id : 0, - shard_count: this.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 e05da1f8..5fd56111 100644 --- a/util/Util.js +++ b/util/Util.js @@ -1,8 +1,32 @@ +const snekfetch = require('snekfetch'); const { promisify } = require('util'); +const { DBOTS_KEY, DBOTSORG_KEY } = process.env; const yes = ['yes', 'y', 'ye', 'yeah', 'yup', 'yea']; const no = ['no', 'n', 'nah', 'nope']; class Util { + static postStats(client) { + if (!DBOTS_KEY || !DBOTSORG_KEY) return; + snekfetch + .post(`https://bots.discord.pw/api/bots/${client.user.id}/stats`) + .set({ Authorization: DBOTS_KEY }) + .send({ + shard_id: client.shard ? client.shard.id : 0, + shard_count: client.options.shardCount || 1, + server_count: client.guilds.size + }) + .catch(err => console.error('[STATS] Failed to post to bots.discord.pw.', err)); + snekfetch + .post(`https://discordbots.org/api/bots/${client.user.id}/stats`) + .set({ Authorization: DBOTSORG_KEY }) + .send({ + shard_id: client.shard ? client.shard.id : 0, + shard_count: client.options.shardCount || 1, + server_count: client.guilds.size + }) + .catch(err => console.error('[STATS] Failed to post to discordbots.org.', err)); + } + static wait(time) { return promisify(setTimeout)(time); }