mcchunkie/plugins/hi.go

43 lines
803 B
Go
Raw Normal View History

2020-01-31 19:58:17 -07:00
package plugins
import (
"fmt"
"log"
"regexp"
"github.com/matrix-org/gomatrix"
)
// Hi responds to hi messages
type Hi struct {
}
func (h *Hi) match(msg string) bool {
2020-02-01 15:14:24 -07:00
re := regexp.MustCompile(`(?i)^hi|hi$`)
2020-01-31 19:58:17 -07:00
return re.MatchString(msg)
}
// Respond to hi events
func (h *Hi) 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")
s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) {
if h.match(post) {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendMessage(c, ev.RoomID, fmt.Sprintf("hi %s!", s))
}
}
}
}
}
}
// Name hi
func (h *Hi) Name() string {
return "Hi"
}