This commit is contained in:
parent
9c78ea2d48
commit
7c684af8c3
79 changed files with 3594 additions and 3257 deletions
82
internal/plugin/plugin.go
Normal file
82
internal/plugin/plugin.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
package plugin
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"git.nakama.town/fmartingr/butterrobot/internal/model"
|
||||
)
|
||||
|
||||
var (
|
||||
// plugins holds all registered plugins
|
||||
plugins = make(map[string]model.Plugin)
|
||||
|
||||
// pluginsMu protects the plugins map
|
||||
pluginsMu sync.RWMutex
|
||||
)
|
||||
|
||||
// Register registers a plugin with the given ID
|
||||
func Register(plugin model.Plugin) {
|
||||
pluginsMu.Lock()
|
||||
defer pluginsMu.Unlock()
|
||||
plugins[plugin.GetID()] = plugin
|
||||
}
|
||||
|
||||
// Get returns a plugin by ID
|
||||
func Get(id string) (model.Plugin, error) {
|
||||
pluginsMu.RLock()
|
||||
defer pluginsMu.RUnlock()
|
||||
|
||||
plugin, exists := plugins[id]
|
||||
if !exists {
|
||||
return nil, model.ErrPluginNotFound
|
||||
}
|
||||
|
||||
return plugin, nil
|
||||
}
|
||||
|
||||
// GetAvailablePlugins returns all registered plugins
|
||||
func GetAvailablePlugins() map[string]model.Plugin {
|
||||
pluginsMu.RLock()
|
||||
defer pluginsMu.RUnlock()
|
||||
|
||||
// Create a copy to avoid race conditions
|
||||
result := make(map[string]model.Plugin, len(plugins))
|
||||
for id, plugin := range plugins {
|
||||
result[id] = plugin
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// BasePlugin provides a common base for plugins
|
||||
type BasePlugin struct {
|
||||
ID string
|
||||
Name string
|
||||
Help string
|
||||
ConfigRequired bool
|
||||
}
|
||||
|
||||
// GetID returns the plugin ID
|
||||
func (p *BasePlugin) GetID() string {
|
||||
return p.ID
|
||||
}
|
||||
|
||||
// GetName returns the plugin name
|
||||
func (p *BasePlugin) GetName() string {
|
||||
return p.Name
|
||||
}
|
||||
|
||||
// GetHelp returns the plugin help text
|
||||
func (p *BasePlugin) GetHelp() string {
|
||||
return p.Help
|
||||
}
|
||||
|
||||
// RequiresConfig indicates if the plugin requires configuration
|
||||
func (p *BasePlugin) RequiresConfig() bool {
|
||||
return p.ConfigRequired
|
||||
}
|
||||
|
||||
// OnMessage is the default implementation that does nothing
|
||||
func (p *BasePlugin) OnMessage(msg *model.Message, config map[string]interface{}) []*model.Message {
|
||||
return nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue