@@ -142,6 +142,22 @@ async def build_task(group_id: str | None) -> BuildImage:
142142
143143
144144class PluginManager :
145+ @staticmethod
146+ async def _get_plugin_by_name_or_module (plugin_name : str ) -> PluginInfo | None :
147+ plugin_name = plugin_name .strip ()
148+ if not plugin_name :
149+ return None
150+ if plugin_name .isdigit ():
151+ return await PluginInfo .get_or_none (id = int (plugin_name ))
152+ plugin = await PluginInfo .get_or_none (
153+ name = plugin_name , load_status = True , plugin_type__not = PluginType .PARENT
154+ )
155+ if plugin :
156+ return plugin
157+ return await PluginInfo .get_or_none (
158+ module = plugin_name , load_status = True , plugin_type__not = PluginType .PARENT
159+ )
160+
145161 @classmethod
146162 async def set_default_status (cls , plugin_name : str , status : bool ) -> str :
147163 """设置插件进群默认状态
@@ -153,12 +169,7 @@ async def set_default_status(cls, plugin_name: str, status: bool) -> str:
153169 返回:
154170 str: 返回信息
155171 """
156- if plugin_name .isdigit ():
157- plugin = await PluginInfo .get_or_none (id = int (plugin_name ))
158- else :
159- plugin = await PluginInfo .get_or_none (
160- name = plugin_name , load_status = True , plugin_type__not = PluginType .PARENT
161- )
172+ plugin = await cls ._get_plugin_by_name_or_module (plugin_name )
162173 if plugin :
163174 plugin .default_status = status
164175 await plugin .save (update_fields = ["default_status" ])
@@ -478,12 +489,7 @@ async def _change_group_plugin(
478489 str: 返回信息
479490 """
480491
481- if plugin_name .isdigit ():
482- plugin = await PluginInfo .get_or_none (id = int (plugin_name ))
483- else :
484- plugin = await PluginInfo .get_or_none (
485- name = plugin_name , load_status = True , plugin_type__not = PluginType .PARENT
486- )
492+ plugin = await cls ._get_plugin_by_name_or_module (plugin_name )
487493 if plugin :
488494 status_str = "开启" if status else "关闭"
489495 if status :
@@ -535,12 +541,7 @@ async def superuser_block(
535541 返回:
536542 str: 返回信息
537543 """
538- if plugin_name .isdigit ():
539- plugin = await PluginInfo .get_or_none (id = int (plugin_name ))
540- else :
541- plugin = await PluginInfo .get_or_none (
542- name = plugin_name , load_status = True , plugin_type__not = PluginType .PARENT
543- )
544+ plugin = await cls ._get_plugin_by_name_or_module (plugin_name )
544545 if plugin :
545546 if group_id :
546547 if not await GroupConsole .is_superuser_block_plugin (
@@ -576,12 +577,7 @@ async def superuser_unblock(
576577 返回:
577578 str: 返回信息
578579 """
579- if plugin_name .isdigit ():
580- plugin = await PluginInfo .get_or_none (id = int (plugin_name ))
581- else :
582- plugin = await PluginInfo .get_or_none (
583- name = plugin_name , load_status = True , plugin_type__not = PluginType .PARENT
584- )
580+ plugin = await cls ._get_plugin_by_name_or_module (plugin_name )
585581 if plugin :
586582 if group_id :
587583 if await GroupConsole .is_superuser_block_plugin (
0 commit comments