mcchunkie/plugins/beat.go
Aaron Bieber d6673169be Switch all the plugins to use the new Process function.
Expand sms to run Process for each plugin, add a "homestead" plugin while here!
\o/
2021-04-01 16:15:25 -06:00

52 lines
1.1 KiB
Go

package plugins
import (
"fmt"
"regexp"
"time"
"github.com/matrix-org/gomatrix"
)
// Beat responds to beat messages
type Beat struct {
}
// Descr describes this plugin
func (h *Beat) Descr() string {
return "Print the current [beat time](https://en.wikipedia.org/wiki/Swatch_Internet_Time)."
}
// Re is the regex for matching beat messages.
func (h *Beat) Re() string {
return `(?i)^\.beat$|^what time is it[\?!]+$|^beat( )?time:?\??$`
}
// Match determines if we are asking for a beat
func (h *Beat) Match(_, msg string) bool {
re := regexp.MustCompile(h.Re())
return re.MatchString(msg)
}
// SetStore we don't need a store here
func (h *Beat) SetStore(_ PluginStore) {}
// RespondText to beat request events
func (h *Beat) RespondText(c *gomatrix.Client, ev *gomatrix.Event, _, _ string) error {
return SendText(c, ev.RoomID, h.Process("", ""))
}
// Process does the heavy lifting of calculating .beat
func (h *Beat) Process(from, msg string) string {
n := time.Now()
utc1 := n.Unix() + 3600
r := utc1 % 86400
bt := float32(r) / 86.4
return fmt.Sprintf("@%03d", int32(bt))
}
// Name beat
func (h *Beat) Name() string {
return "Beat"
}