From 2957b6f4d5efde6704bca83e98a205ac3a36bd53 Mon Sep 17 00:00:00 2001 From: cws Date: Mon, 23 Jan 2017 21:13:21 -0500 Subject: [PATCH] Add reset me command --- plugins/counter/counter.go | 19 ++++++++++++++++--- plugins/counter/counter_test.go | 11 +++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index 32f9b6e..abd5c3f 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -38,8 +38,8 @@ func GetItems(db *sqlx.DB, nick string) ([]Item, error) { return nil, err } // Don't forget to embed the DB into all of that shiz - for _, i := range items { - i.DB = db + for i := range items { + items[i].DB = db } return items, nil } @@ -135,7 +135,20 @@ func (p *CounterPlugin) Message(message msg.Message) bool { return false } - if message.Command && parts[0] == "inspect" && len(parts) == 2 { + if message.Command && message.Body == "reset me" { + items, err := GetItems(p.DB, strings.ToLower(nick)) + if err != nil { + log.Printf("Error getting items to reset %s: %s", nick, err) + p.Bot.SendMessage(channel, "Something is technically wrong with your counters.") + return true + } + log.Printf("Items: %+v", items) + for _, item := range items { + item.Delete() + } + p.Bot.SendMessage(channel, fmt.Sprintf("%s, you are as new, my son.", nick)) + return true + } else if message.Command && parts[0] == "inspect" && len(parts) == 2 { var subject string if parts[1] == "me" { diff --git a/plugins/counter/counter_test.go b/plugins/counter/counter_test.go index 9f1f60c..189458a 100644 --- a/plugins/counter/counter_test.go +++ b/plugins/counter/counter_test.go @@ -26,6 +26,17 @@ func makeMessage(payload string) msg.Message { } } +func TestResetMe(t *testing.T) { + mb := bot.NewMockBot() + c := New(mb) + assert.NotNil(t, c) + c.Message(makeMessage("test++")) + c.Message(makeMessage("!reset me")) + items, err := GetItems(mb.DB(), "tester") + assert.Nil(t, err) + assert.Len(t, items, 0) +} + func TestCounterOne(t *testing.T) { mb := bot.NewMockBot() c := New(mb)