Skip to content

Commit

Permalink
Added space to be included in death messages and added emoji parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattie112 committed Feb 24, 2022
1 parent 3685b2d commit 12dc0f2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ require (
)

require (
github.com/forPelevin/gomoji v1.1.2 // indirect
github.com/gorilla/websocket v1.4.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 // indirect
golang.org/x/sys v0.0.0-20210921065528-437939a70204 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
github.com/chzyer/test v0.0.0-20210722231415-061457976a23/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/forPelevin/gomoji v1.1.2 h1:WtOeuEBAmwsGOvsPfvufTrdqmsJhQF77nGWveFQTDd8=
github.com/forPelevin/gomoji v1.1.2/go.mod h1:Z5cUlNvnrRQPxwMxc8hmn+ZAm0p8WhqE0FvDNnF5Mkw=
github.com/forewing/csgo-rcon v1.3.0 h1:y1BwWL3mLZeXQkOY1ZGD8cq1jOYBi9UXMrBSafNj+zg=
github.com/forewing/csgo-rcon v1.3.0/go.mod h1:hD/ht7ta+ub49Jx+FiP3tI7vKRt/bDEc7vrj3/iOrjw=
github.com/forewing/gobuild v0.4.0/go.mod h1:HRJckNq8s6UAK2r5PsXhFQS+FGk0v5hvO7TmEVrAP5w=
Expand All @@ -16,6 +18,8 @@ github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
Expand Down
11 changes: 9 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/forPelevin/gomoji"
"github.com/forewing/csgo-rcon"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -120,7 +121,7 @@ func parseModLogEntries(message string) string {
match := re.FindStringSubmatch(message)
return fmt.Sprintf(":microscope: | Research finished: `%s`", match[1])
case "PLAYER_DIED":
var re = regexp.MustCompile(`(?m):(\w*)+`)
var re = regexp.MustCompile(`(?m):([\w ]*)+`)
match := re.FindAllStringSubmatch(message, -1)
if len(match) == 3 {
return fmt.Sprintf(":skull: | Player died: `%s`, cause: `%s`", match[1][1], match[2][1])
Expand Down Expand Up @@ -179,20 +180,26 @@ func onReceiveDiscordMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
}

// Send message away!
log.WithFields(logrus.Fields{"message": m.Content}).Debugf("Sending Discord message to output channel")
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 {
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
}
Expand Down
31 changes: 30 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "testing"
import (
"reflect"
"testing"
)

func Test_parseAndFormatMessage(t *testing.T) {
type args struct {
Expand All @@ -17,6 +20,7 @@ func Test_parseAndFormatMessage(t *testing.T) {
// Messages below are generated by the companion mod, but I still want them to go through the normal flow!
{"PLAYER_DIED", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie]\""}, ":skull: | Player died: `Mattie` (unknown cause)"},
{"PLAYER_DIED_CAUSE", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:locomotive]\""}, ":skull: | Player died: `Mattie`, cause: `locomotive`"},
{"PLAYER_DIED_CAUSE2", args{message: "[FactoriGOChatBot]: \"2852569 [PLAYER_DIED:Mattie:big worm]\""}, ":skull: | Player died: `Mattie`, cause: `big worm`"},
{"RESEARCH_STARTED", args{message: "[FactoriGOChatBot]: \"3045105 [RESEARCH_STARTED:nuclear-power]\""}, ":microscope: | Research started: `nuclear-power`"},
{"RESEARCH_FINISHED", args{message: "[FactoriGOChatBot]: \"3229214 [RESEARCH_FINISHED:nuclear-power]\""}, ":microscope: | Research finished: `nuclear-power`"},
// Corrupted messages (as I don't know yet how to fix the file read, so it will have a single line guaranteed
Expand All @@ -30,3 +34,28 @@ func Test_parseAndFormatMessage(t *testing.T) {
})
}
}

func Test_parseDiscordMessage(t *testing.T) {
type args struct {
message string
}
tests := []struct {
name string
args args
want []string
}{
{"Normal chat", args{message: "test"}, []string{"test"}},
{"Wave emoji", args{message: "πŸ‘‹"}, []string{"**waving-hand**"}},
{"Sweat", args{message: "πŸ˜“"}, []string{"**downcast-face-with-sweat**"}},
{"Grin", args{message: "πŸ˜€"}, []string{"**grinning-face**"}},
{"Smile (but slightly)", args{message: "πŸ™‚"}, []string{"**slightly-smiling-face**"}},
{"Cool guy", args{message: "😎"}, []string{"**smiling-face-with-sunglasses**"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseDiscordMessage(tt.args.message); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseDiscordMessage() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 12dc0f2

Please sign in to comment.