counter: maybe count some stuff better via API

This commit is contained in:
Chris Sexton 2023-09-29 10:45:00 -04:00
parent bfd50a346d
commit 5de82d96e4
1 changed files with 9 additions and 4 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/velour/catbase/bot/user" "github.com/velour/catbase/bot/user"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"strconv" "strconv"
"time" "time"
@ -39,8 +40,8 @@ func (p *CounterPlugin) registerWeb() {
func (p *CounterPlugin) mkIncrementByNAPI(direction int) func(w http.ResponseWriter, r *http.Request) { func (p *CounterPlugin) mkIncrementByNAPI(direction int) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
userName := chi.URLParam(r, "user") userName, _ := url.QueryUnescape(chi.URLParam(r, "user"))
itemName := chi.URLParam(r, "item") itemName, _ := url.QueryUnescape(chi.URLParam(r, "item"))
delta, _ := strconv.Atoi(chi.URLParam(r, "delta")) delta, _ := strconv.Atoi(chi.URLParam(r, "delta"))
secret, pass, ok := r.BasicAuth() secret, pass, ok := r.BasicAuth()
@ -85,7 +86,11 @@ func (p *CounterPlugin) mkIncrementByNAPI(direction int) func(w http.ResponseWri
personalMsg = fmt.Sprintf("\nMessage: %s", inputMsg) personalMsg = fmt.Sprintf("\nMessage: %s", inputMsg)
} }
chs := p.cfg.GetArray("channels", []string{p.cfg.Get("channels", "none")}) //chs := p.cfg.GetArray("channels", []string{p.cfg.Get("channels", "none")})
chs := p.cfg.GetMap("counter.channelItems", map[string]string{})
if len(chs) == 0 {
return
}
req := &bot.Request{ req := &bot.Request{
Conn: p.b.DefaultConnector(), Conn: p.b.DefaultConnector(),
Kind: bot.Message, Kind: bot.Message,
@ -93,7 +98,7 @@ func (p *CounterPlugin) mkIncrementByNAPI(direction int) func(w http.ResponseWri
User: &u, User: &u,
// Noting here that we're only going to do goals in a "default" // Noting here that we're only going to do goals in a "default"
// channel even if it should send updates to others. // channel even if it should send updates to others.
Channel: chs[0], Channel: chs[itemName],
Body: fmt.Sprintf("%s += %d", itemName, delta), Body: fmt.Sprintf("%s += %d", itemName, delta),
Time: time.Now(), Time: time.Now(),
}, },