From 3dd1ce9903fec36c30d2502c9b406070625fc497 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Wed, 12 Feb 2020 20:13:05 -0700 Subject: [PATCH] +thanks plugin --- README.md | 1 + plugins/plugins.go | 1 + plugins/thanks.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 plugins/thanks.go diff --git a/README.md b/README.md index 5ca38de..86cb956 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ |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?(\w+)$`|Produces a link to man.openbsd.org.| |Source|`(?i)where is your (source|code)`|Tell people where they can find more information about myself.| +|Thanks|`(?i)^thank you|thank you$|^thanks|thanks$|^ty|ty$`|Bots should be respectful. Respond to thanks.| |Version|`(?i)version$`|Show a bit of information about what we are.| |Wb|`(?i)^welcome back|welcome back$|^wb|wb$`|Respond to welcome back messages.| |Weather|`(?i)^weather: (\d+)$`|Produce weather information for a given ZIP code. Data comes from openweathermap.org.| diff --git a/plugins/plugins.go b/plugins/plugins.go index 3841dad..b84832c 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -78,6 +78,7 @@ var Plugs = Plugins{ &LoveYou{}, &OpenBSDMan{}, &Source{}, + &Thanks{}, &Version{}, &Wb{}, &Weather{}, diff --git a/plugins/thanks.go b/plugins/thanks.go new file mode 100644 index 0000000..d2600e4 --- /dev/null +++ b/plugins/thanks.go @@ -0,0 +1,56 @@ +package plugins + +import ( + "fmt" + "log" + "math/rand" + "regexp" + "time" + + "github.com/matrix-org/gomatrix" +) + +// Thanks responds to thanks +type Thanks struct { +} + +// Descr describes this plugin +func (h *Thanks) Descr() string { + return "Bots should be respectful. Respond to thanks." +} + +// Re checks for various forms of thanks +func (h *Thanks) Re() string { + return `(?i)^thank you|thank you$|^thanks|thanks$|^ty|ty$` +} + +// Match determins if we are being thanked +func (h *Thanks) Match(user, msg string) bool { + re := regexp.MustCompile(h.Re()) + return re.MatchString(msg) && ToMe(user, msg) +} + +// SetStore we don't need a store here +func (h *Thanks) SetStore(s PluginStore) {} + +// RespondText to welcome back events +func (h *Thanks) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) { + s := NameRE.ReplaceAllString(ev.Sender, "$1") + a := []string{ + fmt.Sprintf("welcome %s", s), + "welcome", + "you're welcome", + "np!", + fmt.Sprintf("you'r welcome, %s", s), + } + + rand.Seed(time.Now().Unix()) + + log.Printf("%s: responding to '%s'", h.Name(), ev.Sender) + SendText(c, ev.RoomID, a[rand.Intn(len(a))]) +} + +// Name Thanks +func (h *Thanks) Name() string { + return "Thanks" +}