Skip to content

Commit

Permalink
Merge pull request #168 from Cidan/master
Browse files Browse the repository at this point in the history
Added First Message support to Twitch user messages.
  • Loading branch information
gempir committed Nov 9, 2021
2 parents b173bfc + caf8c4a commit 61d050b
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 34 deletions.
25 changes: 13 additions & 12 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,19 @@ func (msg *WhisperMessage) GetType() MessageType {
type PrivateMessage struct {
User User

Raw string
Type MessageType
RawType string
Tags map[string]string
Message string
Channel string
RoomID string
ID string
Time time.Time
Emotes []*Emote
Bits int
Action bool
Raw string
Type MessageType
RawType string
Tags map[string]string
Message string
Channel string
RoomID string
ID string
Time time.Time
Emotes []*Emote
Bits int
Action bool
FirstMessage bool
}

// GetType implements the Message interface, and returns this message's type
Expand Down
6 changes: 6 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ func assertInt32sEqual(t *testing.T, expected, actual int32) {
}
}

func assertBoolEqual(t *testing.T, expected, actual bool) {
if expected != actual {
t.Errorf("failed asserting that \"%t\" is expected \"%t\"", actual, expected)
}
}

func assertTrue(t *testing.T, actual bool, errorMessage string) {
if !actual {
t.Error(errorMessage)
Expand Down
6 changes: 6 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ func parsePrivateMessage(message *ircMessage) Message {

privateMessage.Emotes = parseEmotes(message.Tags["emotes"], privateMessage.Message)

firstMessage, ok := message.Tags["first-msg"]

if ok {
privateMessage.FirstMessage = firstMessage == "1"
}

return &privateMessage
}

Expand Down
121 changes: 99 additions & 22 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,33 +115,110 @@ func TestCanParseWHISPERActionMessage(t *testing.T) {
}

func TestCanParsePRIVMSGMessage(t *testing.T) {
testMessage := "@badges=premium/1;color=#DAA520;display-name=FletcherCodes;emotes=;flags=;id=6efffc70-27a1-4637-9111-44e5104bb7da;mod=0;room-id=408892348;subscriber=0;tmi-sent-ts=1551473087761;turbo=0;user-id=269899575;user-type= :fletchercodes!fletchercodes@fletchercodes.tmi.twitch.tv PRIVMSG #clippyassistant :Chew your food slower... it's healthier"
type test struct {
name string
message string
expectedMessage PrivateMessage
}
var tests = []test{
{
"Message With First Message",
"@badges=premium/1;color=#DAA520;display-name=FletcherCodes;emotes=;first-msg=1;flags=;id=6efffc70-27a1-4637-9111-44e5104bb7da;mod=0;room-id=408892348;subscriber=0;tmi-sent-ts=1551473087761;turbo=0;user-id=269899575;user-type= :fletchercodes!fletchercodes@fletchercodes.tmi.twitch.tv PRIVMSG #clippyassistant :Chew your food slower... it's healthier",
PrivateMessage{
User: User{
ID: "269899575",
Name: "fletchercodes",
DisplayName: "FletcherCodes",
Color: "#DAA520",
Badges: map[string]int{
"premium": 1,
},
},
Type: PRIVMSG,
RawType: "PRIVMSG",
Message: "Chew your food slower... it's healthier",
Channel: "clippyassistant",
RoomID: "408892348",
ID: "6efffc70-27a1-4637-9111-44e5104bb7da",
FirstMessage: true,
},
},
{
"Message Without First Message",
"@badges=premium/1;color=#DAA520;display-name=FletcherCodes;emotes=;first-msg=0;flags=;id=6efffc70-27a1-4637-9111-44e5104bb7da;mod=0;room-id=408892348;subscriber=0;tmi-sent-ts=1551473087761;turbo=0;user-id=269899575;user-type= :fletchercodes!fletchercodes@fletchercodes.tmi.twitch.tv PRIVMSG #clippyassistant :Chew your food slower... it's healthier",
PrivateMessage{
User: User{
ID: "269899575",
Name: "fletchercodes",
DisplayName: "FletcherCodes",
Color: "#DAA520",
Badges: map[string]int{
"premium": 1,
},
},
Type: PRIVMSG,
RawType: "PRIVMSG",
Message: "Chew your food slower... it's healthier",
Channel: "clippyassistant",
RoomID: "408892348",
ID: "6efffc70-27a1-4637-9111-44e5104bb7da",
FirstMessage: false,
},
},
{
"Message With Missing First Message",
"@badges=premium/1;color=#DAA520;display-name=FletcherCodes;emotes=;flags=;id=6efffc70-27a1-4637-9111-44e5104bb7da;mod=0;room-id=408892348;subscriber=0;tmi-sent-ts=1551473087761;turbo=0;user-id=269899575;user-type= :fletchercodes!fletchercodes@fletchercodes.tmi.twitch.tv PRIVMSG #clippyassistant :Chew your food slower... it's healthier",
PrivateMessage{
User: User{
ID: "269899575",
Name: "fletchercodes",
DisplayName: "FletcherCodes",
Color: "#DAA520",
Badges: map[string]int{
"premium": 1,
},
},
Type: PRIVMSG,
RawType: "PRIVMSG",
Message: "Chew your food slower... it's healthier",
Channel: "clippyassistant",
RoomID: "408892348",
ID: "6efffc70-27a1-4637-9111-44e5104bb7da",
FirstMessage: false,
},
},
}

message := ParseMessage(testMessage)
privateMessage := message.(*PrivateMessage)
user := privateMessage.User
for _, tt := range tests {
func(tt test) {
t.Run(tt.name, func(t *testing.T) {
message := ParseMessage(tt.message)
privateMessage := message.(*PrivateMessage)
user := privateMessage.User

assertStringsEqual(t, "269899575", user.ID)
assertStringsEqual(t, "fletchercodes", user.Name)
assertStringsEqual(t, "FletcherCodes", user.DisplayName)
assertStringsEqual(t, "#DAA520", user.Color)
assertStringsEqual(t, tt.expectedMessage.User.ID, user.ID)
assertStringsEqual(t, tt.expectedMessage.User.Name, user.Name)
assertStringsEqual(t, tt.expectedMessage.User.DisplayName, user.DisplayName)
assertStringsEqual(t, tt.expectedMessage.User.Color, user.Color)
assertStringIntMapsEqual(t, tt.expectedMessage.User.Badges, user.Badges)

expectedBadges := map[string]int{
"premium": 1,
}
assertStringIntMapsEqual(t, expectedBadges, user.Badges)
if privateMessage.Type != tt.expectedMessage.Type {
t.Error("parsing MessageType failed")
}

if privateMessage.Type != PRIVMSG {
t.Error("parsing MessageType failed")
assertStringsEqual(t, tt.expectedMessage.RawType, privateMessage.RawType)
assertStringsEqual(t, tt.expectedMessage.Message, privateMessage.Message)
assertStringsEqual(t, tt.expectedMessage.Channel, privateMessage.Channel)
assertStringsEqual(t, tt.expectedMessage.RoomID, privateMessage.RoomID)
assertStringsEqual(t, tt.expectedMessage.ID, privateMessage.ID)
assertBoolEqual(t, tt.expectedMessage.Action, privateMessage.Action)

assertIntsEqual(t, len(tt.expectedMessage.Emotes), len(privateMessage.Emotes))
assertIntsEqual(t, tt.expectedMessage.Bits, privateMessage.Bits)
assertBoolEqual(t, tt.expectedMessage.FirstMessage, privateMessage.FirstMessage)
})
}(tt)
}
assertStringsEqual(t, "PRIVMSG", privateMessage.RawType)
assertStringsEqual(t, "Chew your food slower... it's healthier", privateMessage.Message)
assertStringsEqual(t, "clippyassistant", privateMessage.Channel)
assertStringsEqual(t, "408892348", privateMessage.RoomID)
assertStringsEqual(t, "6efffc70-27a1-4637-9111-44e5104bb7da", privateMessage.ID)
assertFalse(t, privateMessage.Action, "parsing Action failed")
assertIntsEqual(t, 0, len(privateMessage.Emotes))
assertIntsEqual(t, 0, privateMessage.Bits)
}

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

0 comments on commit 61d050b

Please sign in to comment.