Fixed !clear command to clear server-side chatBuffer as well as client buffers.

This commit is contained in:
rainbow napkin 2025-09-10 03:43:15 -04:00
parent 9eeed591ad
commit de11803cea
3 changed files with 51 additions and 5 deletions

View file

@ -118,16 +118,55 @@ class chatBuffer{
} }
/** /**
* Saves RAM-Based buffer to Channel Document in DB * Clears out buffer timers to prevent saving
* @param {String} reason - Reason for DB save, formatted as 'x minutes/seconds of in/activity', used for logging purposes
* @param {Mongoose.Document} chanDB - Channel Doc to work with, can be left empty for method to auto-find through channel name.
*/ */
async saveDB(reason, chanDB){ clearTimers(){
//clear existing timers //clear existing timers
clearTimeout(this.inactivityTimer); clearTimeout(this.inactivityTimer);
clearTimeout(this.busyTimer); clearTimeout(this.busyTimer);
this.inactivityTimer = null; this.inactivityTimer = null;
this.busyTimer = null; this.busyTimer = null;
}
/**
* Clears RAM-Based chat buffer and saves the result to DB
* @param {String} name - Name of user to clear chats from. Left as null or an empty string, it will clear the entire buffer.
*/
async clearBuffer(name){
//Clear out DB Timers
this.clearTimers();
let reason = "clearing chat";
//If we have a null or empty string passed as name
if(name == null || name == ""){
//Nuke that fcker
this.buffer = [];
//Otherwise
}else{
reason = `clearing ${name}'s chats`
//Iterate through chat buffer by index
for(let chatIndex in this.buffer){
//If the current chat we're looking at was submitted by the given user
if(this.buffer[chatIndex].user.toLowerCase() == name.toLowerCase()){
//Splice that fcker out
this.buffer.splice(chatIndex, 1);
}
}
}
await this.saveDB(reason);
}
/**
* Saves RAM-Based buffer to Channel Document in DB
* @param {String} reason - Reason for DB save, formatted as 'x minutes/seconds of in/activity', used for logging purposes
* @param {Mongoose.Document} chanDB - Channel Doc to work with, can be left empty for method to auto-find through channel name.
*/
async saveDB(reason, chanDB){
//Clear out DB Timers
this.clearTimers();
//if the server is in screamy boi mode //if the server is in screamy boi mode
if(config.verbose){ if(config.verbose){

View file

@ -334,7 +334,11 @@ class chatHandler{
//If no user was entered OR the user was found //If no user was entered OR the user was found
if(user == null || target != null){ if(user == null || target != null){
//Send command out to browsers to drop chats from buffer
this.server.io.in(chan).emit("clearChat", {user}); this.server.io.in(chan).emit("clearChat", {user});
//Clear serverside buffer, down to the DB
activeChan.chatBuffer.clearBuffer(user);
} }
} }
} }

View file

@ -176,8 +176,11 @@ module.exports.errorMiddleware = function(err, req, res, next){
module.exports.dumpError = function(err, date = new Date()){ module.exports.dumpError = function(err, date = new Date()){
try{ try{
const content = `Error Date: ${date.toLocaleString()} (UTC-${date.getTimezoneOffset()/60})\nError Type: ${err.name}\nError Msg:${err.message}\nStack Trace:\n\n${err.stack}`; const content = `Error Date: ${date.toLocaleString()} (UTC-${date.getTimezoneOffset()/60})\nError Type: ${err.name}\nError Msg:${err.message}\nStack Trace:\n\n${err.stack}`;
const path = `log/crash/${date.getTime()}.log`;
fs.writeFile(`log/crash/${date.getTime()}.log`, content); fs.writeFile(path, content);
module.exports.consoleWarn(`Warning: Unexpected Server Crash gracefully dumped to '${path}'... SOMETHING MAY BE VERY BROKEN!!!!`);
}catch(doubleErr){ }catch(doubleErr){
module.exports.consoleWarn("Yo Dawg, I herd you like errors, so I put an error in your error dump, so you can dump while you dump:"); module.exports.consoleWarn("Yo Dawg, I herd you like errors, so I put an error in your error dump, so you can dump while you dump:");
module.exports.consoleWarn(err); module.exports.consoleWarn(err);