加载指定插件。 可以使用完整插件模块名或者插件标识符加载。 参数: name: 插件名称或插件标识符。
(self, name: str)
| 154 | return self.available_plugins |
| 155 | |
| 156 | def load_plugin(self, name: str) -> Plugin | None: |
| 157 | """加载指定插件。 |
| 158 | |
| 159 | 可以使用完整插件模块名或者插件标识符加载。 |
| 160 | |
| 161 | 参数: |
| 162 | name: 插件名称或插件标识符。 |
| 163 | """ |
| 164 | |
| 165 | try: |
| 166 | # load using plugin id |
| 167 | if name in self._third_party_plugin_ids: |
| 168 | module = importlib.import_module(self._third_party_plugin_ids[name]) |
| 169 | elif name in self._searched_plugin_ids: |
| 170 | module = importlib.import_module(self._searched_plugin_ids[name]) |
| 171 | # load using module name |
| 172 | elif ( |
| 173 | name in self._third_party_plugin_ids.values() |
| 174 | or name in self._searched_plugin_ids.values() |
| 175 | ): |
| 176 | module = importlib.import_module(name) |
| 177 | else: |
| 178 | raise RuntimeError(f"Plugin not found: {name}! Check your plugin name") |
| 179 | |
| 180 | if ( |
| 181 | plugin := getattr(module, "__plugin__", None) |
| 182 | ) is None or not isinstance(plugin, Plugin): |
| 183 | raise RuntimeError( |
| 184 | f"Module {module.__name__} is not loaded as a plugin! " |
| 185 | f"Make sure not to import it before loading." |
| 186 | ) |
| 187 | logger.opt(colors=True).success( |
| 188 | f'Succeeded to load plugin "<y>{escape_tag(plugin.id_)}</y>"' |
| 189 | + ( |
| 190 | f' from "<m>{escape_tag(plugin.module_name)}</m>"' |
| 191 | if plugin.module_name != plugin.id_ |
| 192 | else "" |
| 193 | ) |
| 194 | ) |
| 195 | return plugin |
| 196 | except Exception as e: |
| 197 | logger.opt(colors=True, exception=e).error( |
| 198 | f'<r><bg #f8bbd0>Failed to import "{escape_tag(name)}"</bg #f8bbd0></r>' |
| 199 | ) |
| 200 | |
| 201 | def load_all_plugins(self) -> set[Plugin]: |
| 202 | """加载所有可用插件。""" |