refactor a bit. this makes plugins a bit less blumpy

This commit is contained in:
Aaron Bieber 2020-02-02 19:43:26 -07:00
parent 41e6bf180c
commit 9d25ab6041
9 changed files with 81 additions and 118 deletions

13
main.go
View File

@ -131,7 +131,18 @@ func main() {
}
for _, p := range plugins.Plugs {
p.Respond(cli, ev, username)
var post string
var ok bool
if post, ok = ev.Body(); !ok {
// Invaild body, for some reason
return
}
if mtype, ok := ev.MessageType(); ok {
switch mtype {
case "m.text":
p.RespondText(cli, ev, username, post)
}
}
}
})

View File

@ -129,31 +129,24 @@ func (h *Beer) pretty(b BeerResp, random bool) string {
)
}
// Respond to looking up of beer requests
func (h *Beer) 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 {
if h.match(post) {
beer := h.fix(post)
if beer != "" {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
brr, err := h.get(beer)
if err != nil {
SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look for beer. (%s)", ev.Sender, err))
}
// RespondText to looking up of beer requests
func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if h.match(post) {
beer := h.fix(post)
if beer != "" {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
brr, err := h.get(beer)
if err != nil {
SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look for beer. (%s)", ev.Sender, err))
}
switch {
case brr.Nhits == 0:
SendText(c, ev.RoomID, "¯\\_(ツ)_/¯")
case brr.Nhits == 1:
SendText(c, ev.RoomID, fmt.Sprintf("%s", h.pretty(*brr, false)))
case brr.Nhits > 1:
SendText(c, ev.RoomID, fmt.Sprintf("Found %d beers, here is a random one:\n%s", brr.Nhits, h.pretty(*brr, true)))
}
}
}
switch {
case brr.Nhits == 0:
SendText(c, ev.RoomID, "¯\\_(ツ)_/¯")
case brr.Nhits == 1:
SendText(c, ev.RoomID, fmt.Sprintf("%s", h.pretty(*brr, false)))
case brr.Nhits > 1:
SendText(c, ev.RoomID, fmt.Sprintf("Found %d beers, here is a random one:\n%s", brr.Nhits, h.pretty(*brr, true)))
}
}
}

View File

@ -31,20 +31,13 @@ func (h *BotSnack) resp() string {
}
// 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)
SendText(c, ev.RoomID, h.resp())
}
}
}
// RespondText to hi events
func (h *BotSnack) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
u := NameRE.ReplaceAllString(user, "$1")
if ToMe(u, post) {
if h.match(post) {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, h.resp())
}
}
}

View File

@ -17,21 +17,14 @@ func (h *Hi) match(msg string) bool {
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)
SendText(c, ev.RoomID, fmt.Sprintf("hi %s!", s))
}
}
}
// RespondText to hi events
func (h *Hi) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
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)
SendText(c, ev.RoomID, fmt.Sprintf("hi %s!", s))
}
}
}

View File

@ -12,25 +12,18 @@ import (
type HighFive struct {
}
// Respond to high five events
func (h *HighFive) 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 strings.Contains(post, "o/") {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("\\o %s", s))
}
if strings.Contains(post, "\\o") {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("%s o/", s))
}
}
}
// RespondText to high five events
func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) {
if strings.Contains(post, "o/") {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("\\o %s", s))
}
if strings.Contains(post, "\\o") {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("%s o/", s))
}
}
}

View File

@ -32,20 +32,13 @@ func (h *LoveYou) resp() string {
}
// Respond to love events
func (h *LoveYou) 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)
SendText(c, ev.RoomID, h.resp())
}
}
}
// RespondText to love events
func (h *LoveYou) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
u := NameRE.ReplaceAllString(user, "$1")
if ToMe(u, post) {
if h.match(post) {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, h.resp())
}
}
}

View File

@ -10,7 +10,8 @@ import (
// Plugin defines the functions a plugin must implement to be used by
// mcchunkie.
type Plugin interface {
Respond(c *gomatrix.Client, ev *gomatrix.Event, user string)
//Respond(c *gomatrix.Client, ev *gomatrix.Event, user string)
RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, path string)
Name() string
}

View File

@ -17,21 +17,14 @@ func (h *Source) match(msg string) bool {
return re.MatchString(msg)
}
// Respond to questions about TheSource™©®⑨
func (h *Source) 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)
SendText(c, ev.RoomID, fmt.Sprintf("%s: %s ;D", s, "https://git.sr.ht/~qbit/mcchunkie"))
}
}
}
// RespondText to questions about TheSource™©®⑨
func (h *Source) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
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)
SendText(c, ev.RoomID, fmt.Sprintf("%s: %s ;D", s, "https://git.sr.ht/~qbit/mcchunkie"))
}
}
}

View File

@ -22,21 +22,14 @@ func (v *Version) print(to string) string {
return fmt.Sprintf("%s, I am written in Go, running on %s", to, runtime.GOOS)
}
// Respond to version events
func (v *Version) 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 v.match(post) {
log.Printf("%s: responding to '%s'", v.Name(), ev.Sender)
SendText(c, ev.RoomID, v.print(s))
}
}
}
// RespondText to version events
func (v *Version) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
u := NameRE.ReplaceAllString(user, "$1")
s := NameRE.ReplaceAllString(ev.Sender, "$1")
if ToMe(u, post) {
if v.match(post) {
log.Printf("%s: responding to '%s'", v.Name(), ev.Sender)
SendText(c, ev.RoomID, v.print(s))
}
}
}