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.
This commit is contained in:
Alex Vanin 2020-01-11 22:38:51 +03:00
parent 65fc1ccad4
commit f0f31a8415
18 changed files with 813 additions and 13 deletions

View file

@ -26,7 +26,7 @@ func DupHandler() PrivateMessageHandler {
}
}
func (h *dupHandler) IsValid(m string) bool {
func (h *dupHandler) IsValid(m *twitch.PrivateMessage) bool {
return true
}

View file

@ -10,7 +10,7 @@ type (
}
PrivateMessageHandler interface {
IsValid(string) bool
IsValid(m *twitch.PrivateMessage) bool
Handle(m *twitch.PrivateMessage, r Responser)
}
)

View file

@ -0,0 +1,23 @@
package twitchat
import (
"log"
"github.com/gempir/go-twitch-irc/v2"
)
type (
logCheck struct{}
)
func LogCheck() PrivateMessageHandler {
return new(logCheck)
}
func (h *logCheck) IsValid(m *twitch.PrivateMessage) bool {
return true
}
func (h *logCheck) Handle(m *twitch.PrivateMessage, r Responser) {
log.Print("chat <", m.User.DisplayName, "> : ", m.Message)
}

View file

@ -0,0 +1,61 @@
package twitchat
import (
"fmt"
"log"
"strings"
"github.com/gempir/go-twitch-irc/v2"
"galched-bot/modules/youtube"
)
const (
songMsg = "!song"
reqPrefix = "!req " // space in the end is important
)
type (
songRequest struct {
r *youtube.Requester
}
)
func SongRequest(r *youtube.Requester) PrivateMessageHandler {
return &songRequest{r: r}
}
func (h *songRequest) IsValid(m *twitch.PrivateMessage) bool {
return (strings.HasPrefix(m.Message, reqPrefix) && m.Tags["msg-id"] == "highlighted-message") ||
strings.TrimSpace(m.Message) == songMsg
// return strings.HasPrefix(m.Message, reqPrefix) || strings.TrimSpace(m.Message) == songMsg
}
func (h *songRequest) Handle(m *twitch.PrivateMessage, r Responser) {
if strings.TrimSpace(m.Message) == "!song" {
list := h.r.List()
if len(list) > 0 {
line := fmt.Sprintf("Сейчас играет: <%s>", list[0].Title)
r.Say(m.Channel, line)
} else {
r.Say(m.Channel, "Очередь видео пуста")
}
return
}
query := strings.TrimPrefix(m.Message, "!req ")
if len(query) == 0 {
return
}
chatMsg, err := h.r.AddVideo(query, m.User.DisplayName)
if err != nil {
log.Printf("yt: cannot add song from msg <%s>, err: %v", m.Message, err)
if len(chatMsg) > 0 {
r.Say(m.Channel, m.User.DisplayName+" "+chatMsg)
}
return
}
r.Say(m.Channel, m.User.DisplayName+" добавил "+chatMsg)
return
}

View file

@ -2,6 +2,7 @@ package twitchat
import (
"galched-bot/modules/settings"
"galched-bot/modules/youtube"
"github.com/gempir/go-twitch-irc/v2"
)
@ -14,12 +15,14 @@ type (
}
)
func New(s *settings.Settings) (*TwitchIRC, error) {
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)
@ -45,7 +48,7 @@ func (c *TwitchIRC) PrivateMessageHandler(msg twitch.PrivateMessage) {
return
}
for i := range c.handlers {
if c.handlers[i].IsValid(msg.Message) {
if c.handlers[i].IsValid(&msg) {
c.handlers[i].Handle(&msg, c.chat)
}
}