2022-01-18 23:46:10 +08:00
|
|
|
|
"""本模块主要定义了 NoneBot 启动所需函数,供 bot 入口文件调用。
|
|
|
|
|
|
2022-01-12 19:24:01 +08:00
|
|
|
|
## 快捷导入
|
2020-10-08 21:36:57 +08:00
|
|
|
|
|
2022-01-18 23:46:10 +08:00
|
|
|
|
为方便使用,本模块从子模块导入了部分内容,以下内容可以直接通过本模块导入:
|
2022-01-12 18:19:21 +08:00
|
|
|
|
|
2022-01-18 12:31:08 +08:00
|
|
|
|
- `on` => {ref}``on` <nonebot.plugin.on.on>`
|
|
|
|
|
- `on_metaevent` => {ref}``on_metaevent` <nonebot.plugin.on.on_metaevent>`
|
|
|
|
|
- `on_message` => {ref}``on_message` <nonebot.plugin.on.on_message>`
|
|
|
|
|
- `on_notice` => {ref}``on_notice` <nonebot.plugin.on.on_notice>`
|
|
|
|
|
- `on_request` => {ref}``on_request` <nonebot.plugin.on.on_request>`
|
|
|
|
|
- `on_startswith` => {ref}``on_startswith` <nonebot.plugin.on.on_startswith>`
|
|
|
|
|
- `on_endswith` => {ref}``on_endswith` <nonebot.plugin.on.on_endswith>`
|
2022-02-15 08:20:29 +08:00
|
|
|
|
- `on_fullmatch` => {ref}``on_fullmatch` <nonebot.plugin.on.on_fullmatch>`
|
2022-01-18 12:31:08 +08:00
|
|
|
|
- `on_keyword` => {ref}``on_keyword` <nonebot.plugin.on.on_keyword>`
|
|
|
|
|
- `on_command` => {ref}``on_command` <nonebot.plugin.on.on_command>`
|
|
|
|
|
- `on_shell_command` => {ref}``on_shell_command` <nonebot.plugin.on.on_shell_command>`
|
|
|
|
|
- `on_regex` => {ref}``on_regex` <nonebot.plugin.on.on_regex>`
|
2022-08-30 09:54:09 +08:00
|
|
|
|
- `on_type` => {ref}``on_type` <nonebot.plugin.on.on_type>`
|
2022-01-18 12:31:08 +08:00
|
|
|
|
- `CommandGroup` => {ref}``CommandGroup` <nonebot.plugin.on.CommandGroup>`
|
|
|
|
|
- `Matchergroup` => {ref}``MatcherGroup` <nonebot.plugin.on.MatcherGroup>`
|
|
|
|
|
- `load_plugin` => {ref}``load_plugin` <nonebot.plugin.load.load_plugin>`
|
|
|
|
|
- `load_plugins` => {ref}``load_plugins` <nonebot.plugin.load.load_plugins>`
|
|
|
|
|
- `load_all_plugins` => {ref}``load_all_plugins` <nonebot.plugin.load.load_all_plugins>`
|
|
|
|
|
- `load_from_json` => {ref}``load_from_json` <nonebot.plugin.load.load_from_json>`
|
|
|
|
|
- `load_from_toml` => {ref}``load_from_toml` <nonebot.plugin.load.load_from_toml>`
|
2023-06-24 14:47:35 +08:00
|
|
|
|
- `load_builtin_plugin` =>
|
|
|
|
|
{ref}``load_builtin_plugin` <nonebot.plugin.load.load_builtin_plugin>`
|
|
|
|
|
- `load_builtin_plugins` =>
|
|
|
|
|
{ref}``load_builtin_plugins` <nonebot.plugin.load.load_builtin_plugins>`
|
2022-07-20 10:21:31 +08:00
|
|
|
|
- `get_plugin` => {ref}``get_plugin` <nonebot.plugin.get_plugin>`
|
2023-06-24 14:47:35 +08:00
|
|
|
|
- `get_plugin_by_module_name` =>
|
|
|
|
|
{ref}``get_plugin_by_module_name` <nonebot.plugin.get_plugin_by_module_name>`
|
|
|
|
|
- `get_loaded_plugins` =>
|
|
|
|
|
{ref}``get_loaded_plugins` <nonebot.plugin.get_loaded_plugins>`
|
|
|
|
|
- `get_available_plugin_names` =>
|
|
|
|
|
{ref}``get_available_plugin_names` <nonebot.plugin.get_available_plugin_names>`
|
2024-02-05 14:00:49 +08:00
|
|
|
|
- `get_plugin_config` => {ref}``get_plugin_config` <nonebot.plugin.get_plugin_config>`
|
2022-01-18 12:31:08 +08:00
|
|
|
|
- `require` => {ref}``require` <nonebot.plugin.load.require>`
|
2022-01-15 23:53:36 +08:00
|
|
|
|
|
|
|
|
|
FrontMatter:
|
2024-10-22 10:33:48 +08:00
|
|
|
|
mdx:
|
|
|
|
|
format: md
|
2022-01-15 23:53:36 +08:00
|
|
|
|
sidebar_position: 0
|
|
|
|
|
description: nonebot 模块
|
2020-10-08 21:36:57 +08:00
|
|
|
|
"""
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
2022-10-14 09:58:44 +08:00
|
|
|
|
import os
|
2022-11-12 18:22:16 +08:00
|
|
|
|
from importlib.metadata import version
|
2024-04-16 00:33:48 +08:00
|
|
|
|
from typing import Any, Union, TypeVar, Optional, overload
|
2020-12-06 02:30:19 +08:00
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
import loguru
|
|
|
|
|
|
2024-02-16 21:16:46 +08:00
|
|
|
|
from nonebot.compat import model_dump
|
2023-01-01 15:22:07 +08:00
|
|
|
|
from nonebot.log import logger as logger
|
2023-02-26 14:15:10 +08:00
|
|
|
|
from nonebot.adapters import Bot, Adapter
|
2024-01-15 13:24:55 +08:00
|
|
|
|
from nonebot.config import DOTENV_TYPE, Env, Config
|
2023-02-22 23:32:48 +08:00
|
|
|
|
from nonebot.utils import escape_tag, resolve_dot_notation
|
2023-08-26 11:03:24 +08:00
|
|
|
|
from nonebot.drivers import Driver, ASGIMixin, combine_driver
|
2020-07-04 22:51:10 +08:00
|
|
|
|
|
2021-02-22 10:44:42 +08:00
|
|
|
|
try:
|
2022-11-12 18:22:16 +08:00
|
|
|
|
__version__ = version("nonebot2")
|
2022-08-01 10:52:15 +08:00
|
|
|
|
except Exception: # pragma: no cover
|
2021-02-22 10:44:42 +08:00
|
|
|
|
__version__ = None
|
2020-11-15 16:18:23 +08:00
|
|
|
|
|
2023-02-28 14:46:09 +08:00
|
|
|
|
A = TypeVar("A", bound=Adapter)
|
|
|
|
|
|
2020-08-14 17:41:24 +08:00
|
|
|
|
_driver: Optional[Driver] = None
|
2020-07-04 22:51:10 +08:00
|
|
|
|
|
|
|
|
|
|
2020-12-05 17:19:23 +08:00
|
|
|
|
def get_driver() -> Driver:
|
2022-01-17 15:06:53 +08:00
|
|
|
|
"""获取全局 {ref}`nonebot.drivers.Driver` 实例。
|
|
|
|
|
|
|
|
|
|
可用于在计划任务的回调等情形中获取当前 {ref}`nonebot.drivers.Driver` 实例。
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:43:07 +08:00
|
|
|
|
返回:
|
2022-01-17 15:06:53 +08:00
|
|
|
|
全局 {ref}`nonebot.drivers.Driver` 对象
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:45:35 +08:00
|
|
|
|
异常:
|
2023-06-24 14:47:35 +08:00
|
|
|
|
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
|
|
|
|
|
({ref}`nonebot.init <nonebot.init>` 尚未调用)
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
|
|
|
|
```python
|
2020-08-18 17:24:49 +08:00
|
|
|
|
driver = nonebot.get_driver()
|
2022-01-12 18:53:30 +08:00
|
|
|
|
```
|
2020-08-18 17:24:49 +08:00
|
|
|
|
"""
|
2020-07-04 22:51:10 +08:00
|
|
|
|
if _driver is None:
|
|
|
|
|
raise ValueError("NoneBot has not been initialized.")
|
|
|
|
|
return _driver
|
|
|
|
|
|
|
|
|
|
|
2023-02-28 14:46:09 +08:00
|
|
|
|
@overload
|
|
|
|
|
def get_adapter(name: str) -> Adapter:
|
2023-06-24 14:47:35 +08:00
|
|
|
|
"""
|
|
|
|
|
参数:
|
|
|
|
|
name: 适配器名称
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
指定名称的 {ref}`nonebot.adapters.Adapter` 对象
|
|
|
|
|
"""
|
2023-02-28 14:46:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@overload
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def get_adapter(name: type[A]) -> A:
|
2023-06-24 14:47:35 +08:00
|
|
|
|
"""
|
|
|
|
|
参数:
|
|
|
|
|
name: 适配器类型
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
指定类型的 {ref}`nonebot.adapters.Adapter` 对象
|
|
|
|
|
"""
|
2023-02-28 14:46:09 +08:00
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def get_adapter(name: Union[str, type[Adapter]]) -> Adapter:
|
2023-02-26 14:15:10 +08:00
|
|
|
|
"""获取已注册的 {ref}`nonebot.adapters.Adapter` 实例。
|
|
|
|
|
|
|
|
|
|
异常:
|
|
|
|
|
ValueError: 指定的 {ref}`nonebot.adapters.Adapter` 未注册
|
2023-06-24 14:47:35 +08:00
|
|
|
|
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
|
|
|
|
|
({ref}`nonebot.init <nonebot.init>` 尚未调用)
|
2023-02-26 14:15:10 +08:00
|
|
|
|
|
|
|
|
|
用法:
|
|
|
|
|
```python
|
|
|
|
|
from nonebot.adapters.console import Adapter
|
|
|
|
|
adapter = nonebot.get_adapter(Adapter)
|
|
|
|
|
```
|
|
|
|
|
"""
|
|
|
|
|
adapters = get_adapters()
|
|
|
|
|
target = name if isinstance(name, str) else name.get_name()
|
|
|
|
|
if target not in adapters:
|
|
|
|
|
raise ValueError(f"Adapter {target} not registered.")
|
|
|
|
|
return adapters[target]
|
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def get_adapters() -> dict[str, Adapter]:
|
2023-02-26 14:15:10 +08:00
|
|
|
|
"""获取所有已注册的 {ref}`nonebot.adapters.Adapter` 实例。
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
所有 {ref}`nonebot.adapters.Adapter` 实例字典
|
|
|
|
|
|
|
|
|
|
异常:
|
2023-06-24 14:47:35 +08:00
|
|
|
|
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
|
|
|
|
|
({ref}`nonebot.init <nonebot.init>` 尚未调用)
|
2023-02-26 14:15:10 +08:00
|
|
|
|
|
|
|
|
|
用法:
|
|
|
|
|
```python
|
|
|
|
|
adapters = nonebot.get_adapters()
|
|
|
|
|
```
|
|
|
|
|
"""
|
|
|
|
|
return get_driver()._adapters.copy()
|
|
|
|
|
|
|
|
|
|
|
2021-06-15 01:23:12 +08:00
|
|
|
|
def get_app() -> Any:
|
2023-08-26 11:03:24 +08:00
|
|
|
|
"""获取全局 {ref}`nonebot.drivers.ASGIMixin` 对应的 Server App 对象。
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:43:07 +08:00
|
|
|
|
返回:
|
2022-01-17 15:06:53 +08:00
|
|
|
|
Server App 对象
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:45:35 +08:00
|
|
|
|
异常:
|
2023-08-26 11:03:24 +08:00
|
|
|
|
AssertionError: 全局 Driver 对象不是 {ref}`nonebot.drivers.ASGIMixin` 类型
|
2023-06-24 14:47:35 +08:00
|
|
|
|
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
|
|
|
|
|
({ref}`nonebot.init <nonebot.init>` 尚未调用)
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
|
|
|
|
```python
|
2020-08-18 17:24:49 +08:00
|
|
|
|
app = nonebot.get_app()
|
2022-01-12 18:53:30 +08:00
|
|
|
|
```
|
2020-08-18 17:24:49 +08:00
|
|
|
|
"""
|
2020-07-04 22:51:10 +08:00
|
|
|
|
driver = get_driver()
|
2023-08-26 11:03:24 +08:00
|
|
|
|
assert isinstance(driver, ASGIMixin), "app object is only available for asgi driver"
|
2020-07-04 22:51:10 +08:00
|
|
|
|
return driver.server_app
|
|
|
|
|
|
|
|
|
|
|
2021-06-15 01:23:12 +08:00
|
|
|
|
def get_asgi() -> Any:
|
2023-09-12 14:31:41 +08:00
|
|
|
|
"""获取全局 {ref}`nonebot.drivers.ASGIMixin` 对应的
|
2023-06-24 14:47:35 +08:00
|
|
|
|
[ASGI](https://asgi.readthedocs.io/) 对象。
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:43:07 +08:00
|
|
|
|
返回:
|
2022-01-17 15:06:53 +08:00
|
|
|
|
ASGI 对象
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:45:35 +08:00
|
|
|
|
异常:
|
2023-08-26 11:03:24 +08:00
|
|
|
|
AssertionError: 全局 Driver 对象不是 {ref}`nonebot.drivers.ASGIMixin` 类型
|
2023-06-24 14:47:35 +08:00
|
|
|
|
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
|
|
|
|
|
({ref}`nonebot.init <nonebot.init>` 尚未调用)
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
|
|
|
|
```python
|
2020-08-18 17:24:49 +08:00
|
|
|
|
asgi = nonebot.get_asgi()
|
2022-01-12 18:53:30 +08:00
|
|
|
|
```
|
2020-08-18 17:24:49 +08:00
|
|
|
|
"""
|
2020-07-04 22:51:10 +08:00
|
|
|
|
driver = get_driver()
|
2021-06-15 01:23:12 +08:00
|
|
|
|
assert isinstance(
|
2023-08-26 11:03:24 +08:00
|
|
|
|
driver, ASGIMixin
|
2023-09-12 14:31:41 +08:00
|
|
|
|
), "asgi object is only available for asgi driver"
|
2020-07-04 22:51:10 +08:00
|
|
|
|
return driver.asgi
|
|
|
|
|
|
|
|
|
|
|
2021-06-27 14:03:54 +08:00
|
|
|
|
def get_bot(self_id: Optional[str] = None) -> Bot:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""获取一个连接到 NoneBot 的 {ref}`nonebot.adapters.Bot` 对象。
|
2022-01-17 15:06:53 +08:00
|
|
|
|
|
|
|
|
|
当提供 `self_id` 时,此函数是 `get_bots()[self_id]` 的简写;
|
2022-02-06 14:52:50 +08:00
|
|
|
|
当不提供时,返回一个 {ref}`nonebot.adapters.Bot`。
|
2021-06-27 14:03:54 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2023-06-24 14:47:35 +08:00
|
|
|
|
self_id: 用来识别 {ref}`nonebot.adapters.Bot` 的
|
|
|
|
|
{ref}`nonebot.adapters.Bot.self_id` 属性
|
2021-06-27 14:03:54 +08:00
|
|
|
|
|
2022-01-12 18:43:07 +08:00
|
|
|
|
返回:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
{ref}`nonebot.adapters.Bot` 对象
|
2021-06-27 14:03:54 +08:00
|
|
|
|
|
2022-01-12 18:45:35 +08:00
|
|
|
|
异常:
|
2022-01-17 15:06:53 +08:00
|
|
|
|
KeyError: 对应 self_id 的 Bot 不存在
|
|
|
|
|
ValueError: 没有传入 self_id 且没有 Bot 可用
|
2023-06-24 14:47:35 +08:00
|
|
|
|
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
|
|
|
|
|
({ref}`nonebot.init <nonebot.init>` 尚未调用)
|
2021-06-27 14:03:54 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
|
|
|
|
```python
|
2022-01-17 15:06:53 +08:00
|
|
|
|
assert nonebot.get_bot("12345") == nonebot.get_bots()["12345"]
|
2021-06-27 14:03:54 +08:00
|
|
|
|
|
|
|
|
|
another_unspecified_bot = nonebot.get_bot()
|
2022-01-12 18:53:30 +08:00
|
|
|
|
```
|
2021-06-27 14:03:54 +08:00
|
|
|
|
"""
|
|
|
|
|
bots = get_bots()
|
|
|
|
|
if self_id is not None:
|
|
|
|
|
return bots[self_id]
|
|
|
|
|
|
|
|
|
|
for bot in bots.values():
|
|
|
|
|
return bot
|
|
|
|
|
|
|
|
|
|
raise ValueError("There are no bots to get.")
|
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def get_bots() -> dict[str, Bot]:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""获取所有连接到 NoneBot 的 {ref}`nonebot.adapters.Bot` 对象。
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:43:07 +08:00
|
|
|
|
返回:
|
2023-06-24 14:47:35 +08:00
|
|
|
|
一个以 {ref}`nonebot.adapters.Bot.self_id` 为键
|
|
|
|
|
{ref}`nonebot.adapters.Bot` 对象为值的字典
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:45:35 +08:00
|
|
|
|
异常:
|
2023-06-24 14:47:35 +08:00
|
|
|
|
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
|
|
|
|
|
({ref}`nonebot.init <nonebot.init>` 尚未调用)
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
|
|
|
|
```python
|
2020-08-18 17:24:49 +08:00
|
|
|
|
bots = nonebot.get_bots()
|
2022-01-12 18:53:30 +08:00
|
|
|
|
```
|
2020-08-18 17:24:49 +08:00
|
|
|
|
"""
|
2022-08-14 19:41:00 +08:00
|
|
|
|
return get_driver().bots
|
2020-08-15 17:47:50 +08:00
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def _resolve_combine_expr(obj_str: str) -> type[Driver]:
|
2021-12-23 17:20:26 +08:00
|
|
|
|
drivers = obj_str.split("+")
|
2023-02-22 23:32:48 +08:00
|
|
|
|
DriverClass = resolve_dot_notation(
|
2021-12-23 17:20:26 +08:00
|
|
|
|
drivers[0], "Driver", default_prefix="nonebot.drivers."
|
|
|
|
|
)
|
|
|
|
|
if len(drivers) == 1:
|
2021-12-24 15:04:18 +08:00
|
|
|
|
logger.trace(f"Detected driver {DriverClass} with no mixins.")
|
2021-12-23 17:20:26 +08:00
|
|
|
|
return DriverClass
|
|
|
|
|
mixins = [
|
2023-02-22 23:32:48 +08:00
|
|
|
|
resolve_dot_notation(mixin, "Mixin", default_prefix="nonebot.drivers.")
|
2021-12-23 17:20:26 +08:00
|
|
|
|
for mixin in drivers[1:]
|
|
|
|
|
]
|
2021-12-24 15:04:18 +08:00
|
|
|
|
logger.trace(f"Detected driver {DriverClass} with mixins {mixins}.")
|
2021-12-23 17:20:26 +08:00
|
|
|
|
return combine_driver(DriverClass, *mixins)
|
|
|
|
|
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def _log_patcher(record: "loguru.Record"):
|
2024-04-20 14:47:12 +08:00
|
|
|
|
"""使用插件标识优化日志展示"""
|
2022-09-09 11:52:57 +08:00
|
|
|
|
record["name"] = (
|
2024-04-20 14:47:12 +08:00
|
|
|
|
plugin.id_
|
2022-09-09 11:52:57 +08:00
|
|
|
|
if (module_name := record["name"])
|
|
|
|
|
and (plugin := get_plugin_by_module_name(module_name))
|
2024-04-20 14:47:12 +08:00
|
|
|
|
else (module_name and module_name.split(".", maxsplit=1)[0])
|
2022-09-09 11:52:57 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2024-01-15 13:24:55 +08:00
|
|
|
|
def init(*, _env_file: Optional[DOTENV_TYPE] = None, **kwargs: Any) -> None:
|
2022-01-17 15:06:53 +08:00
|
|
|
|
"""初始化 NoneBot 以及 全局 {ref}`nonebot.drivers.Driver` 对象。
|
2020-08-19 20:29:37 +08:00
|
|
|
|
|
2022-01-12 18:16:05 +08:00
|
|
|
|
NoneBot 将会从 .env 文件中读取环境信息,并使用相应的 env 文件配置。
|
2020-08-19 20:29:37 +08:00
|
|
|
|
|
2022-01-17 15:06:53 +08:00
|
|
|
|
也可以传入自定义的 `_env_file` 来指定 NoneBot 从该文件读取配置。
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-17 15:06:53 +08:00
|
|
|
|
_env_file: 配置文件名,默认从 `.env.{env_name}` 中读取配置
|
|
|
|
|
kwargs: 任意变量,将会存储到 {ref}`nonebot.drivers.Driver.config` 对象里
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
|
|
|
|
```python
|
2020-08-18 17:24:49 +08:00
|
|
|
|
nonebot.init(database=Database(...))
|
2022-01-12 18:53:30 +08:00
|
|
|
|
```
|
2020-08-18 17:24:49 +08:00
|
|
|
|
"""
|
2020-07-04 22:51:10 +08:00
|
|
|
|
global _driver
|
2020-08-27 16:43:58 +08:00
|
|
|
|
if not _driver:
|
2021-04-19 21:15:10 +08:00
|
|
|
|
logger.success("NoneBot is initializing...")
|
2020-08-27 16:43:58 +08:00
|
|
|
|
env = Env()
|
2022-10-14 09:58:44 +08:00
|
|
|
|
_env_file = _env_file or f".env.{env.environment}"
|
2021-11-22 23:21:26 +08:00
|
|
|
|
config = Config(
|
|
|
|
|
**kwargs,
|
2024-02-06 12:48:23 +08:00
|
|
|
|
_env_file=(
|
|
|
|
|
(".env", _env_file)
|
|
|
|
|
if isinstance(_env_file, (str, os.PathLike))
|
|
|
|
|
else _env_file
|
|
|
|
|
),
|
2021-11-22 23:21:26 +08:00
|
|
|
|
)
|
2020-07-04 22:51:10 +08:00
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
logger.configure(
|
|
|
|
|
extra={"nonebot_log_level": config.log_level}, patcher=_log_patcher
|
|
|
|
|
)
|
2021-08-12 16:45:22 +08:00
|
|
|
|
logger.opt(colors=True).info(
|
2021-11-22 23:21:26 +08:00
|
|
|
|
f"Current <y><b>Env: {escape_tag(env.environment)}</b></y>"
|
|
|
|
|
)
|
2020-09-30 18:01:31 +08:00
|
|
|
|
logger.opt(colors=True).debug(
|
2024-02-16 21:16:46 +08:00
|
|
|
|
f"Loaded <y><b>Config</b></y>: {escape_tag(str(model_dump(config)))}"
|
2021-11-22 23:21:26 +08:00
|
|
|
|
)
|
2020-07-04 22:51:10 +08:00
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
DriverClass = _resolve_combine_expr(config.driver)
|
2020-08-27 16:43:58 +08:00
|
|
|
|
_driver = DriverClass(env, config)
|
2020-07-04 22:51:10 +08:00
|
|
|
|
|
|
|
|
|
|
2021-12-16 17:28:57 +08:00
|
|
|
|
def run(*args: Any, **kwargs: Any) -> None:
|
2022-01-17 15:06:53 +08:00
|
|
|
|
"""启动 NoneBot,即运行全局 {ref}`nonebot.drivers.Driver` 对象。
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-17 15:06:53 +08:00
|
|
|
|
args: 传入 {ref}`nonebot.drivers.Driver.run` 的位置参数
|
|
|
|
|
kwargs: 传入 {ref}`nonebot.drivers.Driver.run` 的命名参数
|
2020-08-18 17:24:49 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
|
|
|
|
```python
|
2020-08-18 17:24:49 +08:00
|
|
|
|
nonebot.run(host="127.0.0.1", port=8080)
|
2022-01-12 18:53:30 +08:00
|
|
|
|
```
|
2020-08-18 17:24:49 +08:00
|
|
|
|
"""
|
2021-04-19 21:15:10 +08:00
|
|
|
|
logger.success("Running NoneBot...")
|
2021-12-16 17:28:57 +08:00
|
|
|
|
get_driver().run(*args, **kwargs)
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
|
|
|
|
|
2022-01-17 15:06:53 +08:00
|
|
|
|
from nonebot.plugin import on as on
|
2022-08-30 09:54:09 +08:00
|
|
|
|
from nonebot.plugin import on_type as on_type
|
2021-09-18 16:11:03 +08:00
|
|
|
|
from nonebot.plugin import require as require
|
|
|
|
|
from nonebot.plugin import on_regex as on_regex
|
|
|
|
|
from nonebot.plugin import on_notice as on_notice
|
|
|
|
|
from nonebot.plugin import get_plugin as get_plugin
|
|
|
|
|
from nonebot.plugin import on_command as on_command
|
|
|
|
|
from nonebot.plugin import on_keyword as on_keyword
|
|
|
|
|
from nonebot.plugin import on_message as on_message
|
|
|
|
|
from nonebot.plugin import on_request as on_request
|
|
|
|
|
from nonebot.plugin import load_plugin as load_plugin
|
|
|
|
|
from nonebot.plugin import on_endswith as on_endswith
|
|
|
|
|
from nonebot.plugin import CommandGroup as CommandGroup
|
|
|
|
|
from nonebot.plugin import MatcherGroup as MatcherGroup
|
|
|
|
|
from nonebot.plugin import load_plugins as load_plugins
|
2022-02-15 08:20:29 +08:00
|
|
|
|
from nonebot.plugin import on_fullmatch as on_fullmatch
|
2022-02-15 08:27:42 +08:00
|
|
|
|
from nonebot.plugin import on_metaevent as on_metaevent
|
2021-09-18 16:11:03 +08:00
|
|
|
|
from nonebot.plugin import on_startswith as on_startswith
|
|
|
|
|
from nonebot.plugin import load_from_json as load_from_json
|
|
|
|
|
from nonebot.plugin import load_from_toml as load_from_toml
|
|
|
|
|
from nonebot.plugin import load_all_plugins as load_all_plugins
|
|
|
|
|
from nonebot.plugin import on_shell_command as on_shell_command
|
2024-02-05 14:00:49 +08:00
|
|
|
|
from nonebot.plugin import get_plugin_config as get_plugin_config
|
2021-09-18 16:11:03 +08:00
|
|
|
|
from nonebot.plugin import get_loaded_plugins as get_loaded_plugins
|
2022-01-10 22:52:10 +08:00
|
|
|
|
from nonebot.plugin import load_builtin_plugin as load_builtin_plugin
|
2021-09-18 16:11:03 +08:00
|
|
|
|
from nonebot.plugin import load_builtin_plugins as load_builtin_plugins
|
2022-05-26 16:35:47 +08:00
|
|
|
|
from nonebot.plugin import get_plugin_by_module_name as get_plugin_by_module_name
|
|
|
|
|
from nonebot.plugin import get_available_plugin_names as get_available_plugin_names
|