mirror of https://github.com/velour/catbase.git
counter: add rmalias and some help
This commit is contained in:
parent
06bf6fd733
commit
d99ee28370
|
@ -100,25 +100,42 @@ func Leader(db *sqlx.DB, itemName string) ([]Item, error) {
|
||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MkAlias(db *sqlx.DB, item, pointsTo string) (*alias, error) {
|
func RmAlias(db *sqlx.DB, aliasName string) error {
|
||||||
item = strings.ToLower(item)
|
q := `delete from counter_alias where item = ?`
|
||||||
|
tx, err := db.Beginx()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(q, aliasName)
|
||||||
|
if err != nil {
|
||||||
|
if err := tx.Rollback(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = tx.Commit()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func MkAlias(db *sqlx.DB, aliasName, pointsTo string) (*alias, error) {
|
||||||
|
aliasName = strings.ToLower(aliasName)
|
||||||
pointsTo = strings.ToLower(pointsTo)
|
pointsTo = 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 (?, ?)`,
|
||||||
item, pointsTo)
|
aliasName, pointsTo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_, err := db.Exec(`update counter_alias set points_to=? where item=?`, pointsTo, item)
|
_, err := db.Exec(`update counter_alias set points_to=? where item=?`, pointsTo, aliasName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var a alias
|
var a alias
|
||||||
if err := db.Get(&a, `select * from counter_alias where item=?`, item); err != nil {
|
if err := db.Get(&a, `select * from counter_alias where item=?`, aliasName); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &a, nil
|
return &a, nil
|
||||||
}
|
}
|
||||||
id, _ := res.LastInsertId()
|
id, _ := res.LastInsertId()
|
||||||
|
|
||||||
return &alias{db, id, item, pointsTo}, nil
|
return &alias{db, id, aliasName, pointsTo}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetItem returns a specific counter for a subject
|
// GetItem returns a specific counter for a subject
|
||||||
|
@ -242,12 +259,21 @@ func (p *CounterPlugin) message(c bot.Connector, kind bot.Kind, message msg.Mess
|
||||||
|
|
||||||
if len(parts) == 3 && strings.ToLower(parts[0]) == "mkalias" {
|
if len(parts) == 3 && strings.ToLower(parts[0]) == "mkalias" {
|
||||||
if _, err := MkAlias(p.DB, parts[1], parts[2]); err != nil {
|
if _, err := MkAlias(p.DB, parts[1], parts[2]); err != nil {
|
||||||
log.Error().Err(err)
|
log.Error().Err(err).Msg("Could not mkalias")
|
||||||
return false
|
p.Bot.Send(c, bot.Message, channel, "We're gonna need too much DB space to make an alias for your mom.")
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
p.Bot.Send(c, bot.Message, channel, fmt.Sprintf("Created alias %s -> %s",
|
p.Bot.Send(c, bot.Message, channel, fmt.Sprintf("Created alias %s -> %s",
|
||||||
parts[1], parts[2]))
|
parts[1], parts[2]))
|
||||||
return true
|
return true
|
||||||
|
} else if len(parts) == 2 && strings.ToLower(parts[0]) == "rmalias" {
|
||||||
|
if err := RmAlias(p.DB, parts[1]); err != nil {
|
||||||
|
log.Error().Err(err).Msg("could not RmAlias")
|
||||||
|
p.Bot.Send(c, bot.Message, channel, "`sudo rm your mom` => Nope, she's staying with me.")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
p.Bot.Send(c, bot.Message, channel, "`sudo rm your mom`")
|
||||||
|
return true
|
||||||
} else if strings.ToLower(parts[0]) == "leaderboard" {
|
} else if strings.ToLower(parts[0]) == "leaderboard" {
|
||||||
var cmd func() ([]Item, error)
|
var cmd func() ([]Item, error)
|
||||||
itNameTxt := ""
|
itNameTxt := ""
|
||||||
|
@ -515,9 +541,11 @@ func (p *CounterPlugin) message(c bot.Connector, kind bot.Kind, message msg.Mess
|
||||||
// Help responds to help requests. Every plugin must implement a help function.
|
// Help responds to help requests. Every plugin must implement a help function.
|
||||||
func (p *CounterPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
func (p *CounterPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
||||||
p.Bot.Send(c, bot.Message, message.Channel, "You can set counters incrementally by using "+
|
p.Bot.Send(c, bot.Message, message.Channel, "You can set counters incrementally by using "+
|
||||||
"<noun>++ and <noun>--. You can see all of your counters using "+
|
"`<noun>++` and `<noun>--`. You can see all of your counters using "+
|
||||||
"\"inspect\", erase them with \"clear\", and view single counters with "+
|
"`inspect`, erase them with `clear`, and view single counters with "+
|
||||||
"\"count\".")
|
"`count`.")
|
||||||
|
p.Bot.Send(c, bot.Message, message.Channel, "You can create aliases with `!mkalias <alias> <original>`")
|
||||||
|
p.Bot.Send(c, bot.Message, message.Channel, "You can remove aliases with `!rmalias <alias>`")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/velour/catbase/plugins/cli"
|
"github.com/velour/catbase/plugins/cli"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/velour/catbase/bot"
|
"github.com/velour/catbase/bot"
|
||||||
"github.com/velour/catbase/bot/msg"
|
"github.com/velour/catbase/bot/msg"
|
||||||
"github.com/velour/catbase/bot/user"
|
"github.com/velour/catbase/bot/user"
|
||||||
|
@ -37,6 +38,27 @@ func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMkAlias(t *testing.T) {
|
||||||
|
mb, c := setup(t)
|
||||||
|
assert.NotNil(t, c)
|
||||||
|
c.message(makeMessage("mkalias fuck mornings"))
|
||||||
|
c.message(makeMessage("fuck++"))
|
||||||
|
item, err := GetItem(mb.DB(), "tester", "mornings")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, 1, item.Count)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRmAlias(t *testing.T) {
|
||||||
|
mb, c := setup(t)
|
||||||
|
assert.NotNil(t, c)
|
||||||
|
c.message(makeMessage("mkalias fuck mornings"))
|
||||||
|
c.message(makeMessage("rmalias fuck"))
|
||||||
|
c.message(makeMessage("fuck++"))
|
||||||
|
item, err := GetItem(mb.DB(), "tester", "mornings")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, 0, item.Count)
|
||||||
|
}
|
||||||
|
|
||||||
func TestThreeSentencesExists(t *testing.T) {
|
func TestThreeSentencesExists(t *testing.T) {
|
||||||
mb, c := setup(t)
|
mb, c := setup(t)
|
||||||
assert.NotNil(t, c)
|
assert.NotNil(t, c)
|
||||||
|
@ -253,5 +275,5 @@ func TestHelp(t *testing.T) {
|
||||||
mb, c := setup(t)
|
mb, c := setup(t)
|
||||||
assert.NotNil(t, c)
|
assert.NotNil(t, c)
|
||||||
c.help(&cli.CliPlugin{}, bot.Help, msg.Message{Channel: "channel"}, []string{})
|
c.help(&cli.CliPlugin{}, bot.Help, msg.Message{Channel: "channel"}, []string{})
|
||||||
assert.Len(t, mb.Messages, 1)
|
assert.Greater(t, len(mb.Messages), 1)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue