nonebot2/nonebot/__init__.py

218 lines
4.9 KiB
Python
Raw Normal View History

2020-06-30 10:13:58 +08:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2020-07-04 22:51:10 +08:00
import importlib
2020-08-18 17:24:49 +08:00
from nonebot.typing import Bot, Dict, Type, Union, Driver, Optional, NoReturn
2020-07-04 22:51:10 +08:00
2020-08-14 17:41:24 +08:00
_driver: Optional[Driver] = None
2020-07-04 22:51:10 +08:00
2020-08-14 17:41:24 +08:00
def get_driver() -> Union[NoReturn, Driver]:
2020-08-18 17:24:49 +08:00
"""
2020-08-19 20:29:37 +08:00
:说明:
2020-08-18 17:24:49 +08:00
获取全局 Driver 对象可用于在计划任务的回调中获取当前 Driver 对象
2020-08-19 20:29:37 +08:00
:返回:
2020-08-18 17:24:49 +08:00
* ``Driver``: 全局 Driver 对象
2020-08-19 20:29:37 +08:00
:异常:
2020-08-18 17:24:49 +08:00
* ``ValueError``: 全局 Driver 对象尚未初始化 (nonebot.init 尚未调用)
2020-08-19 20:29:37 +08:00
:用法:
2020-08-18 17:24:49 +08:00
.. code-block:: python
driver = nonebot.get_driver()
"""
2020-07-04 22:51:10 +08:00
if _driver is None:
raise ValueError("NoneBot has not been initialized.")
return _driver
def get_app():
2020-08-18 17:24:49 +08:00
"""
2020-08-19 20:29:37 +08:00
:说明:
2020-08-18 17:24:49 +08:00
获取全局 Driver 对应 Server App 对象
2020-08-19 20:29:37 +08:00
:返回:
2020-08-18 17:24:49 +08:00
* ``Any``: Server App 对象
2020-08-19 20:29:37 +08:00
:异常:
2020-08-18 17:24:49 +08:00
* ``ValueError``: 全局 Driver 对象尚未初始化 (nonebot.init 尚未调用)
2020-08-19 20:29:37 +08:00
:用法:
2020-08-18 17:24:49 +08:00
.. code-block:: python
app = nonebot.get_app()
"""
2020-07-04 22:51:10 +08:00
driver = get_driver()
return driver.server_app
def get_asgi():
2020-08-18 17:24:49 +08:00
"""
2020-08-19 20:29:37 +08:00
:说明:
2020-08-18 17:24:49 +08:00
获取全局 Driver 对应 Asgi 对象
2020-08-19 20:29:37 +08:00
:返回:
2020-08-18 17:24:49 +08:00
* ``Any``: Asgi 对象
2020-08-19 20:29:37 +08:00
:异常:
2020-08-18 17:24:49 +08:00
* ``ValueError``: 全局 Driver 对象尚未初始化 (nonebot.init 尚未调用)
2020-08-19 20:29:37 +08:00
:用法:
2020-08-18 17:24:49 +08:00
.. code-block:: python
asgi = nonebot.get_asgi()
"""
2020-07-04 22:51:10 +08:00
driver = get_driver()
return driver.asgi
2020-08-27 16:43:58 +08:00
def get_bots() -> Union[NoReturn, Dict[str, Bot]]:
2020-08-18 17:24:49 +08:00
"""
2020-08-19 20:29:37 +08:00
:说明:
2020-08-18 17:24:49 +08:00
获取所有通过 ws 连接 NoneBot Bot 对象
2020-08-19 20:29:37 +08:00
:返回:
2020-08-18 17:24:49 +08:00
* ``Dict[str, Bot]``: 一个以字符串 ID 为键Bot 对象为值的字典
2020-08-19 20:29:37 +08:00
:异常:
2020-08-18 17:24:49 +08:00
* ``ValueError``: 全局 Driver 对象尚未初始化 (nonebot.init 尚未调用)
2020-08-19 20:29:37 +08:00
:用法:
2020-08-18 17:24:49 +08:00
.. code-block:: python
bots = nonebot.get_bots()
"""
2020-08-15 17:47:50 +08:00
driver = get_driver()
return driver.bots
2020-09-12 13:34:36 +08:00
from nonebot.sched import scheduler
2020-08-17 16:09:41 +08:00
from nonebot.config import Env, Config
2020-08-27 13:27:42 +08:00
from nonebot.log import logger, default_filter
2020-08-17 16:09:41 +08:00
from nonebot.adapters.cqhttp import Bot as CQBot
2020-08-15 17:47:50 +08:00
try:
import nonebot_test
except ImportError:
nonebot_test = None
def init(*, _env_file: Optional[str] = None, **kwargs):
2020-08-18 17:24:49 +08:00
"""
2020-08-19 20:29:37 +08:00
:说明:
2020-08-18 17:24:49 +08:00
初始化 NoneBot 以及 全局 Driver 对象
2020-08-19 20:29:37 +08:00
2020-08-18 17:24:49 +08:00
NoneBot 将会从 .env 文件中读取环境信息并使用相应的 env 文件配置
2020-08-19 20:29:37 +08:00
2020-08-18 17:24:49 +08:00
你也可以传入自定义的 _env_file 来指定 NoneBot 从该文件读取配置
2020-08-19 20:29:37 +08:00
:参数:
2020-08-18 17:24:49 +08:00
* ``_env_file: Optional[str]``: 配置文件名默认从 .env.{env_name} 中读取配置
* ``**kwargs``: 任意变量将会存储到 Config 对象里
2020-08-19 20:29:37 +08:00
:返回:
2020-08-18 17:24:49 +08:00
2020-09-13 13:01:23 +08:00
- ``None``
2020-08-18 17:24:49 +08:00
2020-08-19 20:29:37 +08:00
:用法:
2020-08-18 17:24:49 +08:00
.. code-block:: python
nonebot.init(database=Database(...))
"""
2020-07-04 22:51:10 +08:00
global _driver
2020-08-27 16:43:58 +08:00
if not _driver:
2020-09-15 14:48:15 +08:00
logger.info("NoneBot is initializing...")
2020-08-27 16:43:58 +08:00
env = Env()
logger.opt(
2020-09-15 14:48:15 +08:00
colors=True).info(f"Current <y><b>Env: {env.environment}</b></y>")
2020-08-27 16:43:58 +08:00
config = Config(**kwargs,
_env_file=_env_file or f".env.{env.environment}")
2020-07-04 22:51:10 +08:00
2020-08-27 16:43:58 +08:00
default_filter.level = "DEBUG" if config.debug else "INFO"
logger.opt(
colors=True).debug(f"Loaded <y><b>Config</b></y>: {config.dict()}")
2020-07-04 22:51:10 +08:00
2020-08-27 16:43:58 +08:00
DriverClass: Type[Driver] = getattr(
importlib.import_module(config.driver), "Driver")
_driver = DriverClass(env, config)
2020-07-04 22:51:10 +08:00
2020-08-27 16:43:58 +08:00
# register build-in adapters
_driver.register_adapter("cqhttp", CQBot)
2020-08-13 15:23:04 +08:00
2020-08-27 16:43:58 +08:00
# load nonebot test frontend if debug
if config.debug and nonebot_test:
logger.debug("Loading nonebot test frontend...")
nonebot_test.init()
2020-08-13 18:16:59 +08:00
2020-09-12 13:34:36 +08:00
if scheduler:
_driver.on_startup(_start_scheduler)
2020-07-04 22:51:10 +08:00
2020-08-18 17:24:49 +08:00
def run(host: Optional[str] = None,
2020-07-04 22:51:10 +08:00
port: Optional[int] = None,
*args,
**kwargs):
2020-08-18 17:24:49 +08:00
"""
2020-08-19 20:29:37 +08:00
:说明:
2020-08-18 17:24:49 +08:00
启动 NoneBot即运行全局 Driver 对象
2020-08-19 20:29:37 +08:00
:参数:
2020-08-18 17:24:49 +08:00
* ``host: Optional[str]``: 主机名IP若不传入则使用配置文件中指定的值
* ``port: Optional[int]``: 端口若不传入则使用配置文件中指定的值
* ``*args``: 传入 Driver.run 的位置参数
* ``**kwargs``: 传入 Driver.run 的命名参数
2020-08-19 20:29:37 +08:00
:返回:
2020-08-18 17:24:49 +08:00
2020-09-13 13:01:23 +08:00
- ``None``
2020-08-18 17:24:49 +08:00
2020-08-19 20:29:37 +08:00
:用法:
2020-08-18 17:24:49 +08:00
.. code-block:: python
nonebot.run(host="127.0.0.1", port=8080)
"""
2020-08-27 16:43:58 +08:00
logger.info("Running NoneBot...")
2020-07-04 22:51:10 +08:00
get_driver().run(host, port, *args, **kwargs)
2020-06-30 10:13:58 +08:00
2020-09-12 13:34:36 +08:00
async def _start_scheduler():
if scheduler and not scheduler.running:
scheduler.configure(_driver.config.apscheduler_config)
scheduler.start()
logger.opt(colors=True).info("<y>Scheduler Started</y>")
from nonebot.plugin import on_message, on_notice, on_request, on_metaevent
from nonebot.plugin import on_startswith, on_endswith, on_command, on_regex
from nonebot.plugin import load_plugin, load_plugins, load_builtin_plugins, get_loaded_plugins