Added basic toke command loading/storage logic

This commit is contained in:
rainbow napkin 2024-12-10 22:23:29 -05:00
parent 47d9aac8f3
commit d516fed309
5 changed files with 505 additions and 25 deletions

View file

@ -18,6 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
const validator = require('validator');//No express here, so regular validator it is!
//Local Imports
const tokebot = require('./tokebot');
const permissionModel = require('../../schemas/permissionSchema');
const channelModel = require('../../schemas/channel/channelSchema');
@ -25,7 +26,8 @@ module.exports = class commandPreprocessor{
constructor(server, chatHandler){
this.server = server;
this.chatHandler = chatHandler;
this.commandProcessor = new commandProcessor(server, this, chatHandler);
this.commandProcessor = new commandProcessor(server, chatHandler);
this.tokebot = new tokebot(server, chatHandler);
}
async preprocess(socket, data){
@ -71,10 +73,16 @@ module.exports = class commandPreprocessor{
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){
//disable chat
this.sendFlag = false;
//Process the command
await this.commandProcessor[this.argumentArray[0].toLowerCase()]();
//Create hash table to hold information about current command
const commandObj = {
socket: this.socket,
commandArray: this.commandArray,
argumentArray: this.argumentArray,
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);
}
}
}
@ -89,53 +97,69 @@ module.exports = class commandPreprocessor{
}
class commandProcessor{
constructor(server, preprocessor, chatHandler){
constructor(server, chatHandler){
this.server = server;
this.preprocessor = preprocessor
this.chatHandler = chatHandler;
}
//Command keywords get run through .toLowerCase(), so we should use lowercase method names for command methods
whisper(){
whisper(commandObj){
//splice out our whisper
this.preprocessor.commandArray.splice(0,2);
commandObj.commandArray.splice(0,2);
//send it
this.chatHandler.relayChat(this.preprocessor.socket, this.preprocessor.commandArray.join(''), 'whisper');
this.chatHandler.relayChat(commandObj.socket, commandObj.commandArray.join(''), 'whisper');
return;
//Make sure to throw the send flag
return false;
}
async announce(){
async announce(commandObj){
//Get the current channel from the database
const chanDB = await channelModel.findOne({name: this.preprocessor.socket.chan});
const chanDB = await channelModel.findOne({name: commandObj.socket.chan});
//Check if the user has permission, and publicly shame them if they don't (lmao)
if(!(this.preprocessor.sendFlag = !(await chanDB.permCheck(this.preprocessor.socket.user, 'announce')))){
if(chanDB != null && await chanDB.permCheck(commandObj.socket.user, 'announce')){
//splice out our whisper
this.preprocessor.commandArray.splice(0,2);
commandObj.commandArray.splice(0,2);
//send it
this.chatHandler.relayChannelAnnouncement(this.preprocessor.socket.chan, this.preprocessor.commandArray.join(''));
this.chatHandler.relayChannelAnnouncement(commandObj.socket.chan, commandObj.commandArray.join(''));
//throw send flag
return false;
}
//throw send flag
return true;
}
async serverannounce(){
async serverannounce(commandObj){
//Check if the user has permission, and publicly shame them if they don't (lmao)
if(!(this.preprocessor.sendFlag = !(await permissionModel.permCheck(this.preprocessor.socket.user, 'announce')))){
if(await permissionModel.permCheck(commandObj.socket.user, 'announce')){
//splice out our whisper
this.preprocessor.commandArray.splice(0,2);
commandObj.commandArray.splice(0,2);
//send it
this.chatHandler.relayServerAnnouncement(this.preprocessor.commandArray.join(''));
this.chatHandler.relayServerAnnouncement(commandObj.commandArray.join(''));
//throw send flag
return false;
}
//throw send flag
return true;
}
async clear(){
async clear(commandObj){
//Get the current channel from the database
const chanDB = await channelModel.findOne({name: this.preprocessor.socket.chan});
const chanDB = await channelModel.findOne({name: commandObj.socket.chan});
//Check if the user has permission, and publicly shame them if they don't (lmao)
if(!(this.preprocessor.sendFlag = !(await chanDB.permCheck(this.preprocessor.socket.user, 'clearChat')))){
if(await chanDB.permCheck(commandObj.socket.user, 'clearChat')){
//Send off the command
this.chatHandler.clearChat(this.preprocessor.socket.chan, this.preprocessor.argumentArray[1]);
this.chatHandler.clearChat(commandObj.socket.chan, commandObj.argumentArray[1]);
//throw send flag
return false;
}
//throw send flag
return true;
}
}

View file

@ -0,0 +1,36 @@
/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024 Rainbownapkin and the TTN Community
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//Local Imports
const tokeCommandModel = require('../../schemas/tokebot/tokeCommandSchema');
module.exports = class tokebot{
constructor(server, chatHandler){
//Set parents
this.server = server;
this.chatHandler = chatHandler;
//Load in toke commands from the DB
this.refreshCommands();
}
async refreshCommands(){
//Pull Command Strings from DB
this.tokeCommands = await tokeCommandModel.getCommandStrings();
}
}