@@ -25,6 +25,8 @@ class ServerFifoManager {
2525 usedFifos : ServerFifo [ ] ;
2626 unusedFifos : ServerFifo [ ] ;
2727 client : Comfy | null ;
28+ static CommandRegex = / ^ (?< command > \/ ) * (?< text > .* ) $ / i;
29+
2830 constructor ( ) {
2931 this . usedFifos = [ ] ;
3032 this . unusedFifos = [ ] ;
@@ -75,17 +77,32 @@ class ServerFifoManager {
7577 }
7678 }
7779
80+ private escapeCommandCharacters ( author , command : string ) : string {
81+ const match = command . match ( ServerFifoManager . CommandRegex ) ;
82+ if ( ! match ?. groups ?. text ) {
83+ this . client ?. logger (
84+ `[${ author . id } ] Trying to execute command through text ${ command } ` ,
85+ "warn"
86+ ) ;
87+ throw new Error ( "Trying to execute command through text" ) ;
88+ }
89+ return match . groups . text ;
90+ }
91+
7892 /**
7993 * Sends a message to a Factorio server which has the same channel ID as the message
8094 * @param {Message } message - Discord message to send to server
8195 * @param {boolean } [sendWithUsername=true] - Whether to send the message with username or not.
8296 */
8397 sendToServer ( message , sendWithUsername = true ) {
8498 let toSend ;
85- if ( sendWithUsername === true )
99+ if ( sendWithUsername ) {
86100 toSend = `${ message . author . username } : ${ message . cleanContent } ` ;
101+ toSend = this . escapeCommandCharacters ( message . author , toSend ) ;
102+ }
87103 else toSend = `${ message . cleanContent } ` ;
88104 toSend = toSend . replaceAll ( "`" , "\\`" ) ;
105+
89106 this . usedFifos . forEach ( ( server ) => {
90107 if ( server . serverObject . discordid === message . channel . id )
91108 server . serverFifo . write ( toSend , ( ) => { } ) ;
@@ -99,8 +116,10 @@ class ServerFifoManager {
99116 */
100117 sendToAll ( message : Message , sendWithUsername = true ) {
101118 let toSend ;
102- if ( sendWithUsername === true )
119+ if ( sendWithUsername ) {
103120 toSend = `${ message . author . username } : ${ message . cleanContent } ` ;
121+ toSend = this . escapeCommandCharacters ( message . author , toSend ) ;
122+ }
104123 else toSend = `${ message . cleanContent } ` ;
105124 toSend = toSend . replaceAll ( "`" , "\\`" ) ;
106125 this . usedFifos . forEach ( ( server ) => {
0 commit comments