55
66from arclet .entari import Session , command , keeping , metadata , plugin
77from arclet .entari .command .provider import AlconnaSuppiler
8- from arclet .entari .event .plugin import PluginLoadedSuccess
8+ from arclet .entari .event .lifespan import Ready
99from arclet .entari .filter import admins , superusers
1010from arclet .entari .localdata import local_data
1111from arclet .entari .plugin import PluginRole , plugin_service
1616 author = [{"name" : "RF-Tar-Railt" , "email" : "rf_tar_railt@qq.com" }],
1717 description = "提供装饰器和工具函数以便于用户控制插件的可用性,比如在负载均衡,特定群下禁用插件等" ,
1818 config = None ,
19+ readme = """
20+ # 插件控制器
21+
22+ 该插件提供了一个简单的命令行接口来管理其他插件的启用状态。用户可以通过该插件列出所有已安装的功能插件,并在特定频道/群组下禁用或启用它们。
23+
24+ ## 使用
25+
26+ **plugin 指令**
27+ - list | 列出 : 列出所有已安装的功能插件
28+ - disable | ban | 禁用 : 禁用插件
29+ - 参数: 插件名或插件ID, 可传入多个, 以空格分隔
30+ - 选项: --global | -g | 全局 : 插件是否全局禁用
31+ - enable | 启用 : 启用插件
32+ - 参数: 插件名或插件ID, 可传入多个, 以空格分隔
33+ - 选项: --global | -g | 全局 : 插件是否解除全局禁用
34+ - clear | 清空 : 清空所有被禁用的插件
35+
36+ **function 指令**
37+ - list | 列出 : 列出所有已安装的功能插件下的功能
38+ - 参数: 插件名或插件ID, 用来列出特定插件的功能, 可选
39+ - disable | ban | 禁用 : 禁用功能
40+ - 参数: 功能名, 可传入多个, 以空格分隔。功能名格式为 `插件名:功能名` 或 `插件ID:功能名`,
41+ 如果不指定插件,则会在所有插件中查找该功能
42+ - enable | 启用 : 启用功能
43+ - 参数: 功能名, 可传入多个, 以空格分隔。功能名格式为 `插件名:功能名` 或 `插件ID:功能名`,
44+ 如果不指定插件,则会在所有插件中查找该功能
45+ - clear | 清空 : 清空所有被禁用的功能
46+ """ ,
1947)
2048
2149channel_plugin_disables_file = local_data .get_data_file ("control" , "plugin_disables.json" )
@@ -37,6 +65,7 @@ def _save_data(data: dict):
3765 "plugin_disables" , obj_factory = _load_data , dispose = _save_data
3866)
3967plugin_functions : dict [str , dict [str , str ]] = keeping ("plugin_functions" , obj_factory = dict )
68+ sub_functino_map : dict [str , dict [str , str ]] = keeping ("sub_functino_map" , obj_factory = dict )
4069
4170plugin_control = Alconna (
4271 "plugin" ,
@@ -64,7 +93,7 @@ def _save_data(data: dict):
6493)
6594function_control = Alconna (
6695 "function" ,
67- Subcommand ("list" , alias = ["列出" ]),
96+ Subcommand ("list" , Args [ "name?" , str , Field ( completion = lambda : "试试用 help" )], alias = ["列出" ]),
6897 Subcommand (
6998 "disable" ,
7099 Args ["names" , MultiVar (str ), Field (completion = lambda : "试试用 help" )],
@@ -81,7 +110,7 @@ def _save_data(data: dict):
81110 meta = CommandMeta (
82111 "管理特定频道/群组下插件中功能的启用状态" ,
83112 usage = "可传入多个功能名, 以空格分隔" ,
84- example = "$功能 列出\n $功能 禁用 Echo. echo" ,
113+ example = "$功能 列出\n $功能 禁用 Echo: echo" ,
85114 ),
86115)
87116
@@ -93,31 +122,10 @@ def _save_data(data: dict):
93122superuser_check = superusers ().check
94123
95124
96- @plug .dispatch (PluginLoadedSuccess )
97- async def hook (event : PluginLoadedSuccess ):
98- if event .plugin_id == plug .id :
99- return
100-
101- target = plugin_service .plugins [event .plugin_id ]
102- plg_id = target .id
103- while plg_id in plugin_service ._subplugined :
104- plg_id = plugin_service ._subplugined [plg_id ]
105-
106- subscribers = plugin .get_plugin_subscribers (target )
107- functions = {}
108- sub_functino_map = {}
109- for sub in subscribers :
110- try :
111- sup = sub .get_propagator (AlconnaSuppiler )
112- sub_functino_map [sub .id ] = sup .cmd .command
113- functions .setdefault (sup .cmd .command , sup .cmd .meta .description )
114- except ValueError :
115- sub_functino_map [sub .id ] = sub .__name__ # sub.label
116- functions [sub .__name__ ] = sub .__doc__ or "" # sub.label
125+ @plug .dispatch (Ready )
126+ async def hook ():
117127
118- plugin_functions [plg_id ] = functions
119-
120- def _check_disable (sub_id : str ):
128+ def _check_disable (sub_id : str , plg_id : str ):
121129
122130 async def _ (session : Session | None = None ):
123131 if not session :
@@ -130,15 +138,33 @@ async def _(session: Session | None = None):
130138 disables = plugin_disables [ch_id ].get (plg_id , {})
131139 if disables .get ("$plugin" , False ):
132140 return STOP
133- if disables .get (sub_functino_map [sub_id ], False ):
134- print (sub_id , sub_functino_map [sub_id ])
141+ if disables .get (sub_functino_map [plg_id ][sub_id ], False ):
135142 return STOP
136143
137144 return _
138145
139146 def collect ():
140- for sub in subscribers :
141- yield sub .propagate (_check_disable (sub .id ), prepend = True )
147+ for plg in plugin .get_plugins (subplugged = True ):
148+ if plg .id == plug .id or (not plg .metadata or plg .metadata .role is not PluginRole .NORMAL ):
149+ continue
150+
151+ plg_id = plg .id
152+ while plg_id in plugin_service ._subplugined :
153+ plg_id = plugin_service ._subplugined [plg_id ]
154+
155+ subscribers = plugin .get_plugin_subscribers (plg )
156+ functions = {}
157+ sub_functino_map [plg_id ] = {}
158+ for sub in subscribers :
159+ try :
160+ sup = sub .get_propagator (AlconnaSuppiler )
161+ sub_functino_map [plg_id ][sub .id ] = sup .cmd .command
162+ functions .setdefault (sup .cmd .command , sup .cmd .meta .description )
163+ except ValueError :
164+ sub_functino_map [plg_id ][sub .id ] = sub .label
165+ functions [sub .label ] = sub .__doc__ or ""
166+ yield sub .propagate (_check_disable (sub .id , plg_id ), prepend = True )
167+ plugin_functions [plg_id ] = functions
142168
143169 plug .effect (collect , "control_inject" )
144170
@@ -162,15 +188,11 @@ async def _(session: Session):
162188 meta = plg .metadata
163189 if meta and meta .role is not PluginRole .NORMAL :
164190 continue
165- line = f"- { meta .name } : { meta .description or '无描述' } " if meta else f"- { plg .id } "
166- stat = "✅ 全局启用 " if plg .is_available else "❌ 全局禁用 "
191+ line = f"{ plg . id } : { meta .name } , { meta .description or '无描述' } " if meta else plg .id
192+ stat = "✅" if plg .is_available else "❌"
167193 if session .event .channel and session .event .channel .id in plugin_disables :
168- stat = (
169- "❌ 禁用"
170- if plugin_disables [session .event .channel .id ].get (plg .id , {}).get ("$plugin" , False )
171- else "✅ 启用"
172- )
173- res += f"{ line } { stat } \n "
194+ stat = "❌" if plugin_disables [session .event .channel .id ].get (plg .id , {}).get ("$plugin" , False ) else "✅"
195+ res += f" { stat } { line } \n "
174196 return res
175197
176198
@@ -260,26 +282,45 @@ async def _():
260282
261283
262284@function_ctl_disp .assign ("list" )
263- async def _ (session : Session ):
285+ async def _ (session : Session , name : str | None = None ):
264286 plgs = plugin .get_plugins ()
287+ plgs = [plg for plg in plgs if not plg .metadata or plg .metadata .role is PluginRole .NORMAL ]
288+ name_to_id = {plg .metadata .name : plg .id for plg in plgs if plg .metadata }
289+ ids = {plg .id for plg in plgs }
290+ if name :
291+ if name not in name_to_id and name not in ids :
292+ return f"未找到插件:{ name } "
293+ plg_id = name_to_id .get (name , name )
294+ functions = plugin_functions .get (plg_id , {})
295+ res = f"插件 [{ name } ] 的功能列表:\n "
296+ for func_name , func_desc in functions .items ():
297+ stat = "✅"
298+ if session .event .channel and session .event .channel .id in plugin_disables :
299+ disables = plugin_disables [session .event .channel .id ].get (plg_id , {})
300+ if disables .get ("$plugin" , False ):
301+ stat = "❌"
302+ elif disables .get (func_name , False ):
303+ stat = "❌"
304+ else :
305+ stat = "✅"
306+ res += f" { stat } { func_name } - { func_desc or '无描述' } \n "
307+ return res
265308 res = "已安装的功能插件及其功能:\n "
266309 for plg in plgs :
267310 meta = plg .metadata
268- if meta and meta .role is not PluginRole .NORMAL :
269- continue
270311 res += f"- { meta .name } : { meta .description or '无描述' } \n " if meta else f"- { plg .id } \n "
271312 functions = plugin_functions .get (plg .id , {})
313+ stat = "✅" if plg .is_available else "❌"
272314 for func_name , func_desc in functions .items ():
273- stat = "✅ 启用" if plg .is_available else "❌ 插件禁用"
274315 if session .event .channel and session .event .channel .id in plugin_disables :
275316 disables = plugin_disables [session .event .channel .id ].get (plg .id , {})
276317 if disables .get ("$plugin" , False ):
277- stat = "❌ 插件禁用 "
318+ stat = "❌"
278319 elif disables .get (func_name , False ):
279- stat = "❌ 禁用 "
320+ stat = "❌"
280321 else :
281- stat = "✅ 启用 "
282- res += f" - { func_name } : { func_desc or '无描述' } { stat } \n "
322+ stat = "✅"
323+ res += f" { stat } { func_name } - { func_desc or '无描述' } \n "
283324 return res
284325
285326
@@ -294,7 +335,7 @@ async def _(
294335 name_to_id = {plg .metadata .name : plg .id for plg in plgs if plg .metadata }
295336 ids = {plg .id for plg in plgs }
296337 for name in names :
297- target , func_name = name .split (". " , 1 ) if "." in name else (None , name )
338+ target , func_name = name .split (": " , 1 ) if "." in name else (None , name )
298339 if target :
299340 if target not in name_to_id and target not in ids :
300341 await session .send_message (f"未找到插件:{ target } " )
0 commit comments