55
66from arclet .entari import Session , command , keeping , metadata , plugin
77from arclet .entari .command .provider import AlconnaSuppiler
8+ from arclet .entari .event .base import SatoriEvent
89from arclet .entari .event .lifespan import Ready
910from arclet .entari .filter import admins , superusers
1011from arclet .entari .localdata import local_data
1112from arclet .entari .plugin import PluginRole , plugin_service
13+ from arclet .entari .scheduler import _ScheduleEvent
1214
1315metadata (
1416 "插件控制器" ,
3638**function 指令**
3739- list | 列出 : 列出所有已安装的功能插件下的功能
3840 - 参数: 插件名或插件ID, 用来列出特定插件的功能, 可选
41+ - 选项: --hide | -H | 隐藏 : 是否列出隐藏的功能 (即监听了非常规事件的功能)
3942- disable | ban | 禁用 : 禁用功能
4043 - 参数: 功能名, 可传入多个, 以空格分隔。功能名格式为 `插件名:功能名` 或 `插件ID:功能名`,
4144 如果不指定插件,则会在所有插件中查找该功能
@@ -64,12 +67,12 @@ def _save_data(data: dict):
6467plugin_disables : dict [str , dict [str , dict [str , bool ]]] = keeping (
6568 "plugin_disables" , obj_factory = _load_data , dispose = _save_data
6669)
67- plugin_functions : dict [str , dict [str , str ]] = keeping ("plugin_functions" , obj_factory = dict )
70+ plugin_functions : dict [str , dict [str , tuple [ str , bool ] ]] = keeping ("plugin_functions" , obj_factory = dict )
6871sub_functino_map : dict [str , dict [str , str ]] = keeping ("sub_functino_map" , obj_factory = dict )
6972
7073plugin_control = Alconna (
7174 "plugin" ,
72- Subcommand ("list" , alias = ["列出" ]),
75+ Subcommand ("list" , alias = ["列出" ], help_text = "列出所有已安装的功能插件" ),
7376 Subcommand (
7477 "disable" ,
7578 Args ["names" , MultiVar (str ), Field (completion = lambda : "试试用 help" )],
@@ -94,7 +97,13 @@ def _save_data(data: dict):
9497plugin_control .shortcut ("插件" , command = "plugin" )
9598function_control = Alconna (
9699 "function" ,
97- Subcommand ("list" , Args ["name?" , str , Field (completion = lambda : "试试用 help" )], alias = ["列出" ]),
100+ Subcommand (
101+ "list" ,
102+ Args ["name?" , str , Field (completion = lambda : "试试用 help" )],
103+ Option ("--hide" , alias = ["-H" , "隐藏" ], action = store_true , default = False , help_text = "是否列出隐藏的功能" ),
104+ alias = ["列出" ],
105+ help_text = "列出所有已安装的功能插件下的功能" ,
106+ ),
98107 Subcommand (
99108 "disable" ,
100109 Args ["names" , MultiVar (str ), Field (completion = lambda : "试试用 help" )],
@@ -147,26 +156,34 @@ async def _(session: Session | None = None):
147156
148157 def collect ():
149158 for plg in plugin .get_plugins (subplugged = True ):
150- if plg .id == plug .id or ( not plg . metadata or plg . metadata . role is not PluginRole . NORMAL ) :
159+ if plg .id == plug .id :
151160 continue
152-
153161 plg_id = plg .id
154162 while plg_id in plugin_service ._subplugined :
155163 plg_id = plugin_service ._subplugined [plg_id ]
164+ target = plugin .plugin_service .plugins [plg_id ]
165+
166+ if (plg .metadata is not None and plg .metadata .role is not PluginRole .NORMAL ) or (
167+ plg .metadata is None and (target .metadata is None or target .metadata .role is not PluginRole .NORMAL )
168+ ):
169+ continue
156170
157171 subscribers = plugin .get_plugin_subscribers (plg )
158- functions = {}
159- sub_functino_map [ plg_id ] = {}
172+ functions : dict [ str , tuple [ str , bool ]] = {}
173+ sub_functino_map . setdefault ( plg_id , {})
160174 for sub in subscribers :
161175 try :
162176 sup = sub .get_propagator (AlconnaSuppiler )
163177 sub_functino_map [plg_id ][sub .id ] = sup .cmd .command
164- functions .setdefault (sup .cmd .command , sup .cmd .meta .description )
178+ functions .setdefault (sup .cmd .command , ( sup .cmd .meta .description , False ) )
165179 except ValueError :
166180 sub_functino_map [plg_id ][sub .id ] = sub .label
167- functions [sub .label ] = sub .__doc__ or ""
181+ functions [sub .label ] = (
182+ sub .__doc__ or "" ,
183+ sub ._listen is not None and not issubclass (sub ._listen , (SatoriEvent , _ScheduleEvent )),
184+ )
168185 yield sub .propagate (_check_disable (sub .id , plg_id ), prepend = True )
169- plugin_functions [ plg_id ] = functions
186+ plugin_functions . setdefault ( plg_id , {}). update ( functions )
170187
171188 plug .effect (collect , "control_inject" )
172189
@@ -284,18 +301,20 @@ async def _():
284301
285302
286303@function_ctl_disp .assign ("list" )
287- async def _ (session : Session , name : str | None = None ):
288- plgs = plugin . get_plugins ( )
289- plgs = [ plg for plg in plgs if not plg . metadata or plg . metadata . role is PluginRole . NORMAL ]
290- name_to_id = { plg . metadata . name : plg . id for plg in plgs if plg . metadata }
291- ids = {plg .id for plg in plgs }
304+ async def _ (
305+ session : Session , name : str | None = None , hide : command . Query [ bool ] = command . Query ( "list.hide.value" , False )
306+ ):
307+ plg_ids = set ( plugin_functions . keys ())
308+ name_to_id = {plg .metadata . name : plg . id for plg in plugin . get_plugins () if plg . metadata and plg . id in plg_ids }
292309 if name :
293- if name not in name_to_id and name not in ids :
310+ if name not in name_to_id and name not in plg_ids :
294311 return f"未找到插件:{ name } "
295312 plg_id = name_to_id .get (name , name )
296313 functions = plugin_functions .get (plg_id , {})
297314 res = f"插件 [{ name } ] 的功能列表:\n "
298- for func_name , func_desc in functions .items ():
315+ for func_name , (func_desc , is_hide ) in functions .items ():
316+ if is_hide and not hide .result :
317+ continue
299318 stat = "✅"
300319 if session .event .channel and session .event .channel .id in plugin_disables :
301320 disables = plugin_disables [session .event .channel .id ].get (plg_id , {})
@@ -308,12 +327,16 @@ async def _(session: Session, name: str | None = None):
308327 res += f" { stat } { func_name } - { func_desc or '无描述' } \n "
309328 return res
310329 res = "已安装的功能插件及其功能:\n "
311- for plg in plgs :
330+ for plg in plugin .get_plugins ():
331+ if plg .id not in plg_ids :
332+ continue
312333 meta = plg .metadata
313334 res += f"- { meta .name } : { meta .description or '无描述' } \n " if meta else f"- { plg .id } \n "
314335 functions = plugin_functions .get (plg .id , {})
315336 stat = "✅" if plg .is_available else "❌"
316- for func_name , func_desc in functions .items ():
337+ for func_name , (func_desc , is_hide ) in functions .items ():
338+ if is_hide and not hide .result :
339+ continue
317340 if session .event .channel and session .event .channel .id in plugin_disables :
318341 disables = plugin_disables [session .event .channel .id ].get (plg .id , {})
319342 if disables .get ("$plugin" , False ):
@@ -332,21 +355,18 @@ async def _(
332355 session : Session ,
333356 names : tuple [str , ...],
334357):
335- plgs = plugin .get_plugins ()
336- plgs = [plg for plg in plgs if not plg .metadata or plg .metadata .role is PluginRole .NORMAL ]
337- name_to_id = {plg .metadata .name : plg .id for plg in plgs if plg .metadata }
338- ids = {plg .id for plg in plgs }
358+ plg_ids = set (plugin_functions .keys ())
359+ name_to_id = {plg .metadata .name : plg .id for plg in plugin .get_plugins () if plg .metadata and plg .id in plg_ids }
339360 for name in names :
340- target , func_name = name .split (":" , 1 ) if ". " in name else (None , name )
361+ target , func_name = name .split (":" , 1 ) if ": " in name else (None , name )
341362 if target :
342- if target not in name_to_id and target not in ids :
363+ if target not in name_to_id and target not in plg_ids :
343364 await session .send_message (f"未找到插件:{ target } " )
344365 continue
345366 plg_id = name_to_id .get (target , target )
346367 else :
347- for plg in plgs :
348- if func_name in plugin_functions .get (plg .id , {}):
349- plg_id = plg .id
368+ for plg_id , functions in plugin_functions .items ():
369+ if func_name in functions :
350370 break
351371 else :
352372 await session .send_message (f"未找到功能:{ func_name } " )
@@ -367,21 +387,18 @@ async def _(
367387 session : Session ,
368388 names : tuple [str , ...],
369389):
370- plgs = plugin .get_plugins ()
371- plgs = [plg for plg in plgs if not plg .metadata or plg .metadata .role is PluginRole .NORMAL ]
372- name_to_id = {plg .metadata .name : plg .id for plg in plgs if plg .metadata }
373- ids = {plg .id for plg in plgs }
390+ plg_ids = set (plugin_functions .keys ())
391+ name_to_id = {plg .metadata .name : plg .id for plg in plugin .get_plugins () if plg .metadata and plg .id in plg_ids }
374392 for name in names :
375- target , func_name = name .split (". " , 1 ) if ". " in name else (None , name )
393+ target , func_name = name .split (": " , 1 ) if ": " in name else (None , name )
376394 if target :
377- if target not in name_to_id and target not in ids :
395+ if target not in name_to_id and target not in plg_ids :
378396 await session .send_message (f"未找到插件:{ target } " )
379397 continue
380398 plg_id = name_to_id .get (target , target )
381399 else :
382- for plg in plgs :
383- if func_name in plugin_functions .get (plg .id , {}):
384- plg_id = plg .id
400+ for plg_id , functions in plugin_functions .items ():
401+ if func_name in functions :
385402 break
386403 else :
387404 await session .send_message (f"未找到功能:{ func_name } " )
0 commit comments