sanitary high5

This commit is contained in:
Aaron Bieber 2020-03-13 15:05:08 -06:00
parent 45d7dc3ae6
commit 5be882f004
4 changed files with 36 additions and 7 deletions

View File

@ -10,7 +10,7 @@
|BotSnack|`(?i)botsnack`|Consumes a botsnack. This pleases mcchunkie and brings balance to the universe.|
|Feder|`(?i)^feder: (.*)$`|check the Matrix federation status of a given URL.|
|Ham|`(?i)^ham: (\w+)$`|queries the FCC's [ULS](https://wireless2.fcc.gov/UlsApp/UlsSearch/searchLicense.jsp) for a given callsign.|
|HighFive|`o/|\o`|Everyone loves highfives.|
|HighFive|`o/|\\o`|Everyone loves highfives.|
|Hi|`(?i)^hi|hi$`|Friendly bots say hi.|
|LoveYou|`(?i)i love you`|Spreading love where ever we can by responding when someone shows us love.|
|OpenBSDMan|`(?i)^man: ([1-9][p]?)?\s?(.+)$`|Produces a link to man.openbsd.org.|

View File

@ -4,7 +4,7 @@ import (
"testing"
)
func TestMatch(t *testing.T) {
func TestBeatMatch(t *testing.T) {
testStrings := make(map[string]bool)
testStrings["what time is it??!!?!"] = true
testStrings["what time is it"] = false

View File

@ -2,7 +2,8 @@ package plugins
import (
"fmt"
"strings"
"regexp"
"time"
"github.com/matrix-org/gomatrix"
)
@ -16,7 +17,7 @@ func rightFive() string {
}
func leftFive() string {
return "\\o"
return `\\o`
}
// Descr describes this plugin
@ -34,18 +35,26 @@ func (h *HighFive) SetStore(s PluginStore) {}
// Match determines if we should bother giving a high five
func (h *HighFive) Match(user, msg string) bool {
return ToMe(user, msg)
re := regexp.MustCompile(h.Re())
return ToMe(user, msg) && re.MatchString(msg)
}
// RespondText to high five events
func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
s := NameRE.ReplaceAllString(ev.Sender, "$1")
if strings.Contains(post, rightFive()) {
rm := regexp.MustCompile(rightFive())
lm := regexp.MustCompile(leftFive())
if rm.MatchString(post) {
SendText(c, ev.RoomID, fmt.Sprintf("\\o %s", s))
time.Sleep(time.Second * 5)
SendText(c, ev.RoomID, fmt.Sprintf("now go wash your hands, %s", s))
}
if strings.Contains(post, leftFive()) {
if lm.MatchString(post) {
SendText(c, ev.RoomID, fmt.Sprintf("%s o/", s))
time.Sleep(time.Second * 5)
SendText(c, ev.RoomID, fmt.Sprintf("now go wash your hands, %s", s))
}
}

20
plugins/highfive_test.go Normal file
View File

@ -0,0 +1,20 @@
package plugins
import (
"testing"
)
func TestHighFiveMatch(t *testing.T) {
testStrings := make(map[string]bool)
testStrings["o/"] = true
testStrings[`\o`] = true
testStrings["_o"] = false
testStrings["o_"] = false
h := &HighFive{}
for msg, should := range testStrings {
if h.Match("", msg) != should {
t.Errorf("HighFive expected to match %q (%t); but doesn't\n", msg, should)
}
}
}