@@ -31,98 +31,227 @@ func WriteToLogFile(logPath string, line string) error {
31
31
return nil
32
32
}
33
33
34
- // Action when a player message is detected
35
- func PlayerMessageAction (line string , serverID int ) error {
36
- // Player name
34
+ // Define the functions for each game, here is Minecraft
35
+ func handleMinecraftPlayerMessage (line string ) (string , string , string , string , error ) {
37
36
playerChatRegex := regexp .MustCompile (`\[(\d{2}:\d{2}:\d{2})\] \[Server thread/INFO](?: \[.+?/MinecraftServer])?: <(.+?)> (.+)` )
38
37
matches := playerChatRegex .FindStringSubmatch (line )
39
38
if len (matches ) < 4 {
40
- return fmt .Errorf ("ERROR WHILE EXTRACTING CHAT PLAYER NAME... MAYBE IT'S NOT A PLAYER MESSAGE ? LINE: %v" , line )
39
+ return "" , "" , "" , "" , fmt .Errorf ("ERROR WHILE EXTRACTING CHAT PLAYER NAME FOR MINECRAFT" )
40
+ }
41
+ playerName := matches [2 ]
42
+ message := matches [3 ]
43
+
44
+ // Get player UUID and head URL for Minecraft
45
+ playerUUID , err := services .GetMinecraftPlayerUUID (playerName )
46
+ if err != nil {
47
+ return "" , "" , "" , "" , fmt .Errorf ("ERROR WHILE GETTING PLAYER UUID: %v" , err )
48
+ }
49
+
50
+ playerHeadURL , err := services .GetMinecraftPlayerHeadURL (playerUUID )
51
+ if err != nil {
52
+ return "" , "" , "" , "" , fmt .Errorf ("ERROR WHILE GETTING PLAYER HEAD URL: %v" , err )
41
53
}
54
+
55
+ titleURL := "https://fr.namemc.com/profile/" + playerUUID
56
+ return playerName , message , playerHeadURL , titleURL , nil
57
+ }
58
+
59
+ // Define the functions for each game, here is Palworld
60
+ func handlePalworldPlayerMessage (line string ) (string , string , string , string , error ) {
61
+ playerChatRegex := regexp .MustCompile (`\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] \[CHAT\] <(.+?)> (.+)` )
62
+ matches := playerChatRegex .FindStringSubmatch (line )
63
+ if len (matches ) < 3 {
64
+ return "" , "" , "" , "" , fmt .Errorf ("ERROR WHILE EXTRACTING CHAT PLAYER NAME FOR PALWORLD" )
65
+ }
66
+ playerName := matches [1 ]
67
+ message := matches [2 ]
68
+ return playerName , message , "" , "" , nil
69
+ }
70
+
71
+ // Create a map for game-specific actions
72
+ var gameActionsMap = map [string ]func (string ) (string , string , string , string , error ){
73
+ "Minecraft" : handleMinecraftPlayerMessage ,
74
+ "Palworld" : handlePalworldPlayerMessage ,
75
+ }
76
+
77
+ // Action when a player message is detected
78
+ func PlayerMessageAction (line string , serverID int ) error {
42
79
// Server infos
43
80
server , err := db .GetServerById (serverID )
44
81
if err != nil {
45
82
return fmt .Errorf ("ERROR WHILE GETTING SERVER BY ID FOR PLAYER MESSAGE: %v" , err )
46
83
}
47
- // Action
48
- playerName := matches [2 ]
49
- message := matches [3 ]
50
84
51
- playerUUID , err := services .GetMinecraftPlayerUUID (playerName ) // Get player UUID
52
- if err != nil {
53
- return fmt .Errorf ("ERROR WHILE GETTING PLAYER UUID: %v" , err )
85
+ // Get the appropriate game action function from the map
86
+ actionFunc , exists := gameActionsMap [server .Jeu ]
87
+ if ! exists {
88
+ return fmt .Errorf ("ERROR: SERVER GAME %v IS NOT SUPPORTED" , server .Jeu )
54
89
}
55
90
56
- playerHeadURL , err := services .GetMinecraftPlayerHeadURL (playerUUID ) // Get player head URL
91
+ // Call the specific action function for the game
92
+ playerName , message , playerHeadURL , titleURL , err := actionFunc (line )
57
93
if err != nil {
58
- return fmt . Errorf ( "ERROR WHILE GETTING PLAYER HEAD URL: %v" , err )
94
+ return err
59
95
}
60
96
97
+ // Bot config
98
+ botName := "mineotterBot" // Can be extended similarly using the mappage if needed
99
+
100
+ // Send the Discord embed message
61
101
embed := models.EmbedConfig {
62
102
Title : playerName ,
63
- TitleURL : "https://fr.namemc.com/profile/" + playerUUID ,
103
+ TitleURL : titleURL ,
64
104
Description : message ,
65
105
Color : server .EmbedColor ,
66
106
Thumbnail : playerHeadURL ,
67
- MainImage : "" ,
68
107
Footer : "Message venant de " + server .Nom ,
69
- Author : "" ,
70
- AuthorIcon : "" ,
71
- Timestamp : false ,
72
108
}
73
- err = discord .SendDiscordEmbedWithModel (config .AppConfig .Bots ["mineotterBot" ], config .AppConfig .DiscordChannels .MinecraftChatChannelID , embed )
109
+
110
+ err = discord .SendDiscordEmbedWithModel (config .AppConfig .Bots [botName ], config .AppConfig .DiscordChannels .MinecraftChatChannelID , embed )
74
111
if err != nil {
75
112
return fmt .Errorf ("ERROR WHILE SENDING DISCORD EMBED: %v" , err )
76
113
}
77
114
return nil
78
115
}
79
116
80
- // Action when a player joined the server
81
- func PlayerJoinedAction (line string , serverID int ) error {
82
- // Player name
117
+ // Define the functions for each game, here is Minecraft
118
+ func handleMinecraftPlayerJoined (line string ) (string , error ) {
83
119
playerJoinedRegex := regexp .MustCompile (`\[(\d{2}:\d{2}:\d{2})\] \[Server thread/INFO](?: \[.+?/MinecraftServer])?: (.+) joined the game` )
84
120
matches := playerJoinedRegex .FindStringSubmatch (line )
85
121
if len (matches ) < 3 {
86
- return fmt .Errorf ("ERROR WHILE EXTRACTING JOINED PLAYER NAME" )
122
+ return "" , fmt .Errorf ("ERROR WHILE EXTRACTING JOINED PLAYER NAME FOR MINECRAFT SERVER" )
123
+ }
124
+ return matches [2 ], nil
125
+ }
126
+
127
+ // Define the functions for each game, here is Palworld
128
+ func handlePalworldPlayerJoined (line string ) (string , error ) {
129
+ playerJoinedRegex := regexp .MustCompile (`\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] \[LOG\] (.+?) \d{1,3}(?:\.\d{1,3}){3} connected the server` )
130
+ matches := playerJoinedRegex .FindStringSubmatch (line )
131
+ if len (matches ) < 2 {
132
+ return "" , fmt .Errorf ("ERROR WHILE EXTRACTING JOINED PLAYER NAME FOR PALWORLD SERVER" )
87
133
}
134
+ return matches [1 ], nil
135
+ }
136
+
137
+ // Create a map for game-specific actions for joined and left actions
138
+ var gameJoinActionsMap = map [string ]func (string ) (string , error ){
139
+ "Minecraft" : handleMinecraftPlayerJoined ,
140
+ "Palworld" : handlePalworldPlayerJoined ,
141
+ }
142
+
143
+ // Action when a player joined the server
144
+ func PlayerJoinedAction (line string , serverID int ) error {
88
145
// Server infos
89
146
server , err := db .GetServerById (serverID )
90
147
if err != nil {
91
- return fmt .Errorf ("ERROR WHILE GETTING SERVER BY ID FOR MINECRAFT SERVER STOPPED : %v" , err )
148
+ return fmt .Errorf ("ERROR WHILE GETTING SERVER BY ID FOR PLAYER JOINED : %v" , err )
92
149
}
93
- // Action
94
- discord .SendDiscordEmbed (config .AppConfig .Bots ["mineotterBot" ], config .AppConfig .DiscordChannels .MinecraftChatChannelID , matches [2 ]+ " à rejoint " + server .Nom , "" , server .EmbedColor )
95
- playerID , err := db .CheckAndInsertPlayerWithPlayerName (matches [2 ], 1 , "now" )
150
+
151
+ // Get the appropriate game action function from the map for "joined" action
152
+ actionFunc , exists := gameJoinActionsMap [server .Jeu ]
153
+ if ! exists {
154
+ return fmt .Errorf ("ERROR: SERVER GAME %v IS NOT SUPPORTED" , server .Jeu )
155
+ }
156
+
157
+ // Call the specific action function for the game
158
+ playerName , err := actionFunc (line )
159
+ if err != nil {
160
+ return err
161
+ }
162
+
163
+ // Bot config
164
+ var botName string
165
+ if server .Jeu == "Minecraft" {
166
+ botName = "mineotterBot"
167
+ } else {
168
+ botName = "multiloutreBot"
169
+ }
170
+
171
+ // Send the Discord embed message
172
+ discord .SendDiscordEmbed (config .AppConfig .Bots [botName ], config .AppConfig .DiscordChannels .MinecraftChatChannelID , playerName + " a rejoint " + server .Nom , "" , server .EmbedColor )
173
+
174
+ // Handle player connection log in DB
175
+ playerID , err := db .CheckAndInsertPlayerWithPlayerName (playerName , 1 , "now" )
96
176
if err != nil {
97
177
return fmt .Errorf ("ERROR WHILE CHECKING OR INSERTING PLAYER: %v" , err )
98
178
}
179
+
99
180
err = db .SaveConnectionLog (playerID , serverID )
100
181
if err != nil {
101
- return fmt .Errorf ("ERROR WHILE SAVING CONNECTION LOG: FOR PLAYER %v IN DATABASE: %v" , matches [ 2 ] , err )
182
+ return fmt .Errorf ("ERROR WHILE SAVING CONNECTION LOG: FOR PLAYER %v IN DATABASE: %v" , playerName , err )
102
183
}
184
+
103
185
err = db .UpdatePlayerLastConnection (playerID )
104
186
if err != nil {
105
- return fmt .Errorf ("ERROR WHILE UPDATING LAST CONNECTION FOR PLAYER %v IN DATABASE: %v" , matches [ 2 ] , err )
187
+ return fmt .Errorf ("ERROR WHILE UPDATING LAST CONNECTION FOR PLAYER %v IN DATABASE: %v" , playerName , err )
106
188
}
107
- WriteToLogFile ("/var/log/serversentinel/playerjoined.log" , matches [2 ])
189
+
190
+ // Log to file
191
+ WriteToLogFile ("/var/log/serversentinel/playerjoined.log" , playerName )
192
+
108
193
return nil
109
194
}
110
195
111
- // Action when a player left the server
112
- func PlayerLeftAction (line string , serverID int ) error {
113
- // Player name
196
+ // Define the functions for each game, here is Minecraft
197
+ func handleMinecraftPlayerLeft (line string ) (string , error ) {
114
198
playerDisconnectedRegex := regexp .MustCompile (`\[\d{2}:\d{2}:\d{2}\] \[Server thread/INFO\].*?: ([^\s]+) (?:left the game|disconnected|lost connection)` )
115
199
matches := playerDisconnectedRegex .FindStringSubmatch (line )
116
200
if len (matches ) < 2 || len (matches [1 ]) < 3 { // Minecraft player names are at least 3 characters long, so this filter prevents false positives
117
- return fmt .Errorf ("ERROR WHILE EXTRACTING DISCONNECTED PLAYER NAME" )
201
+ return "" , fmt .Errorf ("ERROR WHILE EXTRACTING DISCONNECTED PLAYER NAME FOR MINECRAFT SERVER " )
118
202
}
203
+ return matches [1 ], nil
204
+ }
205
+
206
+ // Define the functions for each game, here is Palworld
207
+ func handlePalworldPlayerLeft (line string ) (string , error ) {
208
+ playerDisconnectedRegex := regexp .MustCompile (`\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\] \[LOG\] (.+?) left the server` )
209
+ matches := playerDisconnectedRegex .FindStringSubmatch (line )
210
+ if len (matches ) < 2 {
211
+ return "" , fmt .Errorf ("ERROR WHILE EXTRACTING LEFT PLAYER NAME FOR PALWORLD SERVER" )
212
+ }
213
+ return matches [1 ], nil
214
+ }
215
+
216
+ // Create a map for game-specific actions for joined and left actions
217
+ var gameLeaveActionsMap = map [string ]func (string ) (string , error ){
218
+ "Minecraft" : handleMinecraftPlayerLeft ,
219
+ "Palworld" : handlePalworldPlayerLeft ,
220
+ }
221
+
222
+ // Action when a player left the server
223
+ func PlayerLeftAction (line string , serverID int ) error {
119
224
// Server infos
120
225
server , err := db .GetServerById (serverID )
121
226
if err != nil {
122
- return fmt .Errorf ("ERROR WHILE GETTING SERVER BY ID FOR MINECRAFT SERVER STOPPED: %v" , err )
227
+ return fmt .Errorf ("ERROR WHILE GETTING SERVER BY ID FOR PLAYER LEFT: %v" , err )
228
+ }
229
+
230
+ // Get the appropriate game action function from the map for "left" action
231
+ actionFunc , exists := gameLeaveActionsMap [server .Jeu ]
232
+ if ! exists {
233
+ return fmt .Errorf ("ERROR: SERVER GAME %v IS NOT SUPPORTED" , server .Jeu )
234
+ }
235
+
236
+ // Call the specific action function for the game
237
+ playerName , err := actionFunc (line )
238
+ if err != nil {
239
+ return err
123
240
}
124
- // Action
125
- discord .SendDiscordEmbed (config .AppConfig .Bots ["mineotterBot" ], config .AppConfig .DiscordChannels .MinecraftChatChannelID , matches [1 ]+ " à quitté " + server .Nom , "" , server .EmbedColor )
126
- WriteToLogFile ("/var/log/serversentinel/playerdisconnected.log" , matches [1 ])
241
+
242
+ // Bot config
243
+ var botName string
244
+ if server .Jeu == "Minecraft" {
245
+ botName = "mineotterBot"
246
+ } else {
247
+ botName = "multiloutreBot"
248
+ }
249
+
250
+ // Send the Discord embed message
251
+ discord .SendDiscordEmbed (config .AppConfig .Bots [botName ], config .AppConfig .DiscordChannels .MinecraftChatChannelID , playerName + " a quitté " + server .Nom , "" , server .EmbedColor )
252
+
253
+ // Log to file
254
+ WriteToLogFile ("/var/log/serversentinel/playerdisconnected.log" , playerName )
255
+
127
256
return nil
128
257
}
0 commit comments