Skip to content

Commit

Permalink
Add User role fields
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascdev committed Sep 28, 2024
1 parent f36aaab commit b0ff0d1
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 14 deletions.
13 changes: 8 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ var (

// User data you receive from TMI
type User struct {
ID string
Name string
DisplayName string
Color string
Badges map[string]int
ID string
Name string
DisplayName string
Color string
Badges map[string]int
IsBroadcaster bool
IsMod bool
IsVip bool
}

// Message interface that all messages implement
Expand Down
25 changes: 20 additions & 5 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,27 @@ func parseMessageType(messageType string) MessageType {
}

func parseUser(message *ircMessage) User {
var (
isBroadcaster bool
isMod bool
isVip bool
)

isBroadcaster = message.Tags["user-id"] == message.Tags["room-id"]
_, isVip = message.Tags["vip"]
if value, tagFound := message.Tags["mod"]; tagFound {
isMod = value == "1"
}

user := User{
ID: message.Tags["user-id"],
Name: message.Source.Username,
DisplayName: message.Tags["display-name"],
Color: message.Tags["color"],
Badges: make(map[string]int),
ID: message.Tags["user-id"],
Name: message.Source.Username,
DisplayName: message.Tags["display-name"],
Color: message.Tags["color"],
Badges: make(map[string]int),
IsBroadcaster: isBroadcaster,
IsMod: isMod,
IsVip: isVip,
}

if rawBadges := message.Tags["badges"]; rawBadges != "" {
Expand Down
119 changes: 115 additions & 4 deletions message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestCanParsePRIVMSGMessage(t *testing.T) {
message string
expectedMessage PrivateMessage
}
var tests = []test{
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= :[email protected] PRIVMSG #clippyassistant :Chew your food slower... it's healthier",
Expand Down Expand Up @@ -292,6 +292,117 @@ func TestCanParsePRIVMSGMessage(t *testing.T) {
}
}

func TestCanParsePRIVMSGUserRoles(t *testing.T) {
type expectedRoles struct {
IsBroadcaster bool
IsModerator bool
IsVIP bool
}
type test struct {
name string
message string
expectedRoles expectedRoles
expectedMessage PrivateMessage
}
tests := []test{
{
"Message From Broadcaster",
"@badge-info=subscriber/23;badges=broadcaster/1,subscriber/0,sub-gifter/5;client-nonce=8754bd6c285fb6795d58dae6ec70796a;color=#8A2BE2;display-name=hash_table;emotes=;first-msg=0;flags=;id=05eb8c6f-6606-4a44-90f3-fa2d516f9c51;mod=0;returning-chatter=0;room-id=63073510;subscriber=1;tmi-sent-ts=1727526200010;turbo=0;user-id=63073510;user-type= :hash_table!hash_table@hash_table.tmi.twitch.tv PRIVMSG #hash_table :asd",
expectedRoles{
IsBroadcaster: true,
IsModerator: false,
IsVIP: false,
},
PrivateMessage{
User: User{
ID: "63073510",
Name: "hash_table",
DisplayName: "hash_table",
Color: "#8A2BE2",
IsBroadcaster: true,
IsMod: false,
IsVip: false,
},
Type: PRIVMSG,
RawType: "PRIVMSG",
Message: "asd",
Channel: "hash_table",
RoomID: "63073510",
ID: "05eb8c6f-6606-4a44-90f3-fa2d516f9c51",
FirstMessage: false,
},
},
{
"Message From Moderator",
"@badge-info=founder/1;badges=moderator/1,founder/0,game-developer/1;color=#8A2BE2;display-name=hash_table;emotes=;first-msg=0;flags=;id=9e9f5092-2372-47cb-a996-09e055e45480;mod=1;returning-chatter=0;room-id=488227870;subscriber=1;tmi-sent-ts=1727528720632;turbo=0;user-id=63073510;user-type=mod :hash_table!hash_table@hash_table.tmi.twitch.tv PRIVMSG #frizzeh :asd",
expectedRoles{
IsBroadcaster: false,
IsModerator: true,
IsVIP: false,
},
PrivateMessage{
User: User{
ID: "63073510",
Name: "hash_table",
DisplayName: "hash_table",
Color: "#8A2BE2",
IsBroadcaster: false,
IsMod: true,
IsVip: false,
},
Type: PRIVMSG,
RawType: "PRIVMSG",
Message: "asd",
Channel: "frizzeh",
RoomID: "488227870",
ID: "9e9f5092-2372-47cb-a996-09e055e45480",
FirstMessage: false,
},
},
{
"Message From VIP",
"@badge-info=founder/1;badges=vip/1,founder/0,game-developer/1;color=#8A2BE2;display-name=hash_table;emotes=;first-msg=0;flags=;id=cbbd0def-69e6-4cc1-a3bf-6268e737bd69;mod=0;returning-chatter=0;room-id=488227870;subscriber=1;tmi-sent-ts=1727529432379;turbo=0;user-id=63073510;user-type=;vip=1 :hash_table!hash_table@hash_table.tmi.twitch.tv PRIVMSG #frizzeh :asd",
expectedRoles{
IsBroadcaster: false,
IsModerator: false,
IsVIP: true,
},
PrivateMessage{
User: User{
ID: "63073510",
Name: "hash_table",
DisplayName: "hash_table",
Color: "#8A2BE2",
IsBroadcaster: false,
IsMod: false,
IsVip: true,
},
Type: PRIVMSG,
RawType: "PRIVMSG",
Message: "asd",
Channel: "frizzeh",
RoomID: "488227870",
ID: "cbbd0def-69e6-4cc1-a3bf-6268e737bd69",
FirstMessage: false,
},
},
}

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

assertBoolEqual(t, tt.expectedRoles.IsBroadcaster, user.IsBroadcaster)
assertBoolEqual(t, tt.expectedRoles.IsModerator, user.IsMod)
assertBoolEqual(t, tt.expectedRoles.IsVIP, user.IsVip)
})
}(tt)
}
}

func TestCanParsePRIVMSGActionMessage(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= :[email protected] PRIVMSG #clippyassistant :\u0001ACTION Thrashh5, FeelsWayTooAmazingMan kinda\u0001"

Expand Down Expand Up @@ -320,7 +431,7 @@ func TestCanHandleBadEmoteMessage(t *testing.T) {
message string
expectedEmotes []Emote
}
var tests = []test{
tests := []test{
{
"Broken Emote Message",
"@badge-info=subscriber/3;badges=subscriber/3;color=#0000FF;display-name=Linkoping;emotes=25:40-44;flags=17-26:S.6;id=744f9c58-b180-4f46-bd9e-b515b5ef75c1;mod=0;room-id=188442366;subscriber=1;tmi-sent-ts=1566335866017;turbo=0;user-id=91673457;user-type= :[email protected] PRIVMSG #queenqarro :Då kan du begära skadestånd och förtal Kappa",
Expand Down Expand Up @@ -723,7 +834,7 @@ func TestPRIVMSGEmotesParsedProperly(t *testing.T) {
name string
message string
}
var tests = []test{
tests := []test{
{
"Normal PRIVMSG",
"@badge-info=subscriber/52;badges=broadcaster/1,subscriber/48,partner/1;color=#CC44FF;display-name=pajlada;emotes=25:6-10/1902:16-20;flags=;id=2a3f9d35-5487-4239-80b3-6c9a5a1907a9;mod=0;room-id=11148817;subscriber=1;tmi-sent-ts=1587291978478;turbo=0;user-id=11148817;user-type= :[email protected] PRIVMSG #pajlada :-tags Kappa 123 Keepo",
Expand Down Expand Up @@ -758,7 +869,7 @@ func TestPRIVMSGMalformedEmotesDontCrash(t *testing.T) {
name string
message string
}
var tests = []test{
tests := []test{
{
"Broken #1",
"@badge-info=subscriber/52;badges=moderator/1,subscriber/48;color=#2E8B57;display-name=pajbot;emotes=80481_/3:7-14;flags=;id=1ec936d3-7853-4113-9984-664ac5c42694;mod=1;room-id=11148817;subscriber=1;tmi-sent-ts=1589640131796;turbo=0;user-id=82008718;user-type=mod :[email protected] PRIVMSG #pajlada :󠀀-tags pajaW_/3.0",
Expand Down

0 comments on commit b0ff0d1

Please sign in to comment.