newsbid: refactor

This commit is contained in:
Chris Sexton 2021-01-31 18:08:25 -05:00 committed by Chris Sexton
parent aad4ecf143
commit 0e5f7eb2d5
4 changed files with 118 additions and 79 deletions

View File

@ -245,6 +245,17 @@ func (b *bot) RegisterRegex(p Plugin, kind Kind, r *regexp.Regexp, resp Response
b.callbacks[t][kind][r] = append(b.callbacks[t][kind][r], resp) b.callbacks[t][kind][r] = append(b.callbacks[t][kind][r], resp)
} }
// RegisterRegexCmd is a shortcut to filter non-command messages from a registration
func (b *bot) RegisterRegexCmd(p Plugin, kind Kind, r *regexp.Regexp, resp ResponseHandler) {
newResp := func(req Request) bool {
if !req.Msg.Command {
return false
}
return resp(req)
}
b.RegisterRegex(p, kind, r, newResp)
}
// Register a callback // Register a callback
// This function should be considered deprecated. // This function should be considered deprecated.
func (b *bot) Register(p Plugin, kind Kind, cb Callback) { func (b *bot) Register(p Plugin, kind Kind, cb Callback) {

View File

@ -94,6 +94,10 @@ type Bot interface {
// Kind will be matched to the event for the callback // Kind will be matched to the event for the callback
RegisterRegex(Plugin, Kind, *regexp.Regexp, ResponseHandler) RegisterRegex(Plugin, Kind, *regexp.Regexp, ResponseHandler)
// Register a plugin callback filtering non-commands out
// Kind will be matched to the event for the callback
RegisterRegexCmd(Plugin, Kind, *regexp.Regexp, ResponseHandler)
// Register a plugin callback // Register a plugin callback
// Kind will be matched to the event for the callback // Kind will be matched to the event for the callback
Register(Plugin, Kind, Callback) Register(Plugin, Kind, Callback)

View File

@ -55,6 +55,7 @@ func (mb *MockBot) Send(c Connector, kind Kind, args ...interface{}) (string, er
func (mb *MockBot) AddPlugin(f Plugin) {} func (mb *MockBot) AddPlugin(f Plugin) {}
func (mb *MockBot) Register(p Plugin, kind Kind, cb Callback) {} func (mb *MockBot) Register(p Plugin, kind Kind, cb Callback) {}
func (mb *MockBot) RegisterRegex(p Plugin, kind Kind, r *regexp.Regexp, h ResponseHandler) {} func (mb *MockBot) RegisterRegex(p Plugin, kind Kind, r *regexp.Regexp, h ResponseHandler) {}
func (mb *MockBot) RegisterRegexCmd(p Plugin, kind Kind, r *regexp.Regexp, h ResponseHandler) {}
func (mb *MockBot) RegisterWeb(_, _ string) {} func (mb *MockBot) RegisterWeb(_, _ string) {}
func (mb *MockBot) GetWebNavigation() []EndPoint { return nil } func (mb *MockBot) GetWebNavigation() []EndPoint { return nil }
func (mb *MockBot) Receive(c Connector, kind Kind, msg msg.Message, args ...interface{}) bool { func (mb *MockBot) Receive(c Connector, kind Kind, msg msg.Message, args ...interface{}) bool {

View File

@ -2,13 +2,14 @@ package newsbid
import ( import (
"fmt" "fmt"
"regexp"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"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/bot/msg"
"github.com/velour/catbase/plugins/newsbid/webshit" "github.com/velour/catbase/plugins/newsbid/webshit"
) )
@ -30,27 +31,42 @@ func New(b bot.Bot) *NewsBid {
db: b.DB(), db: b.DB(),
ws: ws, ws: ws,
} }
p.bot.Register(p, bot.Message, p.message)
p.bot.RegisterRegexCmd(p, bot.Message, balanceRegex, p.balanceCmd)
p.bot.RegisterRegexCmd(p, bot.Message, bidsRegex, p.bidsCmd)
p.bot.RegisterRegexCmd(p, bot.Message, scoresRegex, p.scoresCmd)
p.bot.RegisterRegexCmd(p, bot.Message, bidRegex, p.bidCmd)
p.bot.RegisterRegexCmd(p, bot.Message, checkRegex, p.checkCmd)
return p return p
} }
func (p *NewsBid) message(conn bot.Connector, k bot.Kind, message msg.Message, args ...interface{}) bool { var balanceRegex = regexp.MustCompile(`(?i)^balance$`)
body := strings.ToLower(message.Body) var bidsRegex = regexp.MustCompile(`(?i)^bids$`)
ch := message.Channel var scoresRegex = regexp.MustCompile(`(?i)^scores$`)
if message.Command && body == "balance" { var bidRegex = regexp.MustCompile(`(?i)^bid (?P<amount>\S+) (?P<url>)\S+$`)
bal := p.ws.GetBalance(message.User.Name) var checkRegex = regexp.MustCompile(`(?i)^check ngate$`)
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("%s, your current balance is %d.",
message.User.Name, bal)) func (p *NewsBid) balanceCmd(r bot.Request) bool {
if !r.Msg.Command {
return false
}
bal := p.ws.GetBalance(r.Msg.User.Name)
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s, your current balance is %d.",
r.Msg.User.Name, bal))
return true return true
} }
if message.Command && body == "bids" {
func (p *NewsBid) bidsCmd(r bot.Request) bool {
ch := r.Msg.Channel
bids, err := p.ws.GetAllBids() bids, err := p.ws.GetAllBids()
if err != nil { if err != nil {
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error getting bids: %s", err)) p.bot.Send(r.Conn, bot.Message, ch, fmt.Sprintf("Error getting bids: %s", err))
return true return true
} }
if len(bids) == 0 { if len(bids) == 0 {
p.bot.Send(conn, bot.Message, ch, "No bids to report.") p.bot.Send(r.Conn, bot.Message, ch, "No bids to report.")
return true return true
} }
sort.Slice(bids, func(i, j int) bool { sort.Slice(bids, func(i, j int) bool {
@ -64,17 +80,20 @@ func (p *NewsBid) message(conn bot.Connector, k bot.Kind, message msg.Message, a
hnURL := fmt.Sprintf("https://news.ycombinator.com/item?id=%d", b.HNID) hnURL := fmt.Sprintf("https://news.ycombinator.com/item?id=%d", b.HNID)
out += fmt.Sprintf("• %s bid %s <%s|%s> (<%s|Comments>)\n", b.User, b.BidStr, b.URL, b.Title, hnURL) out += fmt.Sprintf("• %s bid %s <%s|%s> (<%s|Comments>)\n", b.User, b.BidStr, b.URL, b.Title, hnURL)
} }
p.bot.Send(conn, bot.Message, ch, out) p.bot.Send(r.Conn, bot.Message, ch, out)
return true return true
} }
if message.Command && body == "scores" {
func (p *NewsBid) scoresCmd(r bot.Request) bool {
ch := r.Msg.Channel
bals, err := p.ws.GetAllBalances() bals, err := p.ws.GetAllBalances()
if err != nil { if err != nil {
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error getting bids: %s", err)) p.bot.Send(r.Conn, bot.Message, ch, fmt.Sprintf("Error getting bids: %s", err))
return true return true
} }
if len(bals) == 0 { if len(bals) == 0 {
p.bot.Send(conn, bot.Message, ch, "No balances to report.") p.bot.Send(r.Conn, bot.Message, ch, "No balances to report.")
return true return true
} }
out := "NGate balances:\n" out := "NGate balances:\n"
@ -82,38 +101,41 @@ func (p *NewsBid) message(conn bot.Connector, k bot.Kind, message msg.Message, a
for _, b := range bals { for _, b := range bals {
out += fmt.Sprintf("%s has a total score of %d with %d left to bid this session\n", b.User, b.Score, b.Balance) out += fmt.Sprintf("%s has a total score of %d with %d left to bid this session\n", b.User, b.Score, b.Balance)
} }
p.bot.Send(conn, bot.Message, ch, out) p.bot.Send(r.Conn, bot.Message, ch, out)
return true return true
} }
if message.Command && strings.HasPrefix(body, "bid") {
func (p *NewsBid) bidCmd(r bot.Request) bool {
body := strings.ToLower(r.Msg.Body)
ch := r.Msg.Channel
conn := r.Conn
log.Debug().Interface("request", r).Msg("bid request")
parts := strings.Fields(body) parts := strings.Fields(body)
if len(parts) != 3 { if len(parts) != 3 {
p.bot.Send(conn, bot.Message, ch, "You must bid with an amount and a URL.") p.bot.Send(conn, bot.Message, ch, "You must bid with an amount and a URL.")
return true return true
} }
amount, _ := strconv.Atoi(parts[1]) amount, _ := strconv.Atoi(parts[1])
url := parts[2] url := parts[2]
if bid, err := p.ws.Bid(message.User.Name, amount, parts[1], url); err != nil { if bid, err := p.ws.Bid(r.Msg.User.Name, amount, parts[1], url); err != nil {
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error placing bid: %s", err)) p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error placing bid: %s", err))
} else { } else {
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Your bid has been placed on %s", bid.Title)) p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Your bid has been placed on %s", bid.Title))
} }
return true return true
} }
if message.Command && body == "check ngate" {
p.check(conn, ch)
return true
}
return false
}
func (p *NewsBid) check(conn bot.Connector, ch string) { func (p *NewsBid) checkCmd(r bot.Request) bool {
ch := r.Msg.Channel
conn := r.Conn
last := p.bot.Config().GetInt64("newsbid.lastprocessed", 0) last := p.bot.Config().GetInt64("newsbid.lastprocessed", 0)
wr, pubTime, err := p.ws.Check(last) wr, pubTime, err := p.ws.Check(last)
if err != nil { if err != nil {
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error checking ngate: %s", err)) p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error checking ngate: %s", err))
return return true
} }
p.bot.Config().Set("newsbid.lastprocessed", strconv.FormatInt(pubTime, 10)) p.bot.Config().Set("newsbid.lastprocessed", strconv.FormatInt(pubTime, 10))
@ -147,4 +169,5 @@ func (p *NewsBid) check(conn bot.Connector, ch string) {
} }
p.bot.Send(conn, bot.Message, ch, msg) p.bot.Send(conn, bot.Message, ch, msg)
} }
return true
} }