@@ -25,6 +25,8 @@ class ServerFifoManager {
25
25
usedFifos : ServerFifo [ ] ;
26
26
unusedFifos : ServerFifo [ ] ;
27
27
client : Comfy | null ;
28
+ static CommandRegex = / ^ (?< command > \/ ) * (?< text > .* ) $ / i;
29
+
28
30
constructor ( ) {
29
31
this . usedFifos = [ ] ;
30
32
this . unusedFifos = [ ] ;
@@ -75,17 +77,32 @@ class ServerFifoManager {
75
77
}
76
78
}
77
79
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
+
78
92
/**
79
93
* Sends a message to a Factorio server which has the same channel ID as the message
80
94
* @param {Message } message - Discord message to send to server
81
95
* @param {boolean } [sendWithUsername=true] - Whether to send the message with username or not.
82
96
*/
83
97
sendToServer ( message , sendWithUsername = true ) {
84
98
let toSend ;
85
- if ( sendWithUsername === true )
99
+ if ( sendWithUsername ) {
86
100
toSend = `${ message . author . username } : ${ message . cleanContent } ` ;
101
+ toSend = this . escapeCommandCharacters ( message . author , toSend ) ;
102
+ }
87
103
else toSend = `${ message . cleanContent } ` ;
88
104
toSend = toSend . replaceAll ( "`" , "\\`" ) ;
105
+
89
106
this . usedFifos . forEach ( ( server ) => {
90
107
if ( server . serverObject . discordid === message . channel . id )
91
108
server . serverFifo . write ( toSend , ( ) => { } ) ;
@@ -99,8 +116,10 @@ class ServerFifoManager {
99
116
*/
100
117
sendToAll ( message : Message , sendWithUsername = true ) {
101
118
let toSend ;
102
- if ( sendWithUsername === true )
119
+ if ( sendWithUsername ) {
103
120
toSend = `${ message . author . username } : ${ message . cleanContent } ` ;
121
+ toSend = this . escapeCommandCharacters ( message . author , toSend ) ;
122
+ }
104
123
else toSend = `${ message . cleanContent } ` ;
105
124
toSend = toSend . replaceAll ( "`" , "\\`" ) ;
106
125
this . usedFifos . forEach ( ( server ) => {
0 commit comments