Browse Source

Add removal of data when bot kicked from guild

Along with join/leave console messages
master
benji7425 3 years ago
parent
commit
4bb676d136
  1. 14
      discord-bot-core/Client.js
  2. 12
      discord-bot-core/Util.js

14
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...");

12
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) {

Loading…
Cancel
Save