go-twitch-irc
go-twitch-irc copied to clipboard
Add support for mods and vips parsing
fixes #153
Coverage decreased (-3.02%) to 95.732% when pulling b65a6c2a8a87521157a367eaffe4d9081f219991 on TroyDota:mods-vips into 2ffb3958f61a48f355e20b1c5b8eb6778e4a1742 on gempir:master.
I tried to make it goroutine safe. likely crappy implementation of that.
I don't really like it much to be completely fair. I feel like there is probably a better way to make it goroutine safe. It just looks messy.
Feb 15 01:48:53.965 [INFO] (twitch.NoticeMessage) {
Raw: (string) (len=135) "@msg-id=vips_success :tmi.twitch.tv NOTICE #troydota :The VIPs of this channel are: admiralbulldog, aetaric, ales_, eeddya, komodotroy.",
Type: (twitch.MessageType) 6,
RawType: (string) (len=6) "NOTICE",
Tags: (map[string]string) (len=1) {
(string) (len=6) "msg-id": (string) (len=12) "vips_success"
},
Message: (string) (len=81) "The VIPs of this channel are: admiralbulldog, aetaric, ales_, eeddya, komodotroy.",
Channel: (string) (len=8) "troydota",
MsgID: (string) (len=12) "vips_success"
}
Feb 15 01:48:53.965 [INFO] [admiralbulldog aetaric ales_ eeddya komodotroy]
Feb 15 01:48:53.965 [INFO] [admiralbulldog aetaric ales_ eeddya komodotroy]
Feb 15 01:48:53.965 [INFO] [admiralbulldog aetaric ales_ eeddya komodotroy]
Feb 15 01:48:53.965 [INFO] [admiralbulldog aetaric ales_ eeddya komodotroy]
Feb 15 01:48:53.965 [INFO] [admiralbulldog aetaric ales_ eeddya komodotroy]
Feb 15 01:48:53.965 [INFO] [admiralbulldog aetaric ales_ eeddya komodotroy]
Feb 15 01:48:53.965 [INFO] [admiralbulldog aetaric ales_ eeddya komodotroy]
Feb 15 01:48:53.965 [INFO] [admiralbulldog aetaric ales_ eeddya komodotroy]
Feb 15 01:48:53.965 [INFO] [admiralbulldog aetaric ales_ eeddya komodotroy]
Feb 15 01:48:53.965 [INFO] [admiralbulldog aetaric ales_ eeddya komodotroy]
client.OnNoticeMessage(func(message twitch.NoticeMessage) {
log.Infoln(spew.Sdump(message))
if message.MsgID == "vips_success" {
client.vipsMtx.RLock()
if v, ok := client.vipsChan[message.Channel]; ok {
client.vipsMtx.RUnlock()
v.mutex.Lock()
v.finished = true
for _, ch := range v.chans {
ch <- message
}
v.mutex.Unlock()
} else {
client.vipsMtx.RUnlock()
}
} else if message.MsgID == "room_mods" {
client.modsMtx.RLock()
if v, ok := client.modsChan[message.Channel]; ok {
client.modsMtx.RUnlock()
v.mutex.Lock()
v.finished = true
for _, ch := range v.chans {
ch <- message
}
v.mutex.Unlock()
} else {
client.modsMtx.RUnlock()
}
}
})
client.OnConnect(func() {
for v := 0; v < 10; v++ {
go func() {
log.Println(client.GetVips("troydota"))
}()
}
})
so it seems that the concurrency works perfectly. Since we only send the /vips
command once but all goroutines get a response.
As far as I can see this is missing a timeout currently- You definitely want that I think. Error cases are also for example a connection disconnect, which could be handled too.
The message format isn't guaranteed to be static for either of the responses. I would recommend grabbing the string between :
, and end of string or .
, whichever comes first. This should also allow combining a lot of the common logic between the two even without generics.
https://play.golang.org/p/2LNyI4GbQuh
Also, I know I should write tests for this, but to be completely fair. I am not entirely sure how to write an effective test. That will test, both parsing and race conditions.
I updated the parsing tests. Would like to see some more with like 1 mod/vip. Also maybe some like just a random string and testing what happens.
I also for sure want the other client code tested not only the parsing, not every possible case but some basic tests for sure.
Also make sure to always compile with -race to better see any race conditions you build. https://golang.org/doc/articles/race_detector
Great start for a test, if possible please try also writing fake values from the server so the client starts reading and parsing them.
Also the linter has some issues with your test, maybe split it up a bit or something.
Sure
Also I tried it out and I'm just getting timeouts?
client.OnPrivateMessage(func(message twitch.PrivateMessage) {
fmt.Println(message.Message)
mods, err1 := client.GetMods("gempir", time.Second*5)
if err1 != nil {
log.Println(err1)
}
log.Println(mods)
vips, err2 := client.GetVips("gempir", time.Second*5)
if err2 != nil {
fmt.Println(err2)
}
log.Println(vips)
})
client.Join("gempir")
err := client.Connect()
if err != nil {
panic(err)
}
2021/02/25 20:50:44 request timedout
2021/02/25 20:50:44 []
request timedout
2021/02/25 20:50:49 []
Strange. Sorry ive been a bit busy with another project and do intend to fix and finish this pr. I should finish it this week.
There is no rush for me. Take your time
As per https://discuss.dev.twitch.tv/t/deprecation-of-chat-commands-through-irc/40486
These commands are deprecated. It wouldn't make sense adding them.