helperbot/app/app.go

104 lines
2.4 KiB
Go
Raw Permalink Normal View History

2023-11-30 18:40:06 +00:00
package app
import (
"fmt"
"git.bullercodeworks.com/brian/helperbot/models"
2023-12-01 15:26:25 +00:00
"git.bullercodeworks.com/brian/helperbot/plugins"
2023-11-30 18:40:06 +00:00
"github.com/spf13/viper"
)
type App struct {
2023-12-01 16:57:15 +00:00
debug bool
2023-11-30 18:40:06 +00:00
m *models.BotModel
2023-12-01 15:26:25 +00:00
plugins []models.PluginState
2023-11-30 18:40:06 +00:00
}
func NewApp(debugMode bool) (*App, error) {
a := new(App)
if debugMode {
fmt.Println("Running in Debug Mode. All messages will be sent to Admin DM")
}
a.debug = debugMode
err := a.initialize()
if err != nil {
return nil, err
}
2023-12-01 15:26:25 +00:00
go a.watchMessageChannel()
2023-11-30 18:40:06 +00:00
return a, err
}
func (a *App) initialize() error {
var err error
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
if err = viper.ReadInConfig(); err != nil {
fmt.Println("Config Read Error...")
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
fmt.Println("Config not found, creating")
// Config file not found; create it
viper.WriteConfigAs("config.yaml")
} else {
// Config file was found but another error was produced
fmt.Println("Other config err:", err)
return err
}
}
if a.m, err = models.NewBotModel(a.debug); err != nil {
return err
}
2023-12-01 16:57:15 +00:00
a.m.Running = true
2023-11-30 18:40:06 +00:00
// Load up the plugins
2023-12-01 15:26:25 +00:00
a.plugins = append(a.plugins, &plugins.StatsState{}, &plugins.AoCState{})
for _, p := range a.plugins {
p.Initialize(a.m)
2023-11-30 18:40:06 +00:00
}
a.setupMessageWatchers()
2023-12-01 15:26:25 +00:00
for _, p := range a.plugins {
p.Run()
}
2023-11-30 18:40:06 +00:00
a.m.SendSlackAdminMessage(":robot_face: Helperbot Initialized :robot_face:")
return err
}
2023-12-01 16:57:15 +00:00
func (a *App) Stop() { a.m.Running = false }
func (a *App) IsRunning() bool { return a.m.Running }
2023-11-30 18:40:06 +00:00
func (a *App) SendQuitMessage() { a.m.SendMessage(models.QuitMessage) }
func (a *App) setupMessageWatchers() {
2023-12-01 15:26:25 +00:00
a.m.AddMessageWatcher("main", func(msg models.BotMessage) bool {
2023-11-30 18:40:06 +00:00
if msg.Is(models.QuitMessage) {
// App is shutting down
2023-12-01 15:26:25 +00:00
fmt.Println("Received QuitMessage")
2023-12-01 16:57:15 +00:00
a.m.Running = false
2023-11-30 18:40:06 +00:00
} else if msg.IsError() {
// Received an Error Message
fmt.Printf("ERROR: %s\n", msg)
} else if msg.Dest == models.MsgSrcSlack {
// Sending a message to a slack channel
a.m.SendSlackChannelMessage(msg.Text, msg.Target)
return true
}
return false
})
for _, v := range a.plugins {
2023-12-01 15:26:25 +00:00
a.m.AddMessageWatcher(fmt.Sprintf("plugin-%s", v.Name()), v.ProcessMessage)
a.m.AddRTMWatcher(fmt.Sprintf("plugin-%s", v.Name()), v.ProcessRTMEvent)
2023-11-30 18:40:06 +00:00
}
}
func (a *App) watchMessageChannel() {
2023-12-01 16:57:15 +00:00
for a.m.Running {
2023-11-30 18:40:06 +00:00
a.m.ProcessMessageChannel()
}
}