From 3a7651d1841ce6f8b71da696a17177f90ce2f7a8 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 20 Jan 2019 12:33:19 -0500 Subject: [PATCH] fact: add reaction type facts If a user creates a fact with the verb , catbase will try to react with the emojy that the user specifies. It filters things with spaces and fixes colons, but does not check if the emojy actually exists. There will be no feedback in this case, which should probably get fixed but meh. * Updated mock bot to check reactions, and do filtering correctly. * Added a couple tests of the react functionality. --- bot/mock.go | 12 ++++++++---- plugins/fact/factoid.go | 26 ++++++++++++++++++-------- plugins/fact/remember_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/bot/mock.go b/bot/mock.go index 7d6c6e6..509013e 100644 --- a/bot/mock.go +++ b/bot/mock.go @@ -21,8 +21,9 @@ type MockBot struct { Cfg config.Config - Messages []string - Actions []string + Messages []string + Actions []string + Reactions []string } func (mb *MockBot) Config() *config.Config { return &mb.Cfg } @@ -47,11 +48,14 @@ func (mb *MockBot) ReplyToMessage(channel, message string, replyTo msg.Message) } func (mb *MockBot) MsgReceived(msg msg.Message) {} func (mb *MockBot) EventReceived(msg msg.Message) {} -func (mb *MockBot) Filter(msg msg.Message, s string) string { return "" } +func (mb *MockBot) Filter(msg msg.Message, s string) string { return s } func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil } func (mb *MockBot) CheckAdmin(nick string) bool { return false } -func (mb *MockBot) React(channel, reaction string, message msg.Message) bool { return false } +func (mb *MockBot) React(channel, reaction string, message msg.Message) bool { + mb.Reactions = append(mb.Reactions, reaction) + return false +} func (mb *MockBot) Edit(channel, newMessage, identifier string) bool { isMessage := identifier[0] == 'm' diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index 17a4e4a..b86e72e 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -338,8 +338,16 @@ func findAction(message string) string { // learnFact assumes we have a learning situation and inserts a new fact // into the database -func (p *Factoid) learnFact(message msg.Message, fact, verb, tidbit string) bool { +func (p *Factoid) learnFact(message msg.Message, fact, verb, tidbit string) error { verb = strings.ToLower(verb) + if verb == "react" { + // This would be a great place to check against the API for valid emojy + // I'm too lazy for that + tidbit = strings.Replace(tidbit, ":", "", -1) + if len(strings.Split(tidbit, " ")) > 1 { + return fmt.Errorf("That's not a valid emojy.") + } + } var count sql.NullInt64 err := p.db.QueryRow(`select count(*) from factoid @@ -347,10 +355,10 @@ func (p *Factoid) learnFact(message msg.Message, fact, verb, tidbit string) bool fact, verb, tidbit).Scan(&count) if err != nil { log.Println("Error counting facts: ", err) - return false + return fmt.Errorf("What?") } else if count.Valid && count.Int64 != 0 { log.Println("User tried to relearn a fact.") - return false + return fmt.Errorf("Look, I already know that.") } n := factoid{ @@ -366,10 +374,10 @@ func (p *Factoid) learnFact(message msg.Message, fact, verb, tidbit string) bool err = n.save(p.db) if err != nil { log.Println("Error inserting fact: ", err) - return false + return fmt.Errorf("My brain is overheating.") } - return true + return nil } // findTrigger checks to see if a given string is a trigger or not @@ -398,6 +406,8 @@ func (p *Factoid) sayFact(message msg.Message, fact factoid) { if fact.Verb == "action" { p.Bot.SendAction(message.Channel, msg) + } else if fact.Verb == "react" { + p.Bot.React(message.Channel, msg, message) } else if fact.Verb == "reply" { p.Bot.SendMessage(message.Channel, msg) } else { @@ -476,10 +486,10 @@ func (p *Factoid) learnAction(message msg.Message, action string) bool { strippedaction := strings.Replace(strings.Replace(action, "<", "", 1), ">", "", 1) - if p.learnFact(message, trigger, strippedaction, fact) { - p.Bot.SendMessage(message.Channel, fmt.Sprintf("Okay, %s.", message.User.Name)) + if err := p.learnFact(message, trigger, strippedaction, fact); err != nil { + p.Bot.SendMessage(message.Channel, err.Error()) } else { - p.Bot.SendMessage(message.Channel, "I already know that.") + p.Bot.SendMessage(message.Channel, fmt.Sprintf("Okay, %s.", message.User.Name)) } return true diff --git a/plugins/fact/remember_test.go b/plugins/fact/remember_test.go index 3cbc835..6e8c8a1 100644 --- a/plugins/fact/remember_test.go +++ b/plugins/fact/remember_test.go @@ -50,3 +50,30 @@ func TestCornerCaseBug(t *testing.T) { assert.Nil(t, err) assert.Contains(t, q.Tidbit, "horse dick") } + +func TestReact(t *testing.T) { + msgs := []msg.Message{ + makeMessage("user1", "!testing123 jesus"), + makeMessage("user2", "testing123"), + } + _, p, mb := makePlugin(t) + + for _, m := range msgs { + p.Message(m) + } + assert.Len(t, mb.Reactions, 1) + assert.Contains(t, mb.Reactions[0], "jesus") +} + +func TestReactCantLearnSpaces(t *testing.T) { + msgs := []msg.Message{ + makeMessage("user1", "!test jesus christ"), + } + _, p, mb := makePlugin(t) + + for _, m := range msgs { + p.Message(m) + } + assert.Len(t, mb.Messages, 1) + assert.Contains(t, mb.Messages[0], "not a valid") +}