-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.go
More file actions
66 lines (62 loc) · 1.45 KB
/
bot.go
File metadata and controls
66 lines (62 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package bot
import (
"context"
"errors"
"fmt"
"sync/atomic"
mapset "github.com/deckarep/golang-set/v2"
"github.com/greek-milk-bot/core/models"
"github.com/greek-milk-bot/core/utils"
)
type GreekMilkBot struct {
plugins map[string]*models.PluginInstance
route *Router[models.Packet]
once *atomic.Bool
}
func NewGreekMilkBot(plugins ...models.Plugin) (*GreekMilkBot, error) {
if len(plugins) == 0 {
return nil, errors.New("no plugins")
}
r := &GreekMilkBot{
plugins: make(map[string]*models.PluginInstance),
route: NewRouter[models.Packet](8),
once: new(atomic.Bool),
}
for i, plugin := range plugins {
id := fmt.Sprintf("%d", i)
if plugin == nil {
return nil, errors.New("nil plugin")
}
inst := &models.PluginInstance{
Plugin: plugin,
Meta: utils.NewMap[string, string](),
Tools: mapset.NewSet[string](),
Resources: utils.NewMap[string, models.ResourceProvider](),
}
r.plugins[id] = inst
}
return r, nil
}
func (r *GreekMilkBot) Run(ctx context.Context) error {
if r.once.Swap(true) {
return errors.New("plugin already running")
}
for id, plugin := range r.plugins {
if err := func() error {
bootCtx, cancel := context.WithCancel(ctx)
defer cancel()
plugin.Bus = models.PluginBus{
Context: bootCtx,
ID: id,
}
if err := plugin.Boot(plugin.Bus); err != nil {
return err
}
return nil
}(); err != nil {
return err
}
}
r.route.RunContext(ctx)
return nil
}