galched-bot/modules/twitchat/songrequest.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

61 lines
1.4 KiB
Go

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
}