@@ -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
1920type 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+
81151func 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+
101177func 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
0 commit comments