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 { 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 // RespondText to looking up of beer requests
func (h *Beer) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (h *Beer) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok { if h.match(post) {
switch mtype { beer := h.fix(post)
case "m.text": if beer != "" {
if post, ok := ev.Body(); ok { log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
if h.match(post) { brr, err := h.get(beer)
beer := h.fix(post) if err != nil {
if beer != "" { SendText(c, ev.RoomID, fmt.Sprintf("sorry %s, I can't look for beer. (%s)", ev.Sender, err))
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 { switch {
case brr.Nhits == 0: case brr.Nhits == 0:
SendText(c, ev.RoomID, "¯\\_(ツ)_/¯") SendText(c, ev.RoomID, "¯\\_(ツ)_/¯")
case brr.Nhits == 1: case brr.Nhits == 1:
SendText(c, ev.RoomID, fmt.Sprintf("%s", h.pretty(*brr, false))) SendText(c, ev.RoomID, fmt.Sprintf("%s", h.pretty(*brr, false)))
case brr.Nhits > 1: 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))) 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 // RespondText to hi events
func (h *BotSnack) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (h *BotSnack) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok { u := NameRE.ReplaceAllString(user, "$1")
switch mtype { if ToMe(u, post) {
case "m.text": if h.match(post) {
if post, ok := ev.Body(); ok { log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
u := NameRE.ReplaceAllString(user, "$1") SendText(c, ev.RoomID, h.resp())
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) return re.MatchString(msg)
} }
// Respond to hi events // RespondText to hi events
func (h *Hi) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (h *Hi) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok { u := NameRE.ReplaceAllString(user, "$1")
switch mtype { s := NameRE.ReplaceAllString(ev.Sender, "$1")
case "m.text": if ToMe(u, post) {
if post, ok := ev.Body(); ok { if h.match(post) {
u := NameRE.ReplaceAllString(user, "$1") log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
s := NameRE.ReplaceAllString(ev.Sender, "$1") SendText(c, ev.RoomID, fmt.Sprintf("hi %s!", s))
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 { type HighFive struct {
} }
// Respond to high five events // RespondText to high five events
func (h *HighFive) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (h *HighFive) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok { u := NameRE.ReplaceAllString(user, "$1")
switch mtype { s := NameRE.ReplaceAllString(ev.Sender, "$1")
case "m.text": if ToMe(u, post) {
if post, ok := ev.Body(); ok { if strings.Contains(post, "o/") {
u := NameRE.ReplaceAllString(user, "$1") log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
s := NameRE.ReplaceAllString(ev.Sender, "$1") SendText(c, ev.RoomID, fmt.Sprintf("\\o %s", s))
if ToMe(u, post) { }
if strings.Contains(post, "o/") { if strings.Contains(post, "\\o") {
log.Printf("%s: responding to '%s'", h.Name(), ev.Sender) log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
SendText(c, ev.RoomID, fmt.Sprintf("\\o %s", s)) SendText(c, ev.RoomID, fmt.Sprintf("%s o/", 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 // RespondText to love events
func (h *LoveYou) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (h *LoveYou) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok { u := NameRE.ReplaceAllString(user, "$1")
switch mtype { if ToMe(u, post) {
case "m.text": if h.match(post) {
if post, ok := ev.Body(); ok { log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
u := NameRE.ReplaceAllString(user, "$1") SendText(c, ev.RoomID, h.resp())
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 // Plugin defines the functions a plugin must implement to be used by
// mcchunkie. // mcchunkie.
type Plugin interface { 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 Name() string
} }

View File

@ -17,21 +17,14 @@ func (h *Source) match(msg string) bool {
return re.MatchString(msg) return re.MatchString(msg)
} }
// Respond to questions about TheSource™©®⑨ // RespondText to questions about TheSource™©®⑨
func (h *Source) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (h *Source) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok { u := NameRE.ReplaceAllString(user, "$1")
switch mtype { s := NameRE.ReplaceAllString(ev.Sender, "$1")
case "m.text": if ToMe(u, post) {
if post, ok := ev.Body(); ok { if h.match(post) {
u := NameRE.ReplaceAllString(user, "$1") log.Printf("%s: responding to '%s'", h.Name(), ev.Sender)
s := NameRE.ReplaceAllString(ev.Sender, "$1") SendText(c, ev.RoomID, fmt.Sprintf("%s: %s ;D", s, "https://git.sr.ht/~qbit/mcchunkie"))
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) return fmt.Sprintf("%s, I am written in Go, running on %s", to, runtime.GOOS)
} }
// Respond to version events // RespondText to version events
func (v *Version) Respond(c *gomatrix.Client, ev *gomatrix.Event, user string) { func (v *Version) RespondText(c *gomatrix.Client, ev *gomatrix.Event, user, post string) {
if mtype, ok := ev.MessageType(); ok { u := NameRE.ReplaceAllString(user, "$1")
switch mtype { s := NameRE.ReplaceAllString(ev.Sender, "$1")
case "m.text": if ToMe(u, post) {
if post, ok := ev.Body(); ok { if v.match(post) {
u := NameRE.ReplaceAllString(user, "$1") log.Printf("%s: responding to '%s'", v.Name(), ev.Sender)
s := NameRE.ReplaceAllString(ev.Sender, "$1") SendText(c, ev.RoomID, v.print(s))
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))
}
}
}
} }
} }
} }