slack: update library

This commit is contained in:
Chris Sexton 2021-08-05 10:47:34 -04:00 committed by Chris Sexton
parent 7464a7c84c
commit 99abd9bb2f
2 changed files with 53 additions and 2 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/velour/catbase/connectors/discord" "github.com/velour/catbase/connectors/discord"
"github.com/velour/catbase/plugins/giphy" "github.com/velour/catbase/plugins/giphy"
"github.com/velour/catbase/plugins/last" "github.com/velour/catbase/plugins/last"
"github.com/velour/catbase/plugins/quotegame"
"github.com/velour/catbase/plugins/rest" "github.com/velour/catbase/plugins/rest"
"github.com/velour/catbase/plugins/secrets" "github.com/velour/catbase/plugins/secrets"
@ -163,6 +164,7 @@ func main() {
b.AddPlugin(sms.New(b)) b.AddPlugin(sms.New(b))
b.AddPlugin(countdown.New(b)) b.AddPlugin(countdown.New(b))
b.AddPlugin(rest.New(b)) b.AddPlugin(rest.New(b))
b.AddPlugin(quotegame.New(b))
// catches anything left, will always return true // catches anything left, will always return true
b.AddPlugin(fact.New(b)) b.AddPlugin(fact.New(b))

View File

@ -2,9 +2,11 @@ package quotegame
import ( import (
"math/rand" "math/rand"
"regexp"
"time" "time"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/bot" "github.com/velour/catbase/bot"
"github.com/velour/catbase/config" "github.com/velour/catbase/config"
) )
@ -14,16 +16,38 @@ type QuoteGame struct {
c *config.Config c *config.Config
db *sqlx.DB db *sqlx.DB
handlers []bot.HandlerSpec
currentGame *time.Timer currentGame *time.Timer
} }
func New(b bot.Bot) *QuoteGame { func New(b bot.Bot) *QuoteGame {
return &QuoteGame{ p := &QuoteGame{
b: b, b: b,
c: b.Config(), c: b.Config(),
db: b.DB(), db: b.DB(),
currentGame: nil, currentGame: nil,
} }
p.register()
return p
}
func (p *QuoteGame) register() {
log.Debug().Msg("registering quote handlers")
p.handlers = []bot.HandlerSpec{
{
Kind: bot.Message, IsCmd: true,
Regex: regexp.MustCompile(`(?i)^quote game$`),
HelpText: "Start a quote game",
Handler: p.startGame,
},
{
Kind: bot.Message, IsCmd: false,
Regex: regexp.MustCompile(`.*`),
Handler: p.message,
},
}
p.b.RegisterTable(p, p.handlers)
} }
func (p *QuoteGame) getAllQuotes() ([]string, error) { func (p *QuoteGame) getAllQuotes() ([]string, error) {
@ -48,3 +72,28 @@ func (p *QuoteGame) getRandomquote() (string, error) {
} }
return quotes[rand.Intn(len(quotes))], nil return quotes[rand.Intn(len(quotes))], nil
} }
func (p *QuoteGame) startGame(r bot.Request) bool {
log.Debug().Msg("startGame called")
if p.currentGame != nil {
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "There is already a quote game running.")
return true
}
length := time.Duration(p.c.GetInt("quotegame.length", 120))
p.currentGame = time.AfterFunc(length * time.Second, func() {
p.currentGame = nil
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Game ended.")
})
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Game started.")
return true
}
func (p *QuoteGame) message(r bot.Request) bool {
if p.currentGame == nil {
return false
}
return false
}