commandPreprocessor.js now scrapes links out of chat message before sending to server.

This commit is contained in:
rainbow napkin 2024-12-15 22:00:08 -05:00
parent 805387b3da
commit 4474cc4b6d

View file

@ -9,20 +9,22 @@ class commandPreprocessor{
this.command = command; this.command = command;
this.sendFlag = true; this.sendFlag = true;
this.splitBody();
this.processLocalCommand(); this.processLocalCommand();
this.sendRemoteCommand();
if(this.sendFlag){
this.message = command;
this.processLinks();
this.sendRemoteCommand();
}
} }
splitBody(){ processLocalCommand(){
//Create an empty array to hold the command //Create an empty array to hold the command
this.commandArray = []; this.commandArray = [];
//Split string by words //Split string by words
this.commandArray = this.command.split(/\b/g);//Split by word-borders this.commandArray = this.command.split(/\b/g);//Split by word-borders
this.argumentArray = this.command.match(/\b\w+\b/g);//Match by words surrounded by borders this.argumentArray = this.command.match(/\b\w+\b/g);//Match by words surrounded by borders
}
processLocalCommand(){
//If this is a local command //If this is a local command
if(this.commandArray[0] == '/'){ if(this.commandArray[0] == '/'){
//If the command exists //If the command exists
@ -36,10 +38,30 @@ class commandPreprocessor{
} }
} }
processLinks(){
//Split message by links
var splitMessage = this.message.split(/(https?:\/\/[^\s]+)/g);
//Create an empty array to hold links
this.links = [];
splitMessage.forEach((chunk, chunkIndex) => {
if(chunk.match(/(https?:\/\/[^\s]+)/g)){
//I looked online for obscure characters that no one would use to prevent people from chatting embed placeholders
//Then I found this fucker, turns out it's literally made for the job lmao (even if it was originally intended for paper/magnetic tape)
//Replace link with indexed placeholder
splitMessage[chunkIndex] = `${this.links.length}`
//push current chunk as link
this.links.push(chunk);
}
});
//Join the message back together
this.message = splitMessage.join('');
}
sendRemoteCommand(){ sendRemoteCommand(){
if(this.sendFlag){ this.client.socket.emit("chatMessage",{msg: this.message, links: this.links});
this.client.socket.emit("chatMessage",{msg: this.commandArray.join("")});
}
} }