go-twitch-irc icon indicating copy to clipboard operation
go-twitch-irc copied to clipboard

Add support for mods and vips parsing

Open TroyKomodo opened this issue 3 years ago • 14 comments

fixes #153

TroyKomodo avatar Feb 15 '21 00:02 TroyKomodo

Coverage Status

Coverage decreased (-3.02%) to 95.732% when pulling b65a6c2a8a87521157a367eaffe4d9081f219991 on TroyDota:mods-vips into 2ffb3958f61a48f355e20b1c5b8eb6778e4a1742 on gempir:master.

coveralls avatar Feb 15 '21 00:02 coveralls

I tried to make it goroutine safe. likely crappy implementation of that.

TroyKomodo avatar Feb 15 '21 00:02 TroyKomodo

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.

TroyKomodo avatar Feb 15 '21 00:02 TroyKomodo

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.

TroyKomodo avatar Feb 15 '21 00:02 TroyKomodo

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.

RAnders00 avatar Feb 15 '21 08:02 RAnders00

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.

3ventic avatar Feb 16 '21 19:02 3ventic

https://play.golang.org/p/2LNyI4GbQuh

TroyKomodo avatar Feb 16 '21 20:02 TroyKomodo

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.

TroyKomodo avatar Feb 16 '21 20:02 TroyKomodo

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

gempir avatar Feb 20 '21 14:02 gempir

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.

gempir avatar Feb 25 '21 09:02 gempir

Sure

TroyKomodo avatar Feb 25 '21 11:02 TroyKomodo

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 []

gempir avatar Feb 25 '21 19:02 gempir

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.

TroyKomodo avatar Mar 06 '21 15:03 TroyKomodo

There is no rush for me. Take your time

gempir avatar Mar 06 '21 16:03 gempir

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.

gempir avatar Oct 16 '22 07:10 gempir