galched-bot/modules/twitchat/twitchat.go
alexvanin f0f31a8415 Add twitch point song requests to the bot
This commits adds new feature for galchedbot: video requests
in the twitch chat via highlighted chat messages. This messages
parsed by the bot and added to the video queue, that can be
accessed by the dedicated web server. Video queue requires
authorization based on random token added to the cookies.
2020-01-11 22:38:51 +03:00

55 lines
1.1 KiB
Go

package twitchat
import (
"galched-bot/modules/settings"
"galched-bot/modules/youtube"
"github.com/gempir/go-twitch-irc/v2"
)
type (
TwitchIRC struct {
username string
chat *twitch.Client
handlers []PrivateMessageHandler
}
)
func New(s *settings.Settings, r *youtube.Requester) (*TwitchIRC, error) {
var irc = new(TwitchIRC)
irc.username = s.TwitchUser
irc.handlers = append(irc.handlers, DupHandler())
irc.handlers = append(irc.handlers, SongRequest(r))
// irc.handlers = append(irc.handlers, LogCheck())
irc.chat = twitch.NewClient(s.TwitchUser, s.TwitchToken)
irc.chat.OnPrivateMessage(irc.PrivateMessageHandler)
irc.chat.Join(s.TwitchIRCRoom)
return irc, nil
}
func (c *TwitchIRC) Start() error {
go func() {
err := c.chat.Connect()
_ = err // no point in error because disconnect will be called anyway
}()
return nil
}
func (c *TwitchIRC) Stop() error {
return c.chat.Disconnect()
}
func (c *TwitchIRC) PrivateMessageHandler(msg twitch.PrivateMessage) {
if msg.User.Name == c.username {
return
}
for i := range c.handlers {
if c.handlers[i].IsValid(&msg) {
c.handlers[i].Handle(&msg, c.chat)
}
}
}