mcchunkie/plugins/thanks.go

57 lines
1.2 KiB
Go
Raw Normal View History

2020-02-12 20:13:05 -07:00
package plugins
import (
"fmt"
"math/rand"
"regexp"
"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$`
}
2020-05-13 17:36:26 -06:00
// Match determines if we are being thanked
2020-02-12 20:13:05 -07:00
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
2020-05-13 17:05:01 -06:00
func (h *Thanks) SetStore(_ PluginStore) {}
2020-02-12 20:13:05 -07:00
// Process
func (h *Thanks) Process(from, post string) string {
s := NameRE.ReplaceAllString(from, "$1")
2020-02-12 20:13:05 -07:00
a := []string{
fmt.Sprintf("welcome %s", s),
"welcome",
"you're welcome",
"np!",
2020-03-10 10:30:54 -06:00
fmt.Sprintf("you're welcome, %s", s),
2020-02-12 20:13:05 -07:00
}
return a[rand.Intn(len(a))]
}
// RespondText to welcome back events
func (h *Thanks) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, _ string) error {
return SendText(c, ev.RoomID, h.Process(ev.Sender, ""))
2020-02-12 20:13:05 -07:00
}
// Name Thanks
func (h *Thanks) Name() string {
return "Thanks"
}