add banana stabber + emote sending

Signed-off-by: Aisha Tammy <aisha@bsd.ac>
This commit is contained in:
Aisha Tammy 2022-03-11 14:38:28 -05:00
parent 53eb25ae33
commit e2327e9980
2 changed files with 78 additions and 0 deletions

57
plugins/banana_stab.go Normal file
View File

@ -0,0 +1,57 @@
package plugins
import (
"fmt"
"regexp"
"github.com/matrix-org/gomatrix"
)
type BananaStab struct {
}
// Descr describes this plugin
func (h *BananaStab) Descr() string {
return "Stab someone with some bananas."
}
// Re matches our stabbing format
func (h *BananaStab) Re() string {
return `(?i)^stab: (.+)$`
}
func (h *BananaStab) fix(msg string) string {
re := regexp.MustCompile(h.Re())
stabee := re.ReplaceAllString(msg, "$1")
return stabee
}
// Match checks for our stabee person
func (h *BananaStab) Match(_, msg string) bool {
re := regexp.MustCompile(h.Re())
return re.MatchString(msg)
}
// SetStore does nothing in BananaStab
func (h *BananaStab) SetStore(_ PluginStore) {}
func (h *BananaStab) Process(from, post string) string {
stabee := h.fix(post)
stabtxt := "..."
if stabee != "" {
stabtxt = fmt.Sprintf("stabs %s with the fury of a thousand radioactive bananas", stabee)
}
//jsonmsg := "{ \"body\": \"" + stabtxt + "\", \"type\": \"m.emote\"}"
return stabtxt
}
// RespondText stabs an unsuspecting person
func (h *BananaStab) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, post string) error {
err := SendEmote(c, ev.RoomID, h.Process(ev.Sender, post))
return err
}
// Name BananaStab!
func (h *BananaStab) Name() string {
return "BananaStab"
}

View File

@ -139,6 +139,26 @@ func SendText(c *gomatrix.Client, roomID, message string) error {
return nil return nil
} }
// SendEmote sends an emote to a given room. It pretends to be
// "typing" by calling UserTyping for the caller.
func SendEmote(c *gomatrix.Client, roomID, message string) error {
_, err := c.UserTyping(roomID, true, 3)
if err != nil {
return err
}
_, err = c.SendMessageEvent(roomID, "m.room.message", gomatrix.GetHTMLMessage("m.emote", message))
if err != nil {
return err
}
_, err = c.UserTyping(roomID, false, 0)
if err != nil {
return err
}
return nil
}
// SendHTML sends an html message to a given room. It pretends to be // SendHTML sends an html message to a given room. It pretends to be
// "typing" by calling UserTyping for the caller. // "typing" by calling UserTyping for the caller.
func SendHTML(c *gomatrix.Client, roomID, message string) error { func SendHTML(c *gomatrix.Client, roomID, message string) error {
@ -262,6 +282,7 @@ type Plugins []Plugin
// Plugs defines the "enabled" plugins. // Plugs defines the "enabled" plugins.
var Plugs = Plugins{ var Plugs = Plugins{
&BananaStab{},
&Beat{}, &Beat{},
&Beer{}, &Beer{},
&BotSnack{}, &BotSnack{},