Add chat logging

This commit is contained in:
7cc5c4f330d47060 2024-11-07 01:24:06 -05:00
parent eb1d352915
commit b9bb068252
Signed by: 7cc5c4f330d47060
SSH key fingerprint: SHA256:e+4tcZut1nBpe10PqjaO+Rvie0Q7W4qIvFzcUw+7riA
2 changed files with 48 additions and 0 deletions

31
plugins/chatlog.js Executable file
View file

@ -0,0 +1,31 @@
import chatlog from '../util/chatlog.js'
import { readdirSync, mkdirSync } from "node:fs"
import { default as settings } from '../settings.json' with {type: "json"}
const checkLog = () => {
if (settings.disableLogging) return
try {
if (!readdirSync('.').includes('logs')) mkdirSync('logs')
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 (!readdirSync('./logs').includes(filenameToday)) mkdirSync(`logs/${filenameToday}`)
if (!readdirSync('./logs').includes(filenameTomorrow)) mkdirSync(`logs/${filenameTomorrow}`) // Create tomorrow's log directory early
} catch (e) {
console.log(e) // Prevents some crashes when there is no space remaining on the storage medium the bot is on or when the permissions are incorrect
}
}
setInterval(checkLog, 3600000) // Runs once every hour,
checkLog() // and at bot startup.
export default function 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', c => {
if (!settings.disableLogging && !settings.disableCommandLogging) chatlog(`cmd_${b.host.host}_${b.host.port}`, `[${c.msgType}] ${c.username} (${c.uuid}): ${c.command}`)
})
}

17
util/chatlog.js Executable file
View file

@ -0,0 +1,17 @@
import { appendFileSync } from 'node:fs'
import { default as settings } from '../settings.json' with {type: "json"}
export default function chatlog (fileName, item) {
if (settings.disableLogging) return
const dateToday = new Date(Date.now())
const UTCYears = dateToday.getUTCFullYear()
const UTCMonths = dateToday.getUTCMonth() + 1
const UTCDays = dateToday.getUTCDate()
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 = `${UTCMonths}-${UTCDays}-${UTCYears}`
const logDate = `${UTCMonths}/${UTCDays}/${UTCYears} ${UTCHours}:${UTCMinutes}:${UTCSeconds}.${UTCMilliSeconds}`
appendFileSync(`logs/${filenameToday}/${fileName}.txt`, `[${logDate}] ${item}\n`)
}