Skip to content

Commit

Permalink
Ignore messages that cannot be parsed
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattie112 committed Jan 30, 2022
1 parent 859ebb5 commit 2948169
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM alpine:latest
COPY ./bin/factorigo-chat-bot /factorigo-chat-bot
WORKDIR /opt/project
COPY ./bin/factorigo-chat-bot /opt/project/factorigo-chat-bot

ENTRYPOINT ["factorigo-chat-bot"]
ENTRYPOINT ["/opt/project/factorigo-chat-bot"]
20 changes: 14 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func main() {
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 function small
Expand All @@ -91,14 +96,14 @@ func parseAndFormatMessage(message string) string {
match := re.FindStringSubmatch(message)
return fmt.Sprintf(":red_circle: | `%s` left the game!", match[1])
default:
log.WithField("message", message).Warning("Could not parse message from Factorio, sending raw message to Discord")
return message
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(`(?m) \[(.*):`)
var re = regexp.MustCompile(`(?mU) \[(.*):`)
messageType := re.FindStringSubmatch(message)
switch messageType[1] {
case "RESEARCH_STARTED":
Expand All @@ -114,8 +119,8 @@ func parseModLogEntries(message string) string {
match := re.FindStringSubmatch(message)
return fmt.Sprintf(":skull: | Player died: `%s`", match[1])
default:
log.WithField("message", message).Warning("Could not parse message from mod, sending raw message to Discord")
return message
log.WithField("message", message).Debug("Could not parse message from mod, ignoring")
return ""
}
}

Expand Down Expand Up @@ -177,7 +182,10 @@ func readFactorioLogFile() {
log.Debug("Trigger to read Factorio logfile")
line := getLastLineWithSeek(fileName)
log.WithFields(logrus.Fields{"line": line}).Debug("Read line from Factorio log")
messagesToDiscord <- parseAndFormatMessage(line)
message := parseAndFormatMessage(line)
if message != "" {
messagesToDiscord <- message
}
}
}

Expand Down

0 comments on commit 2948169

Please sign in to comment.