From 0fc78d4b4ce4a0411f2b9e3012ada870f6d5a5c8 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Fri, 1 Dec 2023 10:57:15 -0600 Subject: [PATCH] I think things are pretty good --- Makefile | 4 --- app/app.go | 13 ++++---- buildplugins.sh | 14 -------- clean.sh | 1 - cmd/root.go | 4 +-- models/model.go | 16 ++++----- package.sh | 6 ++-- plugins/plugin_aoc.go | 76 ++++++++++++++++++++++++++----------------- 8 files changed, 65 insertions(+), 69 deletions(-) delete mode 100755 buildplugins.sh diff --git a/Makefile b/Makefile index 3a2d0cc..ebbce33 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,8 @@ helperbot: go build -o build/helperbot *.go -plugins: - ./buildplugins.sh - package: go build -o build/helperbot *.go - ./buildplugins.sh cp helperbot.service build/ cd build && tar -zcvf helperbot.tgz * diff --git a/app/app.go b/app/app.go index d7e0778..d5b76f1 100644 --- a/app/app.go +++ b/app/app.go @@ -9,8 +9,7 @@ import ( ) type App struct { - debug bool - Running bool + debug bool m *models.BotModel @@ -28,7 +27,6 @@ func NewApp(debugMode bool) (*App, error) { if err != nil { return nil, err } - a.Running = true go a.watchMessageChannel() return a, err } @@ -54,6 +52,7 @@ func (a *App) initialize() error { if a.m, err = models.NewBotModel(a.debug); err != nil { return err } + a.m.Running = true // Load up the plugins a.plugins = append(a.plugins, &plugins.StatsState{}, &plugins.AoCState{}) @@ -69,6 +68,9 @@ func (a *App) initialize() error { return err } +func (a *App) Stop() { a.m.Running = false } +func (a *App) IsRunning() bool { return a.m.Running } + func (a *App) SendQuitMessage() { a.m.SendMessage(models.QuitMessage) } func (a *App) setupMessageWatchers() { @@ -76,7 +78,7 @@ func (a *App) setupMessageWatchers() { if msg.Is(models.QuitMessage) { // App is shutting down fmt.Println("Received QuitMessage") - a.Running = false + a.m.Running = false } else if msg.IsError() { // Received an Error Message fmt.Printf("ERROR: %s\n", msg) @@ -89,14 +91,13 @@ func (a *App) setupMessageWatchers() { }) for _, v := range a.plugins { - fmt.Println("Setting up watchers for", v.Name()) a.m.AddMessageWatcher(fmt.Sprintf("plugin-%s", v.Name()), v.ProcessMessage) a.m.AddRTMWatcher(fmt.Sprintf("plugin-%s", v.Name()), v.ProcessRTMEvent) } } func (a *App) watchMessageChannel() { - for a.Running { + for a.m.Running { a.m.ProcessMessageChannel() } } diff --git a/buildplugins.sh b/buildplugins.sh deleted file mode 100755 index adae407..0000000 --- a/buildplugins.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -mkdir -p build/plugins -cd plugins_src -for i in `ls`; do - echo "Building plugin: $i" - cd "$i" - go build -buildmode=plugin "$i.go" - if [ $? -eq 0 ]; then - mv *.so ../../build/plugins - fi - cd .. -done -cd .. diff --git a/clean.sh b/clean.sh index 5235e37..9c7ca62 100755 --- a/clean.sh +++ b/clean.sh @@ -7,4 +7,3 @@ rmE build/aoc-util rmE build/helperbot rmE build/helperbot.service rmE build/helperbot.tgz -rm build/plugins/* diff --git a/cmd/root.go b/cmd/root.go index c3b9e0f..6949826 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -36,9 +36,9 @@ var rootCmd = &cobra.Command{ // Save the changes when the app quits fmt.Println("\nFinishing up...") a.SendQuitMessage() - a.Running = false + a.Stop() }() - for a.Running { + for a.IsRunning() { time.Sleep(time.Second * 1) } fmt.Println("Done") diff --git a/models/model.go b/models/model.go index 79ac326..1a77026 100644 --- a/models/model.go +++ b/models/model.go @@ -24,6 +24,8 @@ type BotModel struct { messageWatchers map[string]func(msg BotMessage) bool rtmWatchers map[string]func(event *slack.RTMEvent) bool + + Running bool } func NewBotModel(debug bool) (*BotModel, error) { @@ -60,7 +62,9 @@ func (m *BotModel) DebugRTMWatcher(event *slack.RTMEvent) bool { } func (m *BotModel) SendMessage(msg BotMessage) { - fmt.Println("Sending:", msg) + if m.debug { + fmt.Println("Sending:", msg) + } m.messages <- msg } @@ -72,11 +76,8 @@ func (m *BotModel) AddRTMWatcher(name string, watcher func(event *slack.RTMEvent } func (m *BotModel) ProcessMessageChannel() { - fmt.Println(">> ProcessMessageChannel") msg := <-m.messages - fmt.Println(">>>> Received Message", msg) - for k, v := range m.messageWatchers { - fmt.Printf(">>>> Message Watcher [%s]\n", k) + for _, v := range m.messageWatchers { // Pass the message to the watcher if v != nil && v(msg) { // if a watcher returns true, the message was consumed @@ -85,11 +86,8 @@ func (m *BotModel) ProcessMessageChannel() { } } func (m *BotModel) ProcessRTMChannel() { - fmt.Println(">> ProcessRTMChannel") msg := <-m.otherRTMEvents - fmt.Println(">>>> Received Message", msg) - for k, v := range m.rtmWatchers { - fmt.Printf(">>>> RTM Watcher [%s]\n", k) + for _, v := range m.rtmWatchers { // Pass the event to the watcher if v(msg) { // if a watcher returns true, the message was consumed diff --git a/package.sh b/package.sh index a02737d..2668f26 100755 --- a/package.sh +++ b/package.sh @@ -1,9 +1,7 @@ #!/bin/bash rm helperbot.tgz -./buildplugins.sh cd cmd go build -o helperbot -mv plugins build/ -#tar -zcvf ../helperbot.tgz helperbot helperbot.service plugins -#cd .. +tar -zcvf ../helperbot.tgz helperbot helperbot.service plugins +cd .. diff --git a/plugins/plugin_aoc.go b/plugins/plugin_aoc.go index ef210df..644ce7d 100644 --- a/plugins/plugin_aoc.go +++ b/plugins/plugin_aoc.go @@ -21,6 +21,7 @@ type AoCState struct { boardId string sessionCookie string sessionNeedsUpdate bool + lastUpdate time.Time aoc *aoc.AoC lastYear int @@ -112,41 +113,28 @@ func (s *AoCState) runLoop() { if err != nil { return } - // Don't do anything until we've done an initial update of all earlier years - for _, yr := range s.GetListOfAoCYears() { - if yr == s.GetLatestYear() { - continue - } - if !s.sessionNeedsUpdate { - fmt.Printf("Startup: Checking if board needs update (%d)\n", yr) - s.AoCSilentBoardCheckAndUpdate(yr) - } - } - for { + for s.model.Running { _, err := s.getChannelId() // This plugin fails without a channel id if err != nil { return } - for _, yr := range s.GetListOfAoCYears() { - if !s.sessionNeedsUpdate { - if s.GetLatestYear() != s.lastYear { - // Latest year changed. Grab that board first. - s.lastYear = s.GetLatestYear() - admin := s.model.GetSlackAdminDMId() - if admin == "" { - s.SendAdminIdError() - return - } - s.SendSlackMessage(fmt.Sprintf(":christmas_tree: AoC Set latest leaderboard to %d", s.lastYear), admin) - s.AoCBoardCheckAndUpdate(s.lastYear) - time.Sleep(time.Minute) + + if !s.sessionNeedsUpdate { + if s.GetLatestYear() != s.lastYear { + // Latest year changed. Update it. + s.lastYear = s.GetLatestYear() + admin := s.model.GetSlackAdminDMId() + if admin == "" { + s.SendAdminIdError() + return } - s.AoCBoardCheckAndUpdate(yr) - time.Sleep(time.Minute) + s.SendSlackMessage(fmt.Sprintf(":christmas_tree: AoC Set latest leaderboard to %d", s.lastYear), admin) } + s.AoCBoardCheckAndUpdate(s.lastYear) + s.lastUpdate = time.Now() } - time.Sleep(time.Minute) + time.Sleep(time.Minute * 15) } s.model.SendMessage(models.NewBotMessage(s.Name(), models.MsgSrcApp, "status", "done", "")) @@ -186,6 +174,9 @@ func (s *AoCState) ProcessAdminDirectMessage(m models.BotMessage) bool { case "session": s.DoSessionCmd(m) return true + case "status": + s.DoStatusCmd(m) + return true } return false } @@ -204,7 +195,11 @@ func (s *AoCState) ProcessChannelMessage(m models.BotMessage) bool { func (s *AoCState) DoHelpCmd(m models.BotMessage) bool { txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n", - "-- WiP --", + "• help - Print this message\n", + "• ping - PONG! Check if the service is running\n", + "• top [year] - Print the top 5 users for the passed year\n", + " If no year is passed, the current year is used\n", + " Passing 'all' considers all years\n", ) s.SendSlackMessage(txt, m.Target) return true @@ -212,7 +207,13 @@ func (s *AoCState) DoHelpCmd(m models.BotMessage) bool { func (s *AoCState) DoHelpAdminCmd(m models.BotMessage) bool { txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n", - "-- WiP --", + "• help - Print this message\n", + "• ping - PONG! Check if the service is running\n", + "• top [year] - Print the top 5 users for the passed year\n", + " If no year is passed, the current year is used\n", + " Passing 'all' considers all years\n", + "• session [token] - Print or set the session cookie\n", + "• status - Print the status of the service\n", ) s.SendSlackMessage(txt, m.Target) return true @@ -250,6 +251,23 @@ func (s *AoCState) DoSessionCmd(m models.BotMessage) bool { return true } +func (s *AoCState) DoStatusCmd(m models.BotMessage) bool { + admin := s.model.GetSlackAdminDMId() + if admin == "" { + s.SendAdminIdError() + return true + } + txt := ":christmas_tree: Advent of Code Status :christmas_tree:" + txt = fmt.Sprintf( + "%s\nLast Update: %s\nNext Update In: %s", txt, + s.lastUpdate.Format(time.RFC3339), + s.lastUpdate.Add(time.Minute*15).Sub(time.Now()).Round(time.Second), + ) + + s.SendSlackMessage(txt, m.Target) + return false +} + func (s *AoCState) DoTopCmd(m models.BotMessage) bool { msgPts := strings.Fields(m.Text) var err error