/
base.go
77 lines (70 loc) · 1.69 KB
/
base.go
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
67
68
69
70
71
72
73
74
75
76
77
package bot
import (
"context"
"fmt"
"github.com/gorilla/websocket"
"github.com/klzwii/mirai-go/function"
"github.com/klzwii/mirai-go/record"
"github.com/klzwii/mirai-go/sender"
"github.com/mitchellh/mapstructure"
"github.com/sirupsen/logrus"
"net/url"
)
var logger = logrus.WithField("core", "bot")
type Bot struct {
sender sender.Sender
conn function.Conn
plugins []Plugin
conf map[string]interface{}
}
type config struct {
Port string
VerifyKey string `mapstructure:"verify_key"`
QQ string
}
func (b *Bot) Start(ctx context.Context) {
initiatePlugins(b.conf, b.plugins, b.sender)
ch := make(chan record.Base, 100)
go b.conn.StartReading(ctx, ch)
logger.Info("bot初始化完成")
for {
select {
case curRecord := <-ch:
go triggerPlugins(curRecord, b)
case <-ctx.Done():
logger.Info("bot退出")
return
}
}
}
func (b *Bot) RegisterPlugin(plugin Plugin) {
b.plugins = append(b.plugins, plugin)
}
func GetBot(rawConfig map[string]any) (*Bot, error) {
var (
conf = &config{}
err error
decoder *mapstructure.Decoder
)
if decoder, err = mapstructure.NewDecoder(&mapstructure.DecoderConfig{ErrorUnset: true, WeaklyTypedInput: true, Result: conf}); err != nil {
return nil, err
}
if err = decoder.Decode(rawConfig["bot"]); err != nil {
return nil, err
}
u := url.URL{Scheme: "ws", Host: fmt.Sprintf("localhost:%s", conf.Port), Path: "/message"}
c, _, err := websocket.DefaultDialer.Dial(u.String(), map[string][]string{
"verifyKey": {conf.VerifyKey},
"qq": {conf.QQ},
})
if err != nil {
return nil, err
}
conn := function.GetWsConn(c)
return &Bot{
sender.GetWSSender(conn, ""),
conn,
[]Plugin{},
rawConfig,
}, nil
}