2019-06-23 13:12:35 +00:00
|
|
|
package twitchat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"galched-bot/modules/settings"
|
2020-01-11 19:38:51 +00:00
|
|
|
"galched-bot/modules/youtube"
|
2019-06-23 13:12:35 +00:00
|
|
|
|
2019-07-20 19:14:07 +00:00
|
|
|
"github.com/gempir/go-twitch-irc/v2"
|
2019-06-23 13:12:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
TwitchIRC struct {
|
|
|
|
username string
|
|
|
|
chat *twitch.Client
|
2019-07-20 19:14:07 +00:00
|
|
|
handlers []PrivateMessageHandler
|
2019-06-23 13:12:35 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-01-11 19:38:51 +00:00
|
|
|
func New(s *settings.Settings, r *youtube.Requester) (*TwitchIRC, error) {
|
2019-06-23 13:12:35 +00:00
|
|
|
var irc = new(TwitchIRC)
|
|
|
|
|
|
|
|
irc.username = s.TwitchUser
|
|
|
|
|
|
|
|
irc.handlers = append(irc.handlers, DupHandler())
|
2020-01-11 19:38:51 +00:00
|
|
|
irc.handlers = append(irc.handlers, SongRequest(r))
|
|
|
|
// irc.handlers = append(irc.handlers, LogCheck())
|
2019-06-23 13:12:35 +00:00
|
|
|
|
|
|
|
irc.chat = twitch.NewClient(s.TwitchUser, s.TwitchToken)
|
2019-07-20 19:14:07 +00:00
|
|
|
irc.chat.OnPrivateMessage(irc.PrivateMessageHandler)
|
2019-06-23 13:12:35 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-07-20 19:14:07 +00:00
|
|
|
func (c *TwitchIRC) PrivateMessageHandler(msg twitch.PrivateMessage) {
|
|
|
|
if msg.User.Name == c.username {
|
2019-06-23 13:12:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for i := range c.handlers {
|
2020-01-11 19:38:51 +00:00
|
|
|
if c.handlers[i].IsValid(&msg) {
|
2019-07-20 19:14:07 +00:00
|
|
|
c.handlers[i].Handle(&msg, c.chat)
|
2019-06-23 13:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|