From 493f07fc6d60f046e7a8b25a9733dc3a7dfc7a39 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Fri, 31 Jan 2020 20:27:22 -0700 Subject: [PATCH] add botsnack --- plugins/botsnack.go | 55 +++++++++++++++++++++++++++++++++++++++++++++ plugins/plugins.go | 1 + 2 files changed, 56 insertions(+) create mode 100644 plugins/botsnack.go diff --git a/plugins/botsnack.go b/plugins/botsnack.go new file mode 100644 index 0000000..a84aeb2 --- /dev/null +++ b/plugins/botsnack.go @@ -0,0 +1,55 @@ +package plugins + +import ( + "log" + "math/rand" + "regexp" + "time" + + "github.com/matrix-org/gomatrix" +) + +// BotSnack responds to hi messages +type BotSnack struct { +} + +func (h *BotSnack) match(msg string) bool { + re := regexp.MustCompile(`botsnack`) + return re.MatchString(msg) +} + +func (h *BotSnack) resp() string { + a := []string{ + "omm nom nom nom", + "*puke*", + "MOAR!", + "=.=", + } + + rand.Seed(time.Now().Unix()) + return a[rand.Intn(len(a))] + +} + +// Respond to hi events +func (h *BotSnack) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { + if mtype, ok := ev.MessageType(); ok { + switch mtype { + case "m.text": + if post, ok := ev.Body(); ok { + u := NameRE.ReplaceAllString(user, "$1") + if ToMe(u, post) { + if h.match(post) { + log.Printf("%s: responding to '%s'", h.Name(), ev.Sender) + SendMessage(c, ev.RoomID, h.resp()) + } + } + } + } + } +} + +// Name hi +func (h *BotSnack) Name() string { + return "BotSnack" +} diff --git a/plugins/plugins.go b/plugins/plugins.go index 1bbb84a..cb4753a 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -42,6 +42,7 @@ type Plugins []Plugin // Plugs are all of our plugins var Plugs = Plugins{ + &BotSnack{}, &HighFive{}, &Hi{}, }