Skip to content

Commit

Permalink
Merge pull request #157 from Cidan/master
Browse files Browse the repository at this point in the history
Added emote position extraction to emote parse.
  • Loading branch information
gempir authored Jul 29, 2021
2 parents 2ffb395 + 91f7dc1 commit 32e2218
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
48 changes: 41 additions & 7 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,18 @@ func init() {
}
}

// EmotePosition is a single position of an emote to be used for text replacement.
type EmotePosition struct {
Start int
End int
}

// Emote twitch emotes
type Emote struct {
Name string
ID string
Count int
Name string
ID string
Count int
Positions []EmotePosition
}

// ParseMessage parse a raw Twitch IRC message
Expand Down Expand Up @@ -484,13 +491,14 @@ func parseEmotes(rawEmotes, message string) []*Emote {

runes := []rune(message)

L:
for _, v := range strings.Split(rawEmotes, "/") {
split := strings.SplitN(v, ":", 2)
if len(split) != 2 {
// We have received bad emote data :(
continue
}
pairs := strings.SplitN(split[1], ",", 2)
pairs := strings.Split(split[1], ",")
if len(pairs) < 1 {
// We have received bad emote data :(
continue
Expand All @@ -512,10 +520,36 @@ func parseEmotes(rawEmotes, message string) []*Emote {
firstIndex = len(runes) - 1
}

var positions []EmotePosition
for _, p := range pairs {
pos := strings.SplitN(p, "-", 2)
if len(pos) != 2 {
// Position is not valid, continue on the outer loop and skip this emote.
continue L
}

// Convert the start and end positions from strings, and bail if it fails.
start, err := strconv.Atoi(pos[0])
if err != nil {
continue L
}

end, err := strconv.Atoi(pos[1])
if err != nil {
continue L
}

positions = append(positions, EmotePosition{
Start: start,
End: end,
})
}

emote := &Emote{
Name: string(runes[firstIndex : lastIndex+1]),
ID: split[0],
Count: strings.Count(split[1], ",") + 1,
Name: string(runes[firstIndex : lastIndex+1]),
ID: split[0],
Count: strings.Count(split[1], ",") + 1,
Positions: positions,
}

emotes = append(emotes, emote)
Expand Down
1 change: 1 addition & 0 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ func TestCanParseEmoteMessage(t *testing.T) {
assertStringsEqual(t, "120232", privateMessage.Emotes[0].ID)
assertStringsEqual(t, "TriHard", privateMessage.Emotes[0].Name)
assertIntsEqual(t, 5, privateMessage.Emotes[0].Count)
assertIntsEqual(t, 5, len(privateMessage.Emotes[0].Positions))
}

func TestCanHandleBadEmoteMessage(t *testing.T) {
Expand Down

0 comments on commit 32e2218

Please sign in to comment.