From d85f906a6957181a376840668d17c60d80612110 Mon Sep 17 00:00:00 2001 From: rainbow napkin Date: Fri, 13 Dec 2024 18:28:48 -0500 Subject: [PATCH] Started work on toke logic --- src/app/channel/chatHandler.js | 20 +++- src/app/channel/commandPreprocessor.js | 18 ++-- src/app/channel/tokebot.js | 100 +++++++++++++++++- .../api/admin/tokeCommandController.js | 2 +- src/schemas/channel/channelSchema.js | 4 +- src/schemas/tokebot/tokeCommandSchema.js | 28 +++++ src/schemas/userSchema.js | 4 +- 7 files changed, 160 insertions(+), 16 deletions(-) diff --git a/src/app/channel/chatHandler.js b/src/app/channel/chatHandler.js index fc11c2e..80df1e7 100644 --- a/src/app/channel/chatHandler.js +++ b/src/app/channel/chatHandler.js @@ -79,14 +79,26 @@ module.exports = class{ } } - relayChat(socket, msg, type = 'chat'){ + relayUserChat(socket, msg, type){ const user = this.server.getSocketInfo(socket); - this.server.io.in(socket.chan).emit("chatMessage", {user: user.user, flair: user.flair, highLevel: user.highLevel, msg, type}); + this.relayChat(user.user, user.flair, user.highLevel, msg, type, socket.chan) + } + + relayChat(user, flair, highLevel, msg, type = 'chat', chan){ + this.server.io.in(chan).emit("chatMessage", {user, flair, highLevel, msg, type}); + } + + relayGlobalChat(user, flair, highLevel, msg, type = 'chat'){ + this.server.io.emit("chatMessage", {user, flair, highLevel, msg, type}); + } + + relayTokeCallout(msg){ + this.relayGlobalChat("Tokebot", "", 10, msg, "toke"); } relayServerAnnouncement(msg){ //This codebase is always at a 10 - this.server.io.emit("chatMessage", {user: "Server", flair: "", highLevel: 10, msg, type: "announcement"}); + this.relayGlobalChat("Server", "", 10, msg, "announcement"); } relayChannelAnnouncement(chan, msg){ @@ -95,7 +107,7 @@ module.exports = class{ //If channel isn't null if(activeChan != null){ //This codebase is always at a 10 - this.server.io.in(chan).emit("chatMessage", {user: "Channel", flair: "", highLevel: 10, msg, type: "announcement"}); + this.relayChat("Channel", "", 10, msg, "announcement", chan); } } diff --git a/src/app/channel/commandPreprocessor.js b/src/app/channel/commandPreprocessor.js index c89e2dd..40c1579 100644 --- a/src/app/channel/commandPreprocessor.js +++ b/src/app/channel/commandPreprocessor.js @@ -71,8 +71,6 @@ module.exports = class commandPreprocessor{ async processServerCommand(){ //If the raw message starts with '!' (skip commands that start with whitespace so people can send example commands in chat) if(this.rawData.msg[0] == '!'){ - //if it isn't just an exclimation point, and we have a real command - if(this.argumentArray != null && this.commandProcessor[this.argumentArray[0].toLowerCase()] != null){ //Create hash table to hold information about current command const commandObj = { socket: this.socket, @@ -81,8 +79,16 @@ module.exports = class commandPreprocessor{ rawData: this.rawData } - //Process the command and use the return value to set the sendflag (true if command valid) - this.sendFlag = await this.commandProcessor[this.argumentArray[0].toLowerCase()](commandObj); + //if it isn't just an exclimation point, and we have a real command + if(this.argumentArray != null){ + if(this.commandProcessor[this.argumentArray[0].toLowerCase()] != null){ + //Process the command and use the return value to set the sendflag (true if command valid) + this.sendFlag = await this.commandProcessor[this.argumentArray[0].toLowerCase()](commandObj); + + }else{ + //Process as toke command if we didnt get a match from the standard server-side command processor + this.sendFlag = await this.tokebot.tokeProcessor(commandObj); + } } } } @@ -91,7 +97,7 @@ module.exports = class commandPreprocessor{ //If the send flag is true if(this.sendFlag){ //FUCKIN' SEND IT! - this.chatHandler.relayChat(this.socket, this.commandArray.join('').trimStart()); + this.chatHandler.relayUserChat(this.socket, this.commandArray.join('').trimStart()); } } } @@ -108,7 +114,7 @@ class commandProcessor{ commandObj.commandArray.splice(0,2); //send it - this.chatHandler.relayChat(commandObj.socket, commandObj.commandArray.join(''), 'whisper'); + this.chatHandler.relayUserChat(commandObj.socket, commandObj.commandArray.join(''), 'whisper'); //Make sure to throw the send flag return false; diff --git a/src/app/channel/tokebot.js b/src/app/channel/tokebot.js index f03115c..4578402 100644 --- a/src/app/channel/tokebot.js +++ b/src/app/channel/tokebot.js @@ -24,6 +24,20 @@ module.exports = class tokebot{ this.server = server; this.chatHandler = chatHandler; + //Set timeouts to null + this.tokeTimer = null; + this.cooldownTimer = null; + + //Set start times + this.tokeTime = 60; + this.cooldownTime = 120; + + //Create counter variable + this.tokeCounter = 0; + + //Create tokers list + this.tokers = []; + //Load in toke commands from the DB this.refreshCommands(); } @@ -33,4 +47,88 @@ module.exports = class tokebot{ this.tokeCommands = await tokeCommandModel.getCommandStrings(); } -} \ No newline at end of file + tokeProcessor(commandObj){ + const foundToke = this.tokeCommands.indexOf(commandObj.argumentArray[0]); + + //If we found a toke + if(foundToke != -1){ + //If there is no active toke or cooldown + if(this.tokeTimer == null && this.cooldownTimer == null){ + //Call-out toke start + this.chatHandler.relayTokeCallout(`A group toke has been started by ${commandObj.socket.user.user} from #${commandObj.socket.chan}! We'll be taking a toke in 60 seconds - join in by posting !${commandObj.argumentArray[0]}`); + //Set a full minute on our toke timer + this.tokeCounter = this.tokeTime; + + //Add the toking user to + this.tokers.push(commandObj.socket.user.user); + + //kick-off the count-down + this.tokeTimer = setTimeout(this.countdown.bind(this), 1000) + }else if(this.cooldownTimer == null){ + //If the tokeTimer is popping but the cooldownTimer has fucked off (a toke is in progress) + const foundToker = this.tokers.indexOf(commandObj.socket.user.user); + + + //if the user has not yet joined the toke + if(foundToker == -1){ + //Call-out toke join + this.chatHandler.relayTokeCallout(`${commandObj.socket.user.user} has joined the toke from #${commandObj.socket.chan}! Post !${commandObj.argumentArray[0]} to take part!`); + + //Add the toking user to + this.tokers.push(commandObj.socket.user.user); + }else{ + console.log('already in toke'); + } + + }else{ + //if the cooldownTimer exists (we're cooling down the toke) + console.log('cooldown toke'); + } + + //Toke command found, don't send as chat + return false; + }else{ + //No toke found, send it down the line, because shaming the user is funny + return true; + } + } + + countdown(){ + //if we have more time to go + if(this.tokeCounter > 3){ + console.log(`toke in: ${this.tokeCounter}`); + }else if(this.tokeCounter > 0){ + //Callout the last three seconds + this.chatHandler.relayTokeCallout(`${this.tokeCounter}...`); + }else{ + //If we have more than one toker + if(this.tokers.length > 1){ + this.chatHandler.relayTokeCallout(`Take a toke ${this.tokers.join(', ')}! ${this.tokers.length} tokers!`); + }else{ + this.chatHandler.relayTokeCallout(`Take a toke ${this.tokers[0]}.`); + } + + //Set the cooldown timer + this.cooldownTimer = setTimeout(this.endCooldown.bind(this), 1000 * this.cooldownTime); + + //Empty out the tokers array + this.tokers = []; + + //Null out our timer + this.tokeTimer = null; + + //return the function before it can continue + return; + } + + //Decrement toke time + this.tokeCounter--; + //try again in another second + this.tokeTimer = setTimeout(this.countdown.bind(this), 1000) + } + + endCooldown(){ + this.cooldownTimer = null; + } + +} diff --git a/src/controllers/api/admin/tokeCommandController.js b/src/controllers/api/admin/tokeCommandController.js index 2af4c40..dcb7b95 100644 --- a/src/controllers/api/admin/tokeCommandController.js +++ b/src/controllers/api/admin/tokeCommandController.js @@ -76,7 +76,7 @@ module.exports.delete = async function(req, res){ return errorHandler(res, `Cannot delete non-existant toke command '!${command}'!`); } - await tokeCommandModel.deleteOne({_id: tokeDB._id}); + await tokeDB.deleteOne(); //Return the updated command list res.status(200); diff --git a/src/schemas/channel/channelSchema.js b/src/schemas/channel/channelSchema.js index d4a3471..48c11b4 100644 --- a/src/schemas/channel/channelSchema.js +++ b/src/schemas/channel/channelSchema.js @@ -25,7 +25,7 @@ const {userModel} = require('../userSchema'); const permissionModel = require('../permissionSchema'); const channelPermissionSchema = require('./channelPermissionSchema'); const channelBanSchema = require('./channelBanSchema'); -const { exceptionHandler } = require('../../utils/loggerUtils'); +const { exceptionHandler, errorHandler } = require('../../utils/loggerUtils'); const channelSchema = new mongoose.Schema({ id: { @@ -538,7 +538,7 @@ channelSchema.methods.nuke = async function(confirm){ } //Annoyingly there isnt a good way to do this from 'this' - var oldChan = await module.exports.deleteOne({_id: this._id}); + var oldChan = await this.deleteOne(); if(oldChan.deletedCount == 0){ throw new Error("Server Error: Unable to delete channel! Please report this error to your server administrator, and with timestamp."); diff --git a/src/schemas/tokebot/tokeCommandSchema.js b/src/schemas/tokebot/tokeCommandSchema.js index 4dad7dc..4920c24 100644 --- a/src/schemas/tokebot/tokeCommandSchema.js +++ b/src/schemas/tokebot/tokeCommandSchema.js @@ -19,6 +19,7 @@ const {mongoose} = require('mongoose'); //Local Imports const defaultTokes = require("../../../defaultTokes.json"); +const server = require('../../server'); const tokeCommandSchema = new mongoose.Schema({ command:{ @@ -27,6 +28,33 @@ const tokeCommandSchema = new mongoose.Schema({ } }); +tokeCommandSchema.pre('save', async function (next){ + + //if the command was changed + if(this.isModified("command")){ + //Get server tokebot object + const tokebot = server.channelManager.chatHandler.commandPreprocessor.tokebot; + + //Pop the command on to the end + tokebot.tokeCommands.push(this.command); + } + + + //All is good, continue on saving. + next(); +}); + +tokeCommandSchema.pre('deleteOne', {document: true}, async function (next){ + //Get server tokebot object + const tokebot = server.channelManager.chatHandler.commandPreprocessor.tokebot; + + //Get the index of the command within tokeCommand and splice it out + tokebot.tokeCommands.splice(tokebot.tokeCommands.indexOf(this.command),1); + + //All is good, continue on deleting. + next(); +}); + tokeCommandSchema.statics.getCommandStrings = async function(){ //Get all toke commands in the DB const tokeDB = await this.find({}); diff --git a/src/schemas/userSchema.js b/src/schemas/userSchema.js index e608ef8..efe2cca 100644 --- a/src/schemas/userSchema.js +++ b/src/schemas/userSchema.js @@ -297,8 +297,8 @@ userSchema.methods.nuke = async function(pass){ if(this.checkPass(pass)){ //Annoyingly there isnt a good way to do this from 'this' - var oldUser = await module.exports.userModel.deleteOne(this); - + var oldUser = await this.deleteOne(); + if(oldUser){ await this.killAllSessions("If you're seeing this, your account has been deleted. So long, and thanks for all the fish! <3"); }else{