counter: trim some spaces near aliases

This commit is contained in:
Chris Sexton 2022-07-18 21:44:03 -04:00
parent 2e0be87dd9
commit b2ec081d51
1 changed files with 23 additions and 8 deletions

View File

@ -3,13 +3,13 @@ package counter
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/config"
"math/rand" "math/rand"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/config"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
@ -23,6 +23,7 @@ type CounterPlugin struct {
b bot.Bot b bot.Bot
db *sqlx.DB db *sqlx.DB
cfg *config.Config cfg *config.Config
modify sync.Mutex
} }
type Item struct { type Item struct {
@ -122,8 +123,8 @@ func RmAlias(db *sqlx.DB, aliasName string) error {
} }
func MkAlias(db *sqlx.DB, aliasName, pointsTo string) (*alias, error) { func MkAlias(db *sqlx.DB, aliasName, pointsTo string) (*alias, error) {
aliasName = strings.ToLower(aliasName) aliasName = strings.TrimSpace(strings.ToLower(aliasName))
pointsTo = strings.ToLower(pointsTo) pointsTo = strings.TrimSpace(strings.ToLower(pointsTo))
res, err := db.Exec(`insert into counter_alias (item, points_to) values (?, ?)`, res, err := db.Exec(`insert into counter_alias (item, points_to) values (?, ?)`,
aliasName, pointsTo) aliasName, pointsTo)
if err != nil { if err != nil {
@ -436,6 +437,8 @@ func (p *CounterPlugin) leaderboardCmd(r bot.Request) bool {
} }
func (p *CounterPlugin) resetCmd(r bot.Request) bool { func (p *CounterPlugin) resetCmd(r bot.Request) bool {
p.modify.Lock()
defer p.modify.Unlock()
nick, id := p.resolveUser(r, "") nick, id := p.resolveUser(r, "")
channel := r.Msg.Channel channel := r.Msg.Channel
@ -509,6 +512,8 @@ func (p *CounterPlugin) inspectCmd(r bot.Request) bool {
} }
func (p *CounterPlugin) clearCmd(r bot.Request) bool { func (p *CounterPlugin) clearCmd(r bot.Request) bool {
p.modify.Lock()
defer p.modify.Unlock()
nick, id := p.resolveUser(r, "") nick, id := p.resolveUser(r, "")
itemName := strings.ToLower(r.Values["what"]) itemName := strings.ToLower(r.Values["what"])
channel := r.Msg.Channel channel := r.Msg.Channel
@ -575,6 +580,8 @@ func (p *CounterPlugin) countCmd(r bot.Request) bool {
} }
func (p *CounterPlugin) incrementCmd(r bot.Request) bool { func (p *CounterPlugin) incrementCmd(r bot.Request) bool {
p.modify.Lock()
defer p.modify.Unlock()
nick, id := r.Msg.User.Name, r.Msg.User.ID nick, id := r.Msg.User.Name, r.Msg.User.ID
if r.Values["who"] != "" { if r.Values["who"] != "" {
nick, id = p.resolveUser(r, r.Values["who"]) nick, id = p.resolveUser(r, r.Values["who"])
@ -603,6 +610,8 @@ func (p *CounterPlugin) incrementCmd(r bot.Request) bool {
} }
func (p *CounterPlugin) decrementCmd(r bot.Request) bool { func (p *CounterPlugin) decrementCmd(r bot.Request) bool {
p.modify.Lock()
defer p.modify.Unlock()
nick, id := r.Msg.User.Name, r.Msg.User.ID nick, id := r.Msg.User.Name, r.Msg.User.ID
if r.Values["who"] != "" { if r.Values["who"] != "" {
nick, id = p.resolveUser(r, r.Values["who"]) nick, id = p.resolveUser(r, r.Values["who"])
@ -628,6 +637,8 @@ func (p *CounterPlugin) decrementCmd(r bot.Request) bool {
} }
func (p *CounterPlugin) addToCmd(r bot.Request) bool { func (p *CounterPlugin) addToCmd(r bot.Request) bool {
p.modify.Lock()
defer p.modify.Unlock()
nick, id := p.resolveUser(r, r.Values["who"]) nick, id := p.resolveUser(r, r.Values["who"])
itemName := r.Values["thing"] itemName := r.Values["thing"]
channel := r.Msg.Channel channel := r.Msg.Channel
@ -652,6 +663,8 @@ func (p *CounterPlugin) addToCmd(r bot.Request) bool {
} }
func (p *CounterPlugin) removeFromCmd(r bot.Request) bool { func (p *CounterPlugin) removeFromCmd(r bot.Request) bool {
p.modify.Lock()
defer p.modify.Unlock()
nick, id := p.resolveUser(r, r.Values["who"]) nick, id := p.resolveUser(r, r.Values["who"])
itemName := r.Values["thing"] itemName := r.Values["thing"]
channel := r.Msg.Channel channel := r.Msg.Channel
@ -687,6 +700,8 @@ func (p *CounterPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message
} }
func (p *CounterPlugin) teaMatchCmd(r bot.Request) bool { func (p *CounterPlugin) teaMatchCmd(r bot.Request) bool {
p.modify.Lock()
defer p.modify.Unlock()
nick, id := r.Msg.User.Name, r.Msg.User.ID nick, id := r.Msg.User.Name, r.Msg.User.ID
channel := r.Msg.Channel channel := r.Msg.Channel