Helperbot V2
This commit is contained in:
3
plugins_src/plugin_stats/go.mod
Normal file
3
plugins_src/plugin_stats/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module git.bullercodeworks.com/brian/helperbot/plugins_src/plugin_stats
|
||||
|
||||
go 1.20
|
1
plugins_src/plugin_stats/go.work
Normal file
1
plugins_src/plugin_stats/go.work
Normal file
@@ -0,0 +1 @@
|
||||
go 1.20
|
80
plugins_src/plugin_stats/plugin_stats.go
Normal file
80
plugins_src/plugin_stats/plugin_stats.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"C"
|
||||
)
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.bullercodeworks.com/brian/helperbot/models"
|
||||
"github.com/slack-go/slack"
|
||||
)
|
||||
|
||||
type StatsState struct {
|
||||
model models.Model
|
||||
lag time.Duration
|
||||
}
|
||||
|
||||
var State StatsState
|
||||
|
||||
/* Plugin Interface Functions */
|
||||
func (s *StatsState) Name() string { return "slack-stats" }
|
||||
func (s *StatsState) Initialize(m models.Model) 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 {
|
||||
switch ev := msg.Data.(type) {
|
||||
case *slack.LatencyReport:
|
||||
s.lag = ev.Value
|
||||
}
|
||||
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.lag == 0 {
|
||||
s.SendSlackMessage("Unknown")
|
||||
return
|
||||
}
|
||||
s.SendSlackMessage(s.lag.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"))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user