I believe it's all working

This commit is contained in:
2023-12-01 09:26:25 -06:00
parent de126011c4
commit ff58d1ddf4
18 changed files with 137 additions and 306 deletions

View File

@@ -2,9 +2,9 @@ package app
import (
"fmt"
"os"
"git.bullercodeworks.com/brian/helperbot/models"
"git.bullercodeworks.com/brian/helperbot/plugins"
"github.com/spf13/viper"
)
@@ -14,7 +14,7 @@ type App struct {
m *models.BotModel
plugins []HelperPlugin
plugins []models.PluginState
}
func NewApp(debugMode bool) (*App, error) {
@@ -29,6 +29,7 @@ func NewApp(debugMode bool) (*App, error) {
return nil, err
}
a.Running = true
go a.watchMessageChannel()
return a, err
}
@@ -55,14 +56,14 @@ func (a *App) initialize() error {
}
// Load up the plugins
pluginDir := a.m.GetPluginDir()
a.loadPluginsFromDirectory(pluginDir)
if err != nil {
fmt.Println("Error loading plugins")
fmt.Println(err.Error())
os.Exit(1)
a.plugins = append(a.plugins, &plugins.StatsState{}, &plugins.AoCState{})
for _, p := range a.plugins {
p.Initialize(a.m)
}
a.setupMessageWatchers()
for _, p := range a.plugins {
p.Run()
}
a.m.SendSlackAdminMessage(":robot_face: Helperbot Initialized :robot_face:")
return err
@@ -71,9 +72,10 @@ func (a *App) initialize() error {
func (a *App) SendQuitMessage() { a.m.SendMessage(models.QuitMessage) }
func (a *App) setupMessageWatchers() {
a.m.AddMessageWatcher(func(msg models.BotMessage) bool {
a.m.AddMessageWatcher("main", func(msg models.BotMessage) bool {
if msg.Is(models.QuitMessage) {
// App is shutting down
fmt.Println("Received QuitMessage")
a.Running = false
} else if msg.IsError() {
// Received an Error Message
@@ -87,8 +89,9 @@ func (a *App) setupMessageWatchers() {
})
for _, v := range a.plugins {
a.m.AddMessageWatcher(v.State.ProcessMessage)
a.m.AddRTMWatcher(v.State.ProcessRTMEvent)
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)
}
}

View File

@@ -1,61 +0,0 @@
package app
import (
"fmt"
"io/ioutil"
"os"
"plugin"
"strings"
"git.bullercodeworks.com/brian/helperbot/models"
)
type HelperPlugin struct {
p *plugin.Plugin
State models.PluginState
}
func (a *App) loadPluginsFromDirectory(dir string) error {
fmt.Println("Loading Plugins (", dir, ")")
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println("Error loading plugins")
fmt.Println(err.Error())
os.Exit(1)
}
for _, f := range files {
if !strings.HasSuffix(f.Name(), ".so") {
fmt.Printf("Skipping file (%s)\n", f.Name())
continue
}
p, err := plugin.Open(dir + f.Name())
if err != nil {
fmt.Println(fmt.Sprintf("Error loading plugin (%s)\n", f.Name()))
fmt.Println(err.Error())
os.Exit(1)
}
hp, err := NewHelperPlugin(p)
if err != nil {
fmt.Println(fmt.Sprintf("Error loading plugin (%s)\n", f.Name()))
fmt.Println(err.Error())
os.Exit(1)
}
hp.State.Initialize(a.m)
hp.State.Run()
fmt.Printf("Plugin Loaded (%s)\n", f.Name())
a.plugins = append(a.plugins, *hp)
}
return nil
}
func NewHelperPlugin(p *plugin.Plugin) (*HelperPlugin, error) {
h := &HelperPlugin{p: p}
// Parse the plugin's state
pluginStateSymbol, err := p.Lookup("State")
if err != nil {
return nil, err
}
h.State = pluginStateSymbol.(models.PluginState)
return h, nil
}