From 6b210b32707898a8fdd448cb38318383519aae49 Mon Sep 17 00:00:00 2001 From: chris062689 Date: Wed, 19 Jul 2017 21:45:58 -0400 Subject: [PATCH] Added Application stats, will output at midnight to the server and log. --- app.js | 5 +++++ commands/ban.js | 1 + commands/warn.js | 2 ++ package.json | 1 + server.js | 19 +++++++++++++++++++ 5 files changed, 28 insertions(+) diff --git a/app.js b/app.js index cf05493..eef1c09 100644 --- a/app.js +++ b/app.js @@ -4,6 +4,11 @@ var Application = function() { this.logChannel = null; this.warnings = []; this.bans = []; + this.stats = { + joins: 0, + leaves: 0, + warnings: 0 + }; }; module.exports = new Application(); diff --git a/commands/ban.js b/commands/ban.js index cbaf640..f56d59d 100644 --- a/commands/ban.js +++ b/commands/ban.js @@ -19,6 +19,7 @@ exports.command = function(message) { app.logChannel.sendMessage(`Error banning ${user} ${user.username}`); logger.error(`Error banning ${user.toString()} ${user} ${user.username}.`, error); }); + data.flushBans(); }); } diff --git a/commands/warn.js b/commands/warn.js index b1468c3..f8f951b 100644 --- a/commands/warn.js +++ b/commands/warn.js @@ -16,6 +16,8 @@ exports.command = function(message) { app.warnings.push(new UserWarning(user.id, user.username, message.author.id, message.author.username, count)); data.flushWarnings(); + app.stats.warnings += 1; + if (count + 1 >= 3) { app.logChannel.sendMessage(`.ban ${user}`); } diff --git a/package.json b/package.json index 98d07da..73fe461 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "discord.js": "^10.0.1", "ip": "^1.1.5", "logdna": "^1.2.3", + "node-schedule": "^1.2.3", "request": "^2.79.0", "winston": "^2.3.0" } diff --git a/server.js b/server.js index 732dda3..22a920a 100644 --- a/server.js +++ b/server.js @@ -29,6 +29,25 @@ client.on('ready', () => { logger.info('Bot is now online and connected to server.'); }); +client.on("guildMemberAdd", (member) => { + app.stats.joins += 1; +}); + +client.on("guildMemberRemove", (member) => { + app.stats.leaves += 1; +}); + +// Output the stats for app.stats every 24 hours. +schedule.scheduleJob({ hour: 23, minute: 59 }, function(){ + logger.info(`Here are today's stats for ${(new Date()).toLocaleDateString()}! ${app.stats.joins} users have joined, ${app.stats.leaves} users have left, ${app.stats.warnings} warnings have been issued.`); + app.logChannel.sendMessage(`Here are today's stats for ${(new Date()).toLocaleDateString()}! ${app.stats.joins} users have joined, ${app.stats.leaves} users have left, ${app.stats.warnings} warnings have been issued.`); + + // Clear the stats for the day. + app.stats.joins = 0; + app.stats.leaves = 0; + app.stats.warnings = 0; +}); + client.on('message', message => { if (message.author.bot && message.content.startsWith('.ban') == false) { return; }