const fs = require('fs').promises; const conf = require('../conf'); const playerListDir = conf.playerListDir; function getFormattedDate() { const months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]; const currentDate = new Date(); const year = currentDate.getUTCFullYear(); const month = months[currentDate.getUTCMonth()]; const day = currentDate.getUTCDate().toString().padStart(2, '0'); return `${month}-${day}-${year}`; } function millisecondsUntilAMUTC() { const now = new Date(); const utcMidnight = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() + 1, 0, 0, 0); // Next UTC midnight const millisecondsUntilMidnightUTC = utcMidnight - now.getTime(); return millisecondsUntilMidnightUTC; } async function currentListedPlayers(file){ try{ let res = await fs.readFile(`${playerListDir}/${file}`); return res.toString().split('\n'); }catch(error){ return []; } } async function updateListFile(file, players){ try{ fs.writeFile(`${playerListDir}/${file}`, players) }catch(error){ console.error('Unkown error writting player file') } } let addLock = false; let toAddList = []; async function addPlayer(playerName){ if(addLock){ toAddList.push(playerName); return ; } addLock = true let now = getFormattedDate(); let current = await currentListedPlayers(now) if(!current.includes(playerName)){ current.push(playerName) await updateListFile(now, current.join('\n')) } addLock = false; if(toAddList.length){ await addPlayer(toAddList.shift()); } } function onJoin(bot){ bot.bot.on('playerJoined', async function(player){ await addPlayer(player.username); }); } function doMidnight(bots){ let later = millisecondsUntilAMUTC(); setTimeout(function(){ }, later) } module.exports = {onJoin, doMidnight};