chipmunkbot3/plugins/seen.js
2024-02-11 21:23:41 -05:00

49 lines
1.2 KiB
JavaScript

const fs = require('fs')
const path = require('path')
const filepath = path.resolve('persistent', 'seen.json')
// load the seen data
let seen = {}
try {
seen = require(filepath)
} catch (e) {
console.log('An error occured while loading seen players.')
}
// save it every 5 minutes
setInterval(() => {
fs.writeFileSync(filepath, JSON.stringify(seen, null, 2))
}, 5 * 6000)
// expose the data to the bot
function bot (bot) {
bot.seen = seen
}
// add players to it
function client (bot, client) {
client.on('player_info', (packet) => {
packet.data.forEach((player) => {
if (player.UUID === client.uuid) return
if (packet.action !== 0) {
const name = bot.players[player.UUID]?.name
if (seen[name] != null) seen[name].last = new Date()
return
}
seen[player.name] ??= {}
if (seen[player.name].first == null) {
seen[player.name].first = new Date()
bot.core.run('minecraft:tellraw @a ' + JSON.stringify([
{ text: 'Welcome ', color: bot.colors.primary },
{ text: player.name, color: bot.colors.secondary },
' to the server!'
]))
}
seen[player.name].last = new Date()
})
})
}
module.exports = { bot, client }