fact: add reaction type facts

If a user creates a fact with the verb <react>, 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.
This commit is contained in:
Chris Sexton 2019-01-20 12:33:19 -05:00
parent 0d730ee08a
commit 3a7651d184
3 changed files with 53 additions and 12 deletions

View File

@ -23,6 +23,7 @@ type MockBot struct {
Messages []string Messages []string
Actions []string Actions []string
Reactions []string
} }
func (mb *MockBot) Config() *config.Config { return &mb.Cfg } 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) MsgReceived(msg msg.Message) {}
func (mb *MockBot) EventReceived(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) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil }
func (mb *MockBot) CheckAdmin(nick string) bool { return false } 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 { func (mb *MockBot) Edit(channel, newMessage, identifier string) bool {
isMessage := identifier[0] == 'm' isMessage := identifier[0] == 'm'

View File

@ -338,8 +338,16 @@ func findAction(message string) string {
// learnFact assumes we have a learning situation and inserts a new fact // learnFact assumes we have a learning situation and inserts a new fact
// into the database // 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) 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 var count sql.NullInt64
err := p.db.QueryRow(`select count(*) from factoid 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) fact, verb, tidbit).Scan(&count)
if err != nil { if err != nil {
log.Println("Error counting facts: ", err) log.Println("Error counting facts: ", err)
return false return fmt.Errorf("What?")
} else if count.Valid && count.Int64 != 0 { } else if count.Valid && count.Int64 != 0 {
log.Println("User tried to relearn a fact.") log.Println("User tried to relearn a fact.")
return false return fmt.Errorf("Look, I already know that.")
} }
n := factoid{ n := factoid{
@ -366,10 +374,10 @@ func (p *Factoid) learnFact(message msg.Message, fact, verb, tidbit string) bool
err = n.save(p.db) err = n.save(p.db)
if err != nil { if err != nil {
log.Println("Error inserting fact: ", err) 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 // 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" { if fact.Verb == "action" {
p.Bot.SendAction(message.Channel, msg) p.Bot.SendAction(message.Channel, msg)
} else if fact.Verb == "react" {
p.Bot.React(message.Channel, msg, message)
} else if fact.Verb == "reply" { } else if fact.Verb == "reply" {
p.Bot.SendMessage(message.Channel, msg) p.Bot.SendMessage(message.Channel, msg)
} else { } else {
@ -476,10 +486,10 @@ func (p *Factoid) learnAction(message msg.Message, action string) bool {
strippedaction := strings.Replace(strings.Replace(action, "<", "", 1), ">", "", 1) strippedaction := strings.Replace(strings.Replace(action, "<", "", 1), ">", "", 1)
if p.learnFact(message, trigger, strippedaction, fact) { if err := p.learnFact(message, trigger, strippedaction, fact); err != nil {
p.Bot.SendMessage(message.Channel, fmt.Sprintf("Okay, %s.", message.User.Name)) p.Bot.SendMessage(message.Channel, err.Error())
} else { } else {
p.Bot.SendMessage(message.Channel, "I already know that.") p.Bot.SendMessage(message.Channel, fmt.Sprintf("Okay, %s.", message.User.Name))
} }
return true return true

View File

@ -50,3 +50,30 @@ func TestCornerCaseBug(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Contains(t, q.Tidbit, "horse dick") assert.Contains(t, q.Tidbit, "horse dick")
} }
func TestReact(t *testing.T) {
msgs := []msg.Message{
makeMessage("user1", "!testing123 <react> 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 <react> 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")
}