helperbot/plugins/plugin_stats.go

70 lines
1.6 KiB
Go

package plugins
import (
"fmt"
"strings"
"git.bullercodeworks.com/brian/helperbot/models"
"github.com/slack-go/slack"
)
type StatsState struct {
model *models.BotModel
}
/* Plugin Interface Functions */
func (s *StatsState) Name() string { return "slack-stats" }
func (s *StatsState) Initialize(m *models.BotModel) error {
s.model = m
return nil
}
func (s *StatsState) ProcessMessage(m models.BotMessage) bool {
fmt.Printf("Stats: Processing Message: %s\n", m)
if m.GetSource() == models.MsgSrcSlack {
admin := s.model.GetSlackAdminDMId()
if m.Target == admin {
return s.ProcessAdminDirectMessage(m)
}
}
return false
}
func (s *StatsState) ProcessRTMEvent(msg *slack.RTMEvent) bool {
return false
}
func (s *StatsState) Run() {}
func (s *StatsState) Exit() {}
/* Other Functions */
func (s *StatsState) ProcessAdminDirectMessage(m models.BotMessage) bool {
msgPts := strings.Fields(m.Text)
if len(msgPts) < 2 || msgPts[0] != "!stats" {
return false
}
switch msgPts[1] {
case "lag", "latency":
s.DoLatencyCommand()
return true
}
return false
}
func (s *StatsState) DoLatencyCommand() {
if s.model.GetSlackLatency() == 0 {
s.SendSlackMessage("Latency is Unknown")
return
}
s.SendSlackMessage(s.model.GetSlackLatency().String())
}
func (s *StatsState) SendSlackMessage(text string) {
admin := s.model.GetSlackAdminDMId()
if admin != "" {
s.model.SendMessage(models.NewBotMessage(s.Name(), models.MsgSrcSlack, models.MsgTpMessage, admin, text))
} else {
s.model.SendMessage(models.NewBotMessage(s.Name(), models.MsgSrcApp, models.MsgTpError, models.MsgSrcApp, "No Admin ID Found"))
}
}