-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.go
128 lines (103 loc) · 1.99 KB
/
plugins.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package plugins
import (
"strings"
"fmt"
"github.com/naryn/maria/models/tts"
"time"
)
const DEFAULT_PLUGIN = "plugins_robot"
const CMD_STOP = "STOP"
const CMD_PAUSE = "PAUSE"
const CMD_RESET = "RESET"
var Plugins map[string]Need
var PluginPause bool
var PluginStop bool
var PluginThread string
func init() {
fmt.Println("init plugin")
PluginPause = false
PluginThread = ""
//Plugins = make(map[string]Need)
}
type Need interface {
Flag() bool
Active() bool
Keyword() []string
Run(msg string)
Control(msg string) string
Say(msg string)
}
func RunBefore(msg string) {
}
func RunAfter(msg string) {
}
func threadRunPlugin(plugin Need, msg string) {
RunBefore(msg)
plugin.Run(msg)
RunAfter(msg)
}
func Say(msg string) {
tts.Say(msg)
}
func PluginControl() bool {
for PluginPause {
time.Sleep(0.8 * time.Second);
}
return !PluginStop
}
func SendCmd(cmd string) {
switch cmd {
case CMD_STOP:
PluginStop = true
PluginPause = false
case CMD_PAUSE:
PluginPause = true
case CMD_RESET:
PluginPause = false
PluginStop = false
}
time.Sleep(1.3 * time.Second);
}
func Each(str string) {
matching := false
for key, plugin := range Plugins {
for _, w := range plugin.Keyword() {
if strings.Contains(str, w) && plugin.Flag() {
/* 查看插件是否是启用状态 */
matching = true
if PluginPause {
SendCmd(plugin.Control(str))
} else if plugin.Active() {
if key != PluginThread && PluginThread != "" {
SendCmd(CMD_STOP)
PluginThread = ""
}
if PluginThread == "" {
PluginThread = key
SendCmd(CMD_RESET)
go threadRunPlugin(plugin, str)
return
} else {
//todo
}
}
}
}
}
if matching == false {
SendCmd(CMD_STOP)
PluginThread = DEFAULT_PLUGIN
go threadRunPlugin(Plugins[DEFAULT_PLUGIN], str)
}
}
func Regist(name string, plugin Need) {
if Plugins == nil {
Plugins = make(map[string]Need)
}
Plugins[name] = plugin
}
func chk(err error) {
if err != nil {
panic(err)
}
}