2023-12-01 15:26:25 +00:00
|
|
|
package plugins
|
2023-11-30 18:40:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.bullercodeworks.com/brian/helperbot/models"
|
|
|
|
"github.com/slack-go/slack"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StatsState struct {
|
2023-12-01 15:26:25 +00:00
|
|
|
model *models.BotModel
|
2023-11-30 18:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Plugin Interface Functions */
|
|
|
|
func (s *StatsState) Name() string { return "slack-stats" }
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *StatsState) Initialize(m *models.BotModel) error {
|
2023-11-30 18:40:06 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-12-01 15:26:25 +00:00
|
|
|
func (s *StatsState) ProcessRTMEvent(msg *slack.RTMEvent) bool {
|
2023-11-30 18:40:06 +00:00
|
|
|
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() {
|
2023-12-01 15:26:25 +00:00
|
|
|
if s.model.GetSlackLatency() == 0 {
|
|
|
|
s.SendSlackMessage("Latency is Unknown")
|
2023-11-30 18:40:06 +00:00
|
|
|
return
|
|
|
|
}
|
2023-12-01 15:26:25 +00:00
|
|
|
s.SendSlackMessage(s.model.GetSlackLatency().String())
|
2023-11-30 18:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"))
|
|
|
|
}
|
|
|
|
}
|