Skip to content

Commit

Permalink
This addresses issue #6 by adding a log file to the main bot.js scrip…
Browse files Browse the repository at this point in the history
…t, allowing for every command executed to be logged in a file, with a timestamp. This will allow the sysadmin to view the log file and see what commands have been issued.
  • Loading branch information
Nos78 committed May 23, 2019
1 parent 16c96ad commit 6af68b3
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const config = require('./config.json');
const configSecret = require('./config-secret.json');
const db = require('./db');
const fs = require('fs');
const cmdLog = './cmdExec.log';

// Set up the logger for debug/info
const logger = require('winston');
Expand Down Expand Up @@ -64,6 +65,18 @@ try {
client.deleteCallingCommand = false;
}

try {
var stream = fs.createWriteStream(cmdLog, {flags:'a'});
client.cmdLogStream = stream;
if(stream == null) {
logger.info(`Unable to initialise file stream for cmdExec.log`);
} else {
stream.write(new Date().toISOString() + ` bot started`) + `/n`;
}
} catch (error) {
logger.info(`Unable to write ${cmdLog}`);
}

//
// Set up the callback functions
//
Expand Down Expand Up @@ -243,6 +256,21 @@ client.on('message', async message => {
client.myselfMaximum = 0;
}
}

if(client.cmdLogStream == null) {
logger.info("No open logfile stream");
client.cmdLogStream = fs.createWriteStream(cmdLog, {flags:'a'});
}
try {
nowDateTimeStr = new Date().toISOString();
logger.debug(`${nowDateTimeStr} - Writing to log file...`);
client.cmdLogStream.write(nowDateTimeStr + ':' + "\n");
client.cmdLogStream.write(" " + `executing ${cmd.name}` + "\n");
client.cmdLogStream.write(" " + `called by ${message.author.username}` + "\n\n");
} catch (e) {
logger.error(e);
}

cmd.execute(message, args);
if (client.deleteCallingCommand) {
message.delete;
Expand Down

0 comments on commit 6af68b3

Please sign in to comment.