-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
435 lines (383 loc) · 12.5 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/forPelevin/gomoji"
"github.com/forewing/csgo-rcon"
"github.com/joho/godotenv"
"github.com/nxadm/tail"
"github.com/sirupsen/logrus"
"io"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"syscall"
"time"
)
var (
log *logrus.Logger
messagesToDiscord chan string
messagesToFactorio chan string
discordActivities chan discordgo.Activity
commands chan string
discordChannelId string
playersOnline int
seed string
tailFile *tail.Tail
// VERSION These can be injected at build time -ldflags "-InputArgs main.VERSION=dev main.BUILD_TIME=201610251410"
VERSION = "Undefined"
// BUILDTIME These can be injected at build time -ldflags "-InputArgs main.VERSION=dev main.BUILD_TIME=201610251410"
BUILDTIME = "Undefined"
config sConfig
)
func main() {
// If we have an .env file -> load it
if _, err := os.Stat(".env"); err == nil {
if err := godotenv.Load(); err != nil {
panic("Could not load .env file")
}
}
log = getLoggerFromConfig(os.Getenv("LOG_LEVEL"))
log.Infof("Starting FactoriGO Chat Bot %s - %s", VERSION, BUILDTIME)
checkRequiredEnvVariables()
config = loadConfig() // Load optional config
discordChannelId = os.Getenv("DISCORD_CHANNEL_ID")
playersOnline = 0
messagesToDiscord = make(chan string)
messagesToFactorio = make(chan string)
commands = make(chan string)
discord := setUpDiscord()
rconClient := setUpRCON()
// Setup file watchers
go readFactorioLogFile(os.Getenv("FACTORIO_LOG"))
if os.Getenv("MOD_LOG") != "" {
go readFactorioLogFile(os.Getenv("MOD_LOG"))
}
// Start functions that handle the dataflow
go sendMessageToFactorio(rconClient)
go sendMessageToDiscord(discord)
go handleCommands(rconClient)
// Setup recurring tasks
periodicTasks := schedule(60*time.Second, func() {
updatePlayerCount(rconClient)
})
// Keep running until getting exit signal
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanup
close(periodicTasks)
close(messagesToDiscord)
close(messagesToFactorio)
close(commands)
_ = discord.Close()
}
func loadConfig() sConfig {
return sConfig{allRocketLaunches: getenvBool("ALL_ROCKET_LAUNCHES")}
}
// Parse the message and format it in a way for Discord
func parseAndFormatMessage(message string) string {
var re = regexp.MustCompile(`(?m)\[(\w*)]`)
messageType := re.FindStringSubmatch(message)
if len(messageType) < 2 {
return ""
}
switch messageType[1] {
case "FactoriGOChatBot":
// Extracted to keep this function small
return parseModLogEntries(message)
case "CHAT":
var re = regexp.MustCompile(`(?m)] (.*): (.*)`)
match := re.FindStringSubmatch(message)
// Ignore GPS (= map pings)
messageContent := match[2]
if strings.Contains(messageContent, "[gps=") {
return ""
}
return fmt.Sprintf(":speech_left: | `%s`: %s", match[1], messageContent)
case "JOIN":
playersOnline += 1
var re = regexp.MustCompile(`(?m)] (\w*)`)
match := re.FindStringSubmatch(message)
return fmt.Sprintf(":green_circle: | `%s` joined the game!", match[1])
case "LEAVE":
playersOnline -= 1
var re = regexp.MustCompile(`(?m)] (\w*)`)
match := re.FindStringSubmatch(message)
return fmt.Sprintf(":red_circle: | `%s` left the game!", match[1])
default:
log.WithField("message", message).Debug("Could not parse message from Factorio, ignoring")
return ""
}
}
// With the companion mod (or manual edit of save game) we can extract extra information!
func parseModLogEntries(message string) string {
var re = regexp.MustCompile(`(?mU) \[(.*):`)
messageType := re.FindStringSubmatch(message)
if len(messageType) < 1 {
return ""
}
switch messageType[1] {
case "RESEARCH_STARTED":
var re = regexp.MustCompile(`(?m):(\S*)]`)
match := re.FindStringSubmatch(message)
return fmt.Sprintf(":microscope: | Research started: `%s`", match[1])
case "RESEARCH_FINISHED":
var re = regexp.MustCompile(`(?m):(\S*)]`)
match := re.FindStringSubmatch(message)
updateDiscordStatus(discordgo.ActivityTypeListening, match[1])
return fmt.Sprintf(":microscope: | Research finished: `%s`", match[1])
case "PLAYER_DIED":
var re = regexp.MustCompile(`(?m):([\w -]*)+`)
match := re.FindAllStringSubmatch(message, -1)
updateDiscordStatus(discordgo.ActivityTypeWatching, match[1][1]+" dying")
// No cause
if len(match) == 2 {
return fmt.Sprintf(":skull: | Player died: `%s` (unknown cause)", match[1][1])
}
cause := match[2][1]
addText := ""
if cause == "locomotive" || cause == "cargo-wagon" || cause == "artillery-wagon" || cause == "fluid-wagon" {
addText = " (hahaha!)"
}
if cause == "cargo-wagon" || cause == "artillery-wagon" || cause == "fluid-wagon" {
addText = " (hahaha! how the hell did you do that?!?!)"
}
if cause == "" {
cause = "unknown"
}
// Only cause (companion mod <= 0.5.0
if len(match) == 3 {
return fmt.Sprintf(":skull: | Player died: `%s`, cause: `%s`%s", match[1][1], cause, addText)
}
// Cause and death count (companion mod >= 0.6.0)
if len(match) == 5 {
return fmt.Sprintf(":skull: | Player died: `%s`, cause: `%s`%s (%s times out of %s deaths)", match[1][1], cause, addText, match[3][1], match[4][1])
}
return ""
case "ROCKET_LAUNCHED":
updateDiscordStatus(discordgo.ActivityTypeWatching, "a rocket launch")
var re = regexp.MustCompile(`(?m):(\d*)]`)
match := re.FindStringSubmatch(message)
launchAmount, _ := strconv.Atoi(match[1])
if config.allRocketLaunches {
return fmt.Sprintf(":rocket: :rocket: :rocket: A rocket has been launched! (%d times)", launchAmount)
} else {
switch {
case launchAmount <= 5:
fallthrough
case launchAmount >= 10 && launchAmount < 100 && launchAmount%10 == 0:
fallthrough
case launchAmount >= 100 && launchAmount%100 == 0:
return fmt.Sprintf(":rocket: :rocket: :rocket: A rocket has been launched! (%d times)", launchAmount)
}
}
return ""
default:
log.WithField("message", message).Debug("Could not parse message from mod, ignoring")
return ""
}
}
func sendMessageToDiscord(discord *discordgo.Session) {
for message := range messagesToDiscord {
_, err := discord.ChannelMessageSend(discordChannelId, message)
if err != nil {
log.WithError(err).WithFields(logrus.Fields{"message": message}).Error("Failed to post message to Discord")
}
}
}
func sendMessageToFactorio(rconClient *rcon.Client) {
log.Debugf("Setting up message handler")
for message := range messagesToFactorio {
message = strings.Replace(message, "'", "\\'", -1)
cmd := "/silent-command game.print('[color=#7289DA][Discord]" + message + "[/color]')"
log.WithFields(logrus.Fields{"cmd": cmd}).Debug("Sending command to Factorio (through RCON)")
_, err := rconClient.Execute(cmd)
if err != nil {
log.WithError(err).WithFields(logrus.Fields{"cmd": cmd}).Error("Unable to send message to Factorio")
}
}
}
func onReceiveDiscordMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messagesToDiscord created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
// Only listen on our Factorio channel
if m.ChannelID != discordChannelId {
return
}
log.WithFields(logrus.Fields{"message": m.Content, "author": m.Author}).Debug("Received message on Discord")
// If the message is "ping" reply with "Pong!"
if m.Content == "ping" {
_, err := s.ChannelMessageSend(m.ChannelID, "Pong!")
if err != nil {
log.WithError(err).WithFields(logrus.Fields{"message": m.Content}).Error("Failed to send message to Discord")
}
}
// Send message away!
nick := m.Member.Nick
if nick == "" {
nick = m.Author.Username
}
// Parse message (and handle multilines)
messages := parseDiscordMessage(m.Content)
log.WithFields(logrus.Fields{"messages": messages}).Debugf("Sending Discord message to output channel")
for _, message := range messages {
if len(message) > 0 && message[0:1] == "!" {
commands <- message
}
messagesToFactorio <- fmt.Sprintf("[%s]: %s", nick, message)
}
}
func parseDiscordMessage(message string) []string {
if gomoji.ContainsEmoji(message) {
res := gomoji.FindAll(message)
for _, emoji := range res {
message = strings.Replace(message, emoji.Character, "**"+emoji.Slug+"**", -1)
}
}
messages := strings.Split(message, "\n")
return messages
}
// Read the last line of a file and puts the parsed message on our output channel
func readFactorioLogFile(filename string) {
seek := tail.SeekInfo{
Offset: 0,
Whence: io.SeekEnd,
}
var err error
tailFile, err = tail.TailFile(filename, tail.Config{
Follow: true,
ReOpen: true,
MustExist: true,
Poll: os.Getenv("POLL_LOG") != "",
Location: &seek,
})
if err != nil {
log.WithError(err).Error("Failed to open mod log file")
return
}
for line := range tailFile.Lines {
if line.Err != nil {
log.WithError(line.Err).Error("Error while tailing log file")
continue
}
log.WithFields(logrus.Fields{"line": line.Text}).Debug("Read line from Factorio log")
message := parseAndFormatMessage(line.Text)
if message != "" {
messagesToDiscord <- message
}
}
}
func updateDiscordStatus(activityType discordgo.ActivityType, name string) {
discordActivities <- discordgo.Activity{
Name: name,
Type: activityType,
}
}
func sendDiscordStatusUpdates(discord *discordgo.Session) {
for activity := range discordActivities {
// Set game status
idle := 0
err := discord.UpdateStatusComplex(discordgo.UpdateStatusData{
IdleSince: &idle,
Activities: []*discordgo.Activity{&activity},
AFK: false,
})
if err != nil {
log.WithError(err).Errorln("Failed to update Discord status")
}
log.Debugln("Updated status to " + activityToStatus(&activity))
}
}
func setUpRCON() *rcon.Client {
rconIp := os.Getenv("RCON_IP")
rconPort := os.Getenv("RCON_PORT")
rconPassword := os.Getenv("RCON_PASSWORD")
rconClient := rcon.New(rconIp+":"+rconPort, rconPassword, time.Second*2)
updatePlayerCount(rconClient)
return rconClient
}
func getSeedFromFactorio(rconClient *rcon.Client) string {
if seed != "" {
return seed
}
msg, err := rconClient.Execute("/seed")
if err != nil {
log.WithFields(logrus.Fields{"err": err}).Error("Could not get seed from Factorio")
return "Unknown"
}
seed = msg
return seed
}
func updatePlayerCount(rconClient *rcon.Client) {
msg, err := rconClient.Execute("/players online count")
if err != nil {
log.WithFields(logrus.Fields{"err": err}).Error("Could not get player count from Factorio")
return
}
playersOnline, err = strconv.Atoi(strings.Split(strings.Split(msg, "(")[1], ")")[0])
if err != nil {
log.WithFields(logrus.Fields{"err": err}).Panic("Could not parse player count from Factorio")
return
}
if playersOnline > 0 {
updateDiscordStatus(discordgo.ActivityTypeWatching, "the factory grow")
} else {
updateDiscordStatus(discordgo.ActivityTypeWatching, "the world burn")
}
}
func setUpDiscord() *discordgo.Session {
discordToken := os.Getenv("DISCORD_TOKEN")
discord, err := discordgo.New("Bot " + discordToken)
if err != nil {
log.WithFields(logrus.Fields{"err": err, "token": discordToken}).Panic("Could register bot with Discord")
}
// Listen to incoming messagesToDiscord from Discord
discord.AddHandler(onReceiveDiscordMessage)
discord.Identify.Intents = discordgo.IntentsGuildMessages
// Open socket to Discord
if discord.Open() != nil {
log.WithFields(logrus.Fields{"err": err}).Panic("Cannot open socket to Discord")
}
log.Infoln("Bot registered by Discord and is now listening for messagesToDiscord")
discordActivities = make(chan discordgo.Activity)
go sendDiscordStatusUpdates(discord)
// Set initial status
updateDiscordStatus(discordgo.ActivityTypeWatching, "the world burn")
return discord
}
func handleCommands(rconClient *rcon.Client) {
for command := range commands {
switch command {
case "!online":
messagesToDiscord <- strconv.Itoa(playersOnline) + " players online"
break
case "!seed":
messagesToDiscord <- getSeedFromFactorio(rconClient)
break
case "!evolution":
msg, err := rconClient.Execute("/evolution")
if err != nil {
log.WithFields(logrus.Fields{"err": err}).Error("Could not get evolution from Factorio")
msg = "Unknown"
}
messagesToDiscord <- msg
break
}
}
}
func checkRequiredEnvVariables() {
vars := []string{"DISCORD_TOKEN", "DISCORD_CHANNEL_ID", "RCON_IP", "RCON_PORT", "RCON_PASSWORD", "FACTORIO_LOG"}
for _, envVar := range vars {
if os.Getenv(envVar) == "" {
log.WithField("envVar", envVar).Fatal("Could not find required ENV VAR")
}
}
}
type sConfig struct {
allRocketLaunches bool
}