mcchunkie/plugins/beat.go

52 lines
1.1 KiB
Go
Raw Normal View History

2020-02-12 20:12:44 -07:00
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)."
}
2020-03-09 07:43:48 -06:00
// Re is the regex for matching beat messages.
2020-02-12 20:12:44 -07:00
func (h *Beat) Re() string {
2020-03-09 07:43:48 -06:00
return `(?i)^\.beat$|^what time is it[\?!]+$|^beat( )?time:?\??$`
2020-02-12 20:12:44 -07:00
}
2020-03-09 07:43:48 -06:00
// Match determines if we are asking for a beat
2020-05-13 17:05:01 -06:00
func (h *Beat) Match(_, msg string) bool {
2020-02-12 20:12:44 -07:00
re := regexp.MustCompile(h.Re())
return re.MatchString(msg)
}
// SetStore we don't need a store here
2020-05-13 17:05:01 -06:00
func (h *Beat) SetStore(_ PluginStore) {}
2020-02-12 20:12:44 -07:00
2020-03-09 07:43:48 -06:00
// RespondText to beat request events
2020-05-13 17:05:01 -06:00
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 {
2020-02-12 20:12:44 -07:00
n := time.Now()
utc1 := n.Unix() + 3600
r := utc1 % 86400
bt := float32(r) / 86.4
return fmt.Sprintf("@%03d", int32(bt))
2020-02-12 20:12:44 -07:00
}
2020-03-09 07:43:48 -06:00
// Name beat
2020-02-12 20:12:44 -07:00
func (h *Beat) Name() string {
return "Beat"
}