From 607a3804bc7e0f7b99a1cd4b166b5e83749bbde7 Mon Sep 17 00:00:00 2001 From: benji7425 Date: Sat, 11 Nov 2017 01:28:38 +0000 Subject: [PATCH] Add removal of data when bot kicked from guild Along with join/leave console messages --- discord-bot-core/Client.js | 14 +++++++++++++- discord-bot-core/Util.js | 12 ++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/discord-bot-core/Client.js b/discord-bot-core/Client.js index 589f87a..ca8e48d 100644 --- a/discord-bot-core/Client.js +++ b/discord-bot-core/Client.js @@ -28,6 +28,8 @@ module.exports = class Client extends Discord.Client { this.on("ready", this.onReady); this.on("message", this.onMessage); this.on("debug", this.onDebug); + this.on("guildCreate", this.onGuildCreate); + this.on("guildDelete", this.onGuildDelete); process.on("uncaughtException", err => this.onUnhandledException(this, err)); } @@ -48,7 +50,7 @@ module.exports = class Client extends Discord.Client { onMessage(message) { if (message.channel.type === "text" && message.member) { - if(!this.guildsData[message.guild.id]) + if (!this.guildsData[message.guild.id]) this.guildsData[message.guild.id] = new this.guildDataModel({ id: message.guild.id }); HandleMessage(this, message, this.commands, this.guildsData[message.guild.id]); } @@ -59,6 +61,16 @@ module.exports = class Client extends Discord.Client { CoreUtil.dateDebug(info); } + onGuildCreate(guild) { + CoreUtil.dateLog(`Added to guild ${guild.name}`); + } + + onGuildDelete(guild) { + CoreUtil.dateLog(`Removed from guild ${guild.name}, removing data for this guild`); + delete this.guildsData[guild.id]; + this.writeFile(); + } + onUnhandledException(client, err) { CoreUtil.dateError(err.message || err); CoreUtil.dateLog("Destroying existing client..."); diff --git a/discord-bot-core/Util.js b/discord-bot-core/Util.js index 7055e10..ba47fcb 100644 --- a/discord-bot-core/Util.js +++ b/discord-bot-core/Util.js @@ -28,19 +28,19 @@ function ask(client, textChannel, member, question) { } function dateLog(...args) { - doDateLog(Console.log, logWriter, args); + doDateLog(Console.log, logWriter, args, "INFO"); } function dateError(...args) { - doDateLog(Console.error, logWriter, args); + doDateLog(Console.error, logWriter, args, "ERROR"); } function dateDebug(...args) { - doDateLog(null, null, args); + doDateLog(null, null, args, "DEBUG"); } -function doDateLog(consoleMethod, fileWriter, args) { - args = formatArgs(args); +function doDateLog(consoleMethod, fileWriter, args, prefix = "") { + args = formatArgs([`[${prefix}]`].concat(args)); if (consoleMethod !== null) consoleMethod.apply(this, args); @@ -52,7 +52,7 @@ function doDateLog(consoleMethod, fileWriter, args) { } function formatArgs(args) { - return ["[", new Date().toUTCString(), "] "].concat(args); + return [`[${new Date().toUTCString()}]`].concat(args); } function formatArgsForFile(args) {