Compare commits

..

2 Commits

Author SHA1 Message Date
Chris Sexton f28026436a twitch: separate into regex commands and add user check 2022-08-09 08:14:36 -04:00
Chris Sexton 6038dd7cf9 git: update ignore 2022-08-09 08:14:36 -04:00
3 changed files with 110 additions and 55 deletions

1
.gitignore vendored
View File

@ -76,3 +76,4 @@ run.sh
impact.ttf impact.ttf
.env .env
rathaus_discord.sh rathaus_discord.sh
emojy

View File

@ -7,6 +7,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"regexp"
"strings" "strings"
"text/template" "text/template"
"time" "time"
@ -26,9 +27,10 @@ const (
) )
type TwitchPlugin struct { type TwitchPlugin struct {
bot bot.Bot b bot.Bot
config *config.Config c *config.Config
twitchList map[string]*Twitcher twitchList map[string]*Twitcher
tbl bot.HandlerTable
} }
type Twitcher struct { type Twitcher struct {
@ -62,13 +64,14 @@ type stream struct {
func New(b bot.Bot) *TwitchPlugin { func New(b bot.Bot) *TwitchPlugin {
p := &TwitchPlugin{ p := &TwitchPlugin{
bot: b, b: b,
config: b.Config(), c: b.Config(),
twitchList: map[string]*Twitcher{}, twitchList: map[string]*Twitcher{},
} }
for _, ch := range p.config.GetArray("Twitch.Channels", []string{}) { for _, ch := range p.c.GetArray("Twitch.Channels", []string{}) {
for _, twitcherName := range p.config.GetArray("Twitch."+ch+".Users", []string{}) { for _, twitcherName := range p.c.GetArray("Twitch."+ch+".Users", []string{}) {
twitcherName = strings.ToLower(twitcherName)
if _, ok := p.twitchList[twitcherName]; !ok { if _, ok := p.twitchList[twitcherName]; !ok {
p.twitchList[twitcherName] = &Twitcher{ p.twitchList[twitcherName] = &Twitcher{
name: twitcherName, name: twitcherName,
@ -81,8 +84,7 @@ func New(b bot.Bot) *TwitchPlugin {
go p.twitchAuthLoop(b.DefaultConnector()) go p.twitchAuthLoop(b.DefaultConnector())
b.Register(p, bot.Message, p.message) p.register()
b.Register(p, bot.Help, p.help)
p.registerWeb() p.registerWeb()
return p return p
@ -91,11 +93,11 @@ func New(b bot.Bot) *TwitchPlugin {
func (p *TwitchPlugin) registerWeb() { func (p *TwitchPlugin) registerWeb() {
r := chi.NewRouter() r := chi.NewRouter()
r.HandleFunc("/{user}", p.serveStreaming) r.HandleFunc("/{user}", p.serveStreaming)
p.bot.RegisterWeb(r, "/isstreaming") p.b.RegisterWeb(r, "/isstreaming")
} }
func (p *TwitchPlugin) serveStreaming(w http.ResponseWriter, r *http.Request) { func (p *TwitchPlugin) serveStreaming(w http.ResponseWriter, r *http.Request) {
userName := chi.URLParam(r, "user") userName := strings.ToLower(chi.URLParam(r, "user"))
if userName == "" { if userName == "" {
fmt.Fprint(w, "User not found.") fmt.Fprint(w, "User not found.")
return return
@ -124,14 +126,39 @@ func (p *TwitchPlugin) serveStreaming(w http.ResponseWriter, r *http.Request) {
} }
} }
func (p *TwitchPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...any) bool { func (p *TwitchPlugin) register() {
body := strings.ToLower(message.Body) p.tbl = bot.HandlerTable{
if body == "twitch status" { {
channel := message.Channel Kind: bot.Message, IsCmd: true,
if users := p.config.GetArray("Twitch."+channel+".Users", []string{}); len(users) > 0 { Regex: regexp.MustCompile(`(?i)^twitch status$`),
HelpText: "Get status of all twitchers",
Handler: p.twitchStatus,
},
{
Kind: bot.Message, IsCmd: false,
Regex: regexp.MustCompile(`(?i)^is (?P<who>.+) streaming\??$`),
HelpText: "Check if a specific twitcher is streaming",
Handler: p.twitchUserStatus,
},
{
Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^reset twitch$`),
HelpText: "Reset the twitch templates",
Handler: p.resetTwitch,
},
}
p.b.Register(p, bot.Help, p.help)
p.b.RegisterTable(p, p.tbl)
}
func (p *TwitchPlugin) twitchStatus(r bot.Request) bool {
channel := r.Msg.Channel
if users := p.c.GetArray("Twitch."+channel+".Users", []string{}); len(users) > 0 {
for _, twitcherName := range users { for _, twitcherName := range users {
if _, ok := p.twitchList[twitcherName]; ok { twitcherName = strings.ToLower(twitcherName)
err := p.checkTwitch(c, channel, p.twitchList[twitcherName], true) // we could re-add them here instead of needing to restart the bot.
if t, ok := p.twitchList[twitcherName]; ok {
err := p.checkTwitch(r.Conn, channel, t, true)
if err != nil { if err != nil {
log.Error().Err(err).Msgf("error in checking twitch") log.Error().Err(err).Msgf("error in checking twitch")
} }
@ -139,13 +166,28 @@ func (p *TwitchPlugin) message(c bot.Connector, kind bot.Kind, message msg.Messa
} }
} }
return true return true
} else if body == "reset twitch" {
p.config.Set("twitch.istpl", isStreamingTplFallback)
p.config.Set("twitch.nottpl", notStreamingTplFallback)
p.config.Set("twitch.stoppedtpl", stoppedStreamingTplFallback)
} }
return false func (p *TwitchPlugin) twitchUserStatus(r bot.Request) bool {
who := strings.ToLower(r.Values["who"])
if t, ok := p.twitchList[who]; ok {
err := p.checkTwitch(r.Conn, r.Msg.Channel, t, true)
if err != nil {
log.Error().Err(err).Msgf("error in checking twitch")
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "I had trouble with that.")
}
} else {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "I don't know who that is.")
}
return true
}
func (p *TwitchPlugin) resetTwitch(r bot.Request) bool {
p.c.Set("twitch.istpl", isStreamingTplFallback)
p.c.Set("twitch.nottpl", notStreamingTplFallback)
p.c.Set("twitch.stoppedtpl", stoppedStreamingTplFallback)
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "The Twitch templates have been reset.")
return true
} }
func (p *TwitchPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...any) bool { func (p *TwitchPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...any) bool {
@ -155,14 +197,14 @@ func (p *TwitchPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message,
msg += fmt.Sprintf("twitch.stoppedtpl (default: %s)\n", stoppedStreamingTplFallback) msg += fmt.Sprintf("twitch.stoppedtpl (default: %s)\n", stoppedStreamingTplFallback)
msg += "You can reset all messages with `!reset twitch`" msg += "You can reset all messages with `!reset twitch`"
msg += "And you can ask who is streaming with `!twitch status`" msg += "And you can ask who is streaming with `!twitch status`"
p.bot.Send(c, bot.Message, message.Channel, msg) p.b.Send(c, bot.Message, message.Channel, msg)
return true return true
} }
func (p *TwitchPlugin) twitchAuthLoop(c bot.Connector) { func (p *TwitchPlugin) twitchAuthLoop(c bot.Connector) {
frequency := p.config.GetInt("Twitch.AuthFreq", 60*60) frequency := p.c.GetInt("Twitch.AuthFreq", 60*60)
cid := p.config.Get("twitch.clientid", "") cid := p.c.Get("twitch.clientid", "")
secret := p.config.Get("twitch.secret", "") secret := p.c.Get("twitch.secret", "")
if cid == "" || secret == "" { if cid == "" || secret == "" {
log.Info().Msgf("Disabling twitch autoauth.") log.Info().Msgf("Disabling twitch autoauth.")
return return
@ -185,8 +227,8 @@ func (p *TwitchPlugin) twitchAuthLoop(c bot.Connector) {
} }
func (p *TwitchPlugin) twitchChannelLoop(c bot.Connector, channel string) { func (p *TwitchPlugin) twitchChannelLoop(c bot.Connector, channel string) {
frequency := p.config.GetInt("Twitch.Freq", 60) frequency := p.c.GetInt("Twitch.Freq", 60)
if p.config.Get("twitch.clientid", "") == "" || p.config.Get("twitch.secret", "") == "" { if p.c.Get("twitch.clientid", "") == "" || p.c.Get("twitch.secret", "") == "" {
log.Info().Msgf("Disabling twitch autochecking.") log.Info().Msgf("Disabling twitch autochecking.")
return return
} }
@ -196,7 +238,8 @@ func (p *TwitchPlugin) twitchChannelLoop(c bot.Connector, channel string) {
for { for {
time.Sleep(time.Duration(frequency) * time.Second) time.Sleep(time.Duration(frequency) * time.Second)
for _, twitcherName := range p.config.GetArray("Twitch."+channel+".Users", []string{}) { for _, twitcherName := range p.c.GetArray("Twitch."+channel+".Users", []string{}) {
twitcherName = strings.ToLower(twitcherName)
if err := p.checkTwitch(c, channel, p.twitchList[twitcherName], false); err != nil { if err := p.checkTwitch(c, channel, p.twitchList[twitcherName], false); err != nil {
log.Error().Err(err).Msgf("error in twitch loop") log.Error().Err(err).Msgf("error in twitch loop")
} }
@ -246,8 +289,8 @@ func (p *TwitchPlugin) checkTwitch(c bot.Connector, channel string, twitcher *Tw
baseURL.RawQuery = query.Encode() baseURL.RawQuery = query.Encode()
cid := p.config.Get("twitch.clientid", "") cid := p.c.Get("twitch.clientid", "")
token := p.config.Get("twitch.token", "") token := p.c.Get("twitch.token", "")
if cid == token && cid == "" { if cid == token && cid == "" {
log.Info().Msgf("Twitch plugin not enabled.") log.Info().Msgf("Twitch plugin not enabled.")
return nil return nil
@ -275,9 +318,9 @@ func (p *TwitchPlugin) checkTwitch(c bot.Connector, channel string, twitcher *Tw
title = games[0].Title title = games[0].Title
} }
notStreamingTpl := p.config.Get("Twitch.NotTpl", notStreamingTplFallback) notStreamingTpl := p.c.Get("Twitch.NotTpl", notStreamingTplFallback)
isStreamingTpl := p.config.Get("Twitch.IsTpl", isStreamingTplFallback) isStreamingTpl := p.c.Get("Twitch.IsTpl", isStreamingTplFallback)
stoppedStreamingTpl := p.config.Get("Twitch.StoppedTpl", stoppedStreamingTplFallback) stoppedStreamingTpl := p.c.Get("Twitch.StoppedTpl", stoppedStreamingTplFallback)
buf := bytes.Buffer{} buf := bytes.Buffer{}
info := struct { info := struct {
@ -295,31 +338,31 @@ func (p *TwitchPlugin) checkTwitch(c bot.Connector, channel string, twitcher *Tw
t, err := template.New("notStreaming").Parse(notStreamingTpl) t, err := template.New("notStreaming").Parse(notStreamingTpl)
if err != nil { if err != nil {
log.Error().Err(err) log.Error().Err(err)
p.bot.Send(c, bot.Message, channel, err) p.b.Send(c, bot.Message, channel, err)
t = template.Must(template.New("notStreaming").Parse(notStreamingTplFallback)) t = template.Must(template.New("notStreaming").Parse(notStreamingTplFallback))
} }
t.Execute(&buf, info) t.Execute(&buf, info)
p.bot.Send(c, bot.Message, channel, buf.String()) p.b.Send(c, bot.Message, channel, buf.String())
} else { } else {
t, err := template.New("isStreaming").Parse(isStreamingTpl) t, err := template.New("isStreaming").Parse(isStreamingTpl)
if err != nil { if err != nil {
log.Error().Err(err) log.Error().Err(err)
p.bot.Send(c, bot.Message, channel, err) p.b.Send(c, bot.Message, channel, err)
t = template.Must(template.New("isStreaming").Parse(isStreamingTplFallback)) t = template.Must(template.New("isStreaming").Parse(isStreamingTplFallback))
} }
t.Execute(&buf, info) t.Execute(&buf, info)
p.bot.Send(c, bot.Message, channel, buf.String()) p.b.Send(c, bot.Message, channel, buf.String())
} }
} else if gameID == "" { } else if gameID == "" {
if twitcher.gameID != "" { if twitcher.gameID != "" {
t, err := template.New("stoppedStreaming").Parse(stoppedStreamingTpl) t, err := template.New("stoppedStreaming").Parse(stoppedStreamingTpl)
if err != nil { if err != nil {
log.Error().Err(err) log.Error().Err(err)
p.bot.Send(c, bot.Message, channel, err) p.b.Send(c, bot.Message, channel, err)
t = template.Must(template.New("stoppedStreaming").Parse(stoppedStreamingTplFallback)) t = template.Must(template.New("stoppedStreaming").Parse(stoppedStreamingTplFallback))
} }
t.Execute(&buf, info) t.Execute(&buf, info)
p.bot.Send(c, bot.Message, channel, buf.String()) p.b.Send(c, bot.Message, channel, buf.String())
} }
twitcher.gameID = "" twitcher.gameID = ""
} else { } else {
@ -327,11 +370,11 @@ func (p *TwitchPlugin) checkTwitch(c bot.Connector, channel string, twitcher *Tw
t, err := template.New("isStreaming").Parse(isStreamingTpl) t, err := template.New("isStreaming").Parse(isStreamingTpl)
if err != nil { if err != nil {
log.Error().Err(err) log.Error().Err(err)
p.bot.Send(c, bot.Message, channel, err) p.b.Send(c, bot.Message, channel, err)
t = template.Must(template.New("isStreaming").Parse(isStreamingTplFallback)) t = template.Must(template.New("isStreaming").Parse(isStreamingTplFallback))
} }
t.Execute(&buf, info) t.Execute(&buf, info)
p.bot.Send(c, bot.Message, channel, buf.String()) p.b.Send(c, bot.Message, channel, buf.String())
} }
twitcher.gameID = gameID twitcher.gameID = gameID
} }
@ -339,8 +382,8 @@ func (p *TwitchPlugin) checkTwitch(c bot.Connector, channel string, twitcher *Tw
} }
func (p *TwitchPlugin) validateCredentials() error { func (p *TwitchPlugin) validateCredentials() error {
cid := p.config.Get("twitch.clientid", "") cid := p.c.Get("twitch.clientid", "")
token := p.config.Get("twitch.token", "") token := p.c.Get("twitch.token", "")
if token == "" { if token == "" {
return p.reAuthenticate() return p.reAuthenticate()
} }
@ -353,8 +396,8 @@ func (p *TwitchPlugin) validateCredentials() error {
} }
func (p *TwitchPlugin) reAuthenticate() error { func (p *TwitchPlugin) reAuthenticate() error {
cid := p.config.Get("twitch.clientid", "") cid := p.c.Get("twitch.clientid", "")
secret := p.config.Get("twitch.secret", "") secret := p.c.Get("twitch.secret", "")
if cid == "" || secret == "" { if cid == "" || secret == "" {
return fmt.Errorf("could not request a new token without config values set") return fmt.Errorf("could not request a new token without config values set")
} }
@ -377,5 +420,5 @@ func (p *TwitchPlugin) reAuthenticate() error {
}{} }{}
err = json.Unmarshal(body, &credentials) err = json.Unmarshal(body, &credentials)
log.Debug().Int("expires", credentials.ExpiresIn).Msgf("setting new twitch token") log.Debug().Int("expires", credentials.ExpiresIn).Msgf("setting new twitch token")
return p.config.RegisterSecret("twitch.token", credentials.AccessToken) return p.c.RegisterSecret("twitch.token", credentials.AccessToken)
} }

View File

@ -13,6 +13,17 @@ import (
"github.com/velour/catbase/bot/user" "github.com/velour/catbase/bot/user"
) )
func makeRequest(payload string) bot.Request {
c, k, m := makeMessage(payload)
return bot.Request{
Conn: c,
Kind: k,
Msg: m,
Values: nil,
Args: nil,
}
}
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) { func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
isCmd := strings.HasPrefix(payload, "!") isCmd := strings.HasPrefix(payload, "!")
if isCmd { if isCmd {
@ -31,8 +42,8 @@ func makeTwitchPlugin(t *testing.T) (*TwitchPlugin, *bot.MockBot) {
c := New(mb) c := New(mb)
mb.Config().Set("twitch.clientid", "fake") mb.Config().Set("twitch.clientid", "fake")
mb.Config().Set("twitch.authorization", "fake") mb.Config().Set("twitch.authorization", "fake")
c.config.SetArray("Twitch.Channels", []string{"test"}) c.c.SetArray("Twitch.Channels", []string{"test"})
c.config.SetArray("Twitch.test.Users", []string{"drseabass"}) c.c.SetArray("Twitch.test.Users", []string{"drseabass"})
assert.NotNil(t, c) assert.NotNil(t, c)
c.twitchList["drseabass"] = &Twitcher{ c.twitchList["drseabass"] = &Twitcher{
@ -45,6 +56,6 @@ func makeTwitchPlugin(t *testing.T) (*TwitchPlugin, *bot.MockBot) {
func TestTwitch(t *testing.T) { func TestTwitch(t *testing.T) {
b, mb := makeTwitchPlugin(t) b, mb := makeTwitchPlugin(t)
b.message(makeMessage("!twitch status")) b.twitchStatus(makeRequest("!twitch status"))
assert.NotEmpty(t, mb.Messages) assert.NotEmpty(t, mb.Messages)
} }