Add chat and command logging

This commit is contained in:
7cc5c4f330d47060 2024-08-15 05:38:09 -04:00
parent fcf3eab60a
commit 59d04b3879
Signed by: 7cc5c4f330d47060
SSH key fingerprint: SHA256:e+4tcZut1nBpe10PqjaO+Rvie0Q7W4qIvFzcUw+7riA
2 changed files with 46 additions and 0 deletions

32
plugins/chatlog.js Executable file
View file

@ -0,0 +1,32 @@
const chatlog = require("../util/chatlog.js")
const fs = require('fs')
const settings = require('../settings.json')
const checkLog = () => {
if(settings.disableLogging) return;
try {
if (!fs.readdirSync('.').includes('botvXLogs')) fs.mkdirSync('botvXLogs');
const dateToday = new Date(Date.now());
const dateTomorrow = new Date(Date.now()+86400000);
const filenameToday = `${dateToday.getUTCMonth()+1}-${dateToday.getUTCDate()}-${dateToday.getUTCFullYear()}`;
const filenameTomorrow = `${dateTomorrow.getUTCMonth()+1}-${dateTomorrow.getUTCDate()}-${dateTomorrow.getUTCFullYear()}`;
if (!fs.readdirSync('./botvXLogs').includes(filenameToday)) fs.mkdirSync(`botvXLogs/${filenameToday}`);
if (!fs.readdirSync('./botvXLogs').includes(filenameTomorrow)) fs.mkdirSync(`botvXLogs/${filenameTomorrow}`); // Create tomorrow's log directory early
} catch(e){
console.log(e) // Prevents some crashes when disk space is full or when the permissions are incorrect
}
}
setInterval(checkLog, 3600000) // Runs once every hour,
checkLog() // and at bot startup.
module.exports = {
load: (b) => {
b.on('plainchat', (msg, type) => {
if(!settings.disableLogging && !settings.disableChatLogging) chatlog(`chat_${b.host.host}_${b.host.port}`, `[${type}] ${msg}`)
})
b.on('command', (name, uuid, text) => {
if(!settings.disableLogging && !settings.disableCommandLogging) chatlog(`cmd_${b.host.host}_${b.host.port}`, `${name} (${uuid}): ${text}`)
})
}
}

14
util/chatlog.js Normal file
View file

@ -0,0 +1,14 @@
const fs = require('fs')
const settings = require('../settings.json')
module.exports = function(fileName, item){
if(settings.disableLogging) return;
const dateToday = new Date(Date.now());
const UTCHours = dateToday.getUTCHours();
const UTCMinutes = dateToday.getUTCMinutes().toString().padStart(2,"0");
const UTCSeconds = dateToday.getUTCSeconds().toString().padStart(2,"0");
const UTCMilliSeconds = dateToday.getUTCMilliseconds().toString().padStart(3,"0");
const filenameToday = `${dateToday.getUTCMonth()+1}-${dateToday.getUTCDate()}-${dateToday.getUTCFullYear()}`;
const logDate = `${dateToday.getUTCMonth()+1}/${dateToday.getUTCDate()}/${dateToday.getUTCFullYear()} ${UTCHours}:${UTCMinutes}:${UTCSeconds}.${UTCMilliSeconds}`;
fs.appendFileSync(`botvXLogs/${filenameToday}/${fileName}.txt`, `[${logDate}] ${item}\n`)
}