2024-12-16 00:52:43 +03:00
|
|
|
package laniakea
|
|
|
|
|
|
|
|
type CommandExecutor func(ctx *MsgContext)
|
|
|
|
|
|
|
|
type PluginBuilder struct {
|
2024-12-17 09:23:53 +03:00
|
|
|
name string
|
|
|
|
commands map[string]*CommandExecutor
|
|
|
|
payloads map[string]*CommandExecutor
|
|
|
|
updateListener *CommandExecutor
|
2024-12-16 00:52:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Plugin struct {
|
2024-12-17 09:23:53 +03:00
|
|
|
Name string
|
|
|
|
Commands map[string]*CommandExecutor
|
|
|
|
Payloads map[string]*CommandExecutor
|
|
|
|
UpdateListener *CommandExecutor
|
2024-12-16 00:52:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPlugin(name string) *PluginBuilder {
|
|
|
|
return &PluginBuilder{
|
|
|
|
name: name,
|
|
|
|
commands: make(map[string]*CommandExecutor),
|
2024-12-17 09:23:53 +03:00
|
|
|
payloads: make(map[string]*CommandExecutor),
|
2024-12-16 00:52:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 09:23:53 +03:00
|
|
|
func (p *PluginBuilder) Command(f CommandExecutor, cmd ...string) *PluginBuilder {
|
|
|
|
for _, c := range cmd {
|
|
|
|
p.commands[c] = &f
|
|
|
|
}
|
2024-12-16 00:52:43 +03:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2024-12-17 09:23:53 +03:00
|
|
|
func (p *PluginBuilder) Payload(f CommandExecutor, payloads ...string) *PluginBuilder {
|
|
|
|
for _, payload := range payloads {
|
|
|
|
p.payloads[payload] = &f
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PluginBuilder) UpdateListener(listener CommandExecutor) *PluginBuilder {
|
|
|
|
p.updateListener = &listener
|
2024-12-16 00:52:43 +03:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PluginBuilder) Build() *Plugin {
|
|
|
|
if len(p.commands) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
plugin := &Plugin{
|
2024-12-17 09:23:53 +03:00
|
|
|
Name: p.name,
|
|
|
|
Commands: p.commands,
|
|
|
|
Payloads: p.payloads,
|
|
|
|
UpdateListener: p.updateListener,
|
2024-12-16 00:52:43 +03:00
|
|
|
}
|
|
|
|
return plugin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Plugin) Execute(cmd string, ctx *MsgContext) {
|
|
|
|
(*p.Commands[cmd])(ctx)
|
|
|
|
}
|
2024-12-17 09:23:53 +03:00
|
|
|
|
|
|
|
func (p *Plugin) ExecutePayload(payload string, ctx *MsgContext) {
|
|
|
|
(*p.Payloads[payload])(ctx)
|
|
|
|
}
|