Skip to content

Commit d1e58f6

Browse files
committed
feat(plugins): admin模块编写完毕
1 parent dc220f1 commit d1e58f6

File tree

3 files changed

+101
-11
lines changed

3 files changed

+101
-11
lines changed

rikkabot/plugins/admin/admin.go

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/Clov614/rikka-bot-wechat/rikkabot/plugins/matcher"
1515
"github.com/Clov614/rikka-bot-wechat/rikkabot/processor/cache"
1616
"github.com/Clov614/rikka-bot-wechat/rikkabot/utils/msgutil"
17+
"strings"
1718
)
1819

1920
type Admin struct {
@@ -53,9 +54,23 @@ func init() {
5354
delOp := delOpFunc(isSelfJudge, adminJudge, admin)
5455
// op list
5556
opList := opListFunc(isSelfJudge, adminJudge, admin)
57+
// op -p 插件管理领域
58+
_, opPBase := opPluginFunc(isSelfJudge, adminJudge) // op -p
59+
// op -p list 插件列表
60+
PL := PLFunc()
61+
// op -p on 启用某插件
62+
onP := onPFunc()
63+
// op -p off
64+
offP := offPFunc()
65+
66+
opPBase.AsChild(PL) // 显示插件列表
67+
opPBase.AsChild(onP) // 启用某插件
68+
opPBase.AsChild(offP) // 禁用插件
69+
5670
// 子action
5771
opMulAction.AsChild(delOp)
5872
opMulAction.AsChild(opList)
73+
opMulAction.AsChild(opPBase) // 插件管理父行动
5974
opMulAction.AsChild(help.AsActionFunc(func(ctx context.Context, recvMsg *message.Message) (reply message.Message, err error) {
6075
recvMsg.Content = admin.help()
6176
return *recvMsg, nil
@@ -78,6 +93,61 @@ func init() {
7893
plugins.GetAutoRegister().RegisterPlugin(admin) // 注册插件
7994
}
8095

96+
func PLFunc() *plugins.ActionHandler {
97+
PLM := matcher.Default().And().N(matcher.PrefixMatcher{Prefix: "list", IsCut: true, IsCaseSensitive: false})
98+
PL := plugins.DefaultActionHandler("op -p list", true).AsMatcher(PLM).AsActionFunc(func(ctx context.Context, recvMsg *message.Message) (reply message.Message, err error) {
99+
var buf bytes.Buffer
100+
buf.WriteString("插件列表\n")
101+
register := plugins.GetAutoRegister()
102+
for i, p := range register.Plugins() {
103+
buf.WriteString(fmt.Sprintf("【%d 插件名称: %s \n状态: %v\n等级: 第%d级 】", i+1, (*p).GetName(), (*p).GetPluginOpt().Enable, (*p).GetPluginOpt().Level))
104+
}
105+
recvMsg.Content = buf.String()
106+
return *recvMsg, nil
107+
})
108+
return PL
109+
}
110+
111+
func onPFunc() *plugins.ActionHandler {
112+
onPM := matcher.Default().And().N(matcher.PrefixMatcher{Prefix: "on", IsCut: true, IsCaseSensitive: false})
113+
onP := plugins.DefaultActionHandler("op -p on", true).AsMatcher(onPM).AsActionFunc(func(ctx context.Context, recvMsg *message.Message) (reply message.Message, err error) {
114+
name := strings.TrimSpace(recvMsg.Content)
115+
ar := plugins.GetAutoRegister()
116+
if name == "admin" {
117+
recvMsg.Content = "管理员插件禁止操作"
118+
return *recvMsg, nil
119+
}
120+
b := ar.EnableByName(name) // 启用某插件
121+
if b {
122+
recvMsg.Content = fmt.Sprintf("启用 %s 成功", name)
123+
} else {
124+
recvMsg.Content = fmt.Sprintf("启用 %s 失败", name)
125+
}
126+
return *recvMsg, nil
127+
})
128+
return onP
129+
}
130+
131+
func offPFunc() *plugins.ActionHandler {
132+
offPM := matcher.Default().And().N(matcher.PrefixMatcher{Prefix: "off", IsCut: true, IsCaseSensitive: false})
133+
offP := plugins.DefaultActionHandler("op -p on", true).AsMatcher(offPM).AsActionFunc(func(ctx context.Context, recvMsg *message.Message) (reply message.Message, err error) {
134+
name := strings.TrimSpace(recvMsg.Content)
135+
ar := plugins.GetAutoRegister()
136+
if name == "admin" {
137+
recvMsg.Content = "管理员插件禁止操作"
138+
return *recvMsg, nil
139+
}
140+
b := ar.DisableByName(name) // 启用某插件
141+
if b {
142+
recvMsg.Content = fmt.Sprintf("禁用 %s 成功", name)
143+
} else {
144+
recvMsg.Content = fmt.Sprintf("禁用 %s 失败", name)
145+
}
146+
return *recvMsg, nil
147+
})
148+
return offP
149+
}
150+
81151
func helpFunc(isSelfJudge matcher.BaseMatcher, adminJudge matcher.Custom) *plugins.ActionHandler {
82152
helpM := matcher.Default().And().N(matcher.PrefixMatcher{Prefix: "help", IsCaseSensitive: false, IsCut: true}, matcher.Default().Or().N(isSelfJudge, adminJudge))
83153
help := plugins.DefaultActionHandler("op help", true).AsMatcher(helpM)
@@ -98,6 +168,12 @@ func delOpFunc(isSelfJudge matcher.BaseMatcher, adminJudge matcher.Custom, admin
98168
return delOp
99169
}
100170

171+
func opPluginFunc(isSelfJudge matcher.BaseMatcher, adminJudge matcher.Custom) (matcher.DefaultMatcher, *plugins.ActionHandler) {
172+
pluginM := matcher.Default().And().N(matcher.PrefixMatcher{Prefix: "-p", IsCaseSensitive: false, IsCut: true}, matcher.Default().Or().N(isSelfJudge, adminJudge))
173+
pluginOp := plugins.DefaultActionHandler("op -p", true).AsMatcher(pluginM)
174+
return pluginM, pluginOp
175+
}
176+
101177
func opListFunc(isSelfJudge matcher.BaseMatcher, adminJudge matcher.Custom, admin *Admin) *plugins.ActionHandler {
102178
opListM := matcher.Default().And().N(matcher.PrefixMatcher{Prefix: "list", IsCaseSensitive: false, IsCut: true}, matcher.Default().Or().N(isSelfJudge, adminJudge))
103179
opList := plugins.DefaultActionHandler("op list", true).AsMatcher(opListM)
@@ -134,17 +210,17 @@ func (a *Admin) help() string {
134210
buf.WriteString("启用模块 op -p on <plugin name>\n")
135211
buf.WriteString("禁用模块 op -p off <plugin name>\n")
136212

137-
buf.WriteString("添加群组白名单(在群聊中使用) op -w \n")
138-
buf.WriteString("移除群组白名单(在群聊中使用) op -w off \n")
139-
//buf.WriteString("显示群组白名单 op -w list\n")
140-
141-
buf.WriteString("添加群组黑名单(在群聊中使用) op -b \n")
142-
buf.WriteString("移除群组黑名单(在群聊中使用) op -b off\n")
143-
//buf.WriteString("显示群组黑名单 op -b list\n")
144-
145-
buf.WriteString("添加用户黑名单 op -u kick <@someone>\n")
146-
buf.WriteString("移除用户黑名单 op -u save <@someone>\n")
147-
buf.WriteString("显示用户黑名单 op -u \n")
213+
//buf.WriteString("添加群组白名单(在群聊中使用) op -w \n")
214+
//buf.WriteString("移除群组白名单(在群聊中使用) op -w off \n")
215+
////buf.WriteString("显示群组白名单 op -w list\n")
216+
//
217+
//buf.WriteString("添加群组黑名单(在群聊中使用) op -b \n")
218+
//buf.WriteString("移除群组黑名单(在群聊中使用) op -b off\n")
219+
////buf.WriteString("显示群组黑名单 op -b list\n")
220+
//
221+
//buf.WriteString("添加用户黑名单 op -u kick <@someone>\n")
222+
//buf.WriteString("移除用户黑名单 op -u save <@someone>\n")
223+
//buf.WriteString("显示用户黑名单 op -u \n")
148224
return buf.String()
149225
}
150226

rikkabot/plugins/admin/admin_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func TestAdminMatcher_Match(t *testing.T) {
3535
text: "op -d @123456 ",
3636
want: true,
3737
},
38+
{
39+
text: "op -p list",
40+
want: true,
41+
},
3842
}
3943
sendChan := make(chan *message.Message, len(tests))
4044
defer close(sendChan)

rikkabot/plugins/plugin.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@ const (
295295
VeryLowLevel
296296
)
297297

298+
var Level2Str = map[uint8]string{
299+
uint8(VeryHighLevel): "VeryHighLevel",
300+
HighLevel: "HighLevel",
301+
UpperLevel: "UpperLevel",
302+
DownLevel: "DownLevel",
303+
MediumLevel: "MediumLevel",
304+
LowLevel: "LowLevel",
305+
VeryLowLevel: "VeryLowLevel",
306+
}
307+
298308
const LevelSize = 7
299309

300310
type PluginOpt struct {

0 commit comments

Comments
 (0)