async def test_load_plugin(app: App, load_plugin: Set["Plugin"]): import nonebot from nonebot.plugin import PluginManager loaded_plugins = { plugin for plugin in nonebot.get_loaded_plugins() if not plugin.parent_plugin } assert loaded_plugins == load_plugin # check simple plugin assert "plugins.export" in sys.modules # check sub plugin plugin = nonebot.get_plugin("nested_subplugin") assert plugin assert "plugins.nested.plugins.nested_subplugin" in sys.modules assert plugin.parent_plugin == nonebot.get_plugin("nested") # check load again with pytest.raises(RuntimeError): PluginManager(plugins=["plugins.export"]).load_all_plugins() with pytest.raises(RuntimeError): PluginManager(search_path=["plugins"]).load_all_plugins() # check not found assert nonebot.load_plugin("some_plugin_not_exist") is None
async def test_get_plugin(app: App, load_plugin: Set["Plugin"]): import nonebot # check simple plugin plugin = nonebot.get_plugin("export") assert plugin assert plugin.module_name == "plugins.export" # check sub plugin plugin = nonebot.get_plugin("nested_subplugin") assert plugin assert plugin.module_name == "plugins.nested.plugins.nested_subplugin" # check get plugin by module name plugin = nonebot.get_plugin_by_module_name("plugins.nested.utils") assert plugin assert plugin.module_name == "plugins.nested"
async def handle_plugin(bot: Bot, event: Event, state: T_State): plugin = state["plugin"] auth_node_plugin = state["auth_node_plugin"] if plugin not in auth_node_plugin: await omegaauth.reject('插件名称错误, 请重新输入:') plugin_auth_nodes = get_plugin(plugin).export.get('auth_node') state["plugin_auth_nodes"] = plugin_auth_nodes an_list = '\n'.join(plugin_auth_nodes) await omegaauth.send(f'{plugin}可配置的权限节点有:\n\n{an_list}')
async def test_load_plugin(load_plugin: Set["Plugin"]): import nonebot loaded_plugins = set(plugin for plugin in nonebot.get_loaded_plugins() if not plugin.parent_plugin) assert loaded_plugins == load_plugin plugin = nonebot.get_plugin("export") assert plugin assert plugin.module_name == "plugins.export" assert "plugins.export" in sys.modules try: nonebot.load_plugin("plugins.export") assert False except RuntimeError: assert True
async def handle_plugin_cooldown(matcher: Matcher, bot: Bot, event: Event, state: T_State): group_id = event.dict().get('group_id') user_id = event.dict().get('user_id') global_config = get_driver().config superusers = global_config.superusers # 忽略超级用户 if user_id in [int(x) for x in superusers]: return # 只处理message事件 if matcher.type != 'message': return # 处理插件冷却 # 冷却处理优先级: 全局>插件>群组>用户 # 冷却限制优先级: 用户>群组>插件>全局 plugin_name = matcher.module plugin = get_plugin(plugin_name) plugin_cool_down_list = plugin.export.get('cool_down') # 只处理声明了__plugin_cool_down__的插件 if not plugin_cool_down_list: return # 检查用户或群组是否有skip_cd权限, 跳过冷却检查 skip_cd_auth_node = f'{plugin_name}.{PluginCoolDown.skip_auth_node}' user_auth_res = DBAuth(auth_id=user_id, auth_type='user', auth_node=skip_cd_auth_node) if user_auth_res.allow_tag().result == 1 and user_auth_res.deny_tag( ).result == 0: return group_auth_res = DBAuth(auth_id=group_id, auth_type='group', auth_node=skip_cd_auth_node) if group_auth_res.allow_tag().result == 1 and group_auth_res.deny_tag( ).result == 0: return # 检查冷却情况 global_check = DBCoolDownEvent.check_global_cool_down_event() plugin_check = DBCoolDownEvent.check_plugin_cool_down_event( plugin=plugin_name) group_check = DBCoolDownEvent.check_group_cool_down_event( plugin=plugin_name, group_id=group_id) user_check = DBCoolDownEvent.check_user_cool_down_event(plugin=plugin_name, user_id=user_id) # 处理全局冷却 # 先检查是否已有全局冷却 if plugin_check.result == 1 or group_check.result == 1 or user_check.result == 1: pass elif global_check.result == 1: await bot.send( event=event, message=Message( f'{MessageSegment.at(user_id=user_id)}命令冷却中!\n{global_check.info}' )) raise IgnoredException('全局命令冷却中') elif global_check.result == 0: pass else: logger.error( f'全局冷却事件异常! group: {group_id}, user: {user_id}, error: {global_check.info}' ) # 然后再处理命令中存在的全局冷却 for time in [ x.cool_down_time for x in plugin_cool_down_list if x.type == 'global' ]: # 若有插件、群组或用户冷却则交由其处理 if plugin_check.result == 1 or group_check.result == 1 or user_check.result == 1: break res = check_and_set_global_cool_down(minutes=time) if res.result == 1: await bot.send( event=event, message=Message( f'{MessageSegment.at(user_id=user_id)}命令冷却中!\n{res.info}')) raise IgnoredException('全局命令冷却中') elif res.result == 0: pass else: logger.error( f'全局冷却事件异常! group: {group_id}, user: {user_id}, error: {res.info}' ) # 处理插件冷却 for time in [ x.cool_down_time for x in plugin_cool_down_list if x.type == 'plugin' ]: # 若有群组或用户冷却则交由其处理 if group_check.result == 1 or user_check.result == 1: break res = check_and_set_plugin_cool_down(minutes=time, plugin=plugin_name) if res.result == 1: await bot.send( event=event, message=Message( f'{MessageSegment.at(user_id=user_id)}命令冷却中!\n{res.info}')) raise IgnoredException('插件命令冷却中') elif res.result == 0: pass else: logger.error( f'插件冷却事件异常! group: {group_id}, user: {user_id}, plugin: {plugin_name}, error: {res.info}' ) # 处理群组冷却 for time in [ x.cool_down_time for x in plugin_cool_down_list if x.type == 'group' ]: if not group_id: break # 若有用户冷却则交由其处理 if user_check.result == 1: break res = check_and_set_group_cool_down(minutes=time, plugin=plugin_name, group_id=group_id) if res.result == 1: await bot.send( event=event, message=Message( f'{MessageSegment.at(user_id=user_id)}命令冷却中!\n{res.info}')) raise IgnoredException('群组命令冷却中') elif res.result == 0: pass else: logger.error( f'群组冷却事件异常! group: {group_id}, user: {user_id}, plugin: {plugin_name}, error: {res.info}' ) # 处理用户冷却 for time in [ x.cool_down_time for x in plugin_cool_down_list if x.type == 'user' ]: if not user_id: break res = check_and_set_user_cool_down(minutes=time, plugin=plugin_name, user_id=user_id) if res.result == 1: await bot.send( event=event, message=Message( f'{MessageSegment.at(user_id=user_id)}命令冷却中!\n{res.info}')) raise IgnoredException('用户命令冷却中') elif res.result == 0: pass else: logger.error( f'用户冷却事件异常! group: {group_id}, user: {user_id}, plugin: {plugin_name}, error: {res.info}' )
import nonebot plugin = nonebot.get_plugin("bad_plugin") assert plugin x = 1 / 0
async def test_bad_plugin(app: App): import nonebot nonebot.load_plugins("bad_plugins") assert nonebot.get_plugin("bad_plugins") is None