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();
}
}

View file

@ -0,0 +1,77 @@
/*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/>.*/
//NPM Imports
const {mongoose} = require('mongoose');
//Local Imports
const defaultTokes = require("../../../defaultTokes.json");
const tokeCommandSchema = new mongoose.Schema({
command:{
type: mongoose.SchemaTypes.String,
required: true
}
});
tokeCommandSchema.statics.getCommandStrings = async function(){
//Get all toke commands in the DB
const tokeDB = await this.find({});
//Create an empty array to hold the toke commands
var tokeArray = [];
//for all toke commands found in the database
tokeDB.forEach((toke)=>{
//Push the command string into the tokeArray
tokeArray.push(toke.command);
})
//return the toke command strings from the database
return tokeArray;
}
tokeCommandSchema.statics.loadDefaults = async function(){
//Make sure registerToke function is happy
const _this = this;
//Ensure default comes first (.bind(this) doesn't seem to work here...)
await registerToke(defaultTokes.default);
//For each entry in the defaultTokes.json file
defaultTokes.array.forEach(registerToke);
async function registerToke(toke){
try{
//Look for toke matching the one from our file
const foundToke = await _this.findOne({command: toke});
//if the toke doesn't exist
if(!foundToke){
const tokeDB = await _this.create({command: toke});
console.log(`Loading default toke command '!${toke}' into DB from defaultTokes.json`);
}
}catch(err){
if(toke != null){
console.log(err);
console.log(`Error loading toke command: '!${toke}'`);
}else{
console.log("Error, null toke!");
}
}
}
}
module.exports = mongoose.model("tokeCommand", tokeCommandSchema);

View file

@ -28,6 +28,7 @@ const channelManager = require('./app/channel/channelManager');
const scheduler = require('./utils/scheduler');
const statModel = require('./schemas/statSchema');
const flairModel = require('./schemas/flairSchema');
const tokeCommandModel = require('./schemas/tokebot/tokeCommandSchema');
const indexRouter = require('./routers/indexRouter');
const registerRouter = require('./routers/registerRouter');
const profileRouter = require('./routers/profileRouter');
@ -110,9 +111,12 @@ app.use(express.static(path.join(__dirname, '../www')));
//Increment launch counter
statModel.incrementLaunchCount();
//Load flairs
//Load default flairs
flairModel.loadDefaults();
//Load default toke commands
tokeCommandModel.loadDefaults();
//Kick off scheduled-jobs
scheduler.kickoff();