diff --git a/nonebot/plugin/load.py b/nonebot/plugin/load.py
index 2d2c55bb..03d5dbde 100644
--- a/nonebot/plugin/load.py
+++ b/nonebot/plugin/load.py
@@ -137,6 +137,12 @@ def load_builtin_plugins(*plugins) -> Set[Plugin]:
return load_all_plugins([f"nonebot.plugins.{p}" for p in plugins], [])
+def _find_manager_by_name(name: str) -> Optional[PluginManager]:
+ for manager in reversed(_managers):
+ if name in manager.plugins or name in manager.searched_plugins:
+ return manager
+
+
def require(name: str) -> Export:
"""获取一个插件的导出内容。
@@ -148,7 +154,13 @@ def require(name: str) -> Export:
异常:
RuntimeError: 插件无法加载
"""
- plugin = get_plugin(name) or load_plugin(name)
+ plugin = get_plugin(name.rsplit(".", 1)[-1])
if not plugin:
- raise RuntimeError(f'Cannot load plugin "{name}"!')
+ manager = _find_manager_by_name(name)
+ if manager:
+ plugin = manager.load_plugin(name)
+ else:
+ plugin = load_plugin(name)
+ if not plugin:
+ raise RuntimeError(f'Cannot load plugin "{name}"!')
return plugin.export
diff --git a/nonebot/plugin/manager.py b/nonebot/plugin/manager.py
index c46c77c4..f359db57 100644
--- a/nonebot/plugin/manager.py
+++ b/nonebot/plugin/manager.py
@@ -53,7 +53,10 @@ class PluginManager:
return [
*chain.from_iterable(
- [*manager.plugins, *manager.searched_plugins.keys()]
+ [
+ *map(lambda x: x.rsplit(".", 1)[-1], manager.plugins),
+ *manager.searched_plugins.keys(),
+ ]
for manager in _pre_managers
)
]
@@ -65,7 +68,7 @@ class PluginManager:
third_party_plugins: Set[str] = set()
for plugin in self.plugins:
- name = plugin.rsplit(".", 1)[-1] if "." in plugin else plugin
+ name = plugin.rsplit(".", 1)[-1]
if name in third_party_plugins or name in previous_plugins:
raise RuntimeError(
f"Plugin already exists: {name}! Check your plugin name"
@@ -99,17 +102,23 @@ class PluginManager:
try:
if name in self.plugins:
module = importlib.import_module(name)
- elif name not in self.searched_plugins:
- raise RuntimeError(f"Plugin not found: {name}! Check your plugin name")
- else:
+ elif name in self.searched_plugins:
module = importlib.import_module(
self._path_to_module_name(self.searched_plugins[name])
)
+ else:
+ raise RuntimeError(f"Plugin not found: {name}! Check your plugin name")
logger.opt(colors=True).success(
f'Succeeded to import "{escape_tag(name)}"'
)
- return getattr(module, "__plugin__", None)
+ plugin = getattr(module, "__plugin__", None)
+ if plugin is None:
+ raise RuntimeError(
+ f"Module {module.__name__} is not loaded as a plugin! "
+ "Make sure not to import it before loading."
+ )
+ return plugin
except Exception as e:
logger.opt(colors=True, exception=e).error(
f'Failed to import "{escape_tag(name)}"'
diff --git a/tests/test_init.py b/tests/test_init.py
index 03da1755..1d241728 100644
--- a/tests/test_init.py
+++ b/tests/test_init.py
@@ -95,8 +95,4 @@ async def test_load_plugin(load_plugin: Set["Plugin"]):
except RuntimeError:
assert True
- try:
- nonebot.load_plugin("some_plugin_no_exist")
- assert False
- except Exception:
- assert nonebot.get_plugin("some_plugin_no_exist") is None
+ assert nonebot.load_plugin("some_plugin_no_exist") is None