2020-10-08 21:36:57 +08:00
|
|
|
|
"""
|
|
|
|
|
快捷导入
|
|
|
|
|
========
|
|
|
|
|
|
|
|
|
|
为方便使用,``nonebot`` 模块从子模块导入了部分内容
|
|
|
|
|
|
|
|
|
|
- ``on_message`` => ``nonebot.plugin.on_message``
|
|
|
|
|
- ``on_notice`` => ``nonebot.plugin.on_notice``
|
|
|
|
|
- ``on_request`` => ``nonebot.plugin.on_request``
|
|
|
|
|
- ``on_metaevent`` => ``nonebot.plugin.on_metaevent``
|
|
|
|
|
- ``on_startswith`` => ``nonebot.plugin.on_startswith``
|
|
|
|
|
- ``on_endswith`` => ``nonebot.plugin.on_endswith``
|
2020-11-10 14:29:50 +08:00
|
|
|
|
- ``on_keyword`` => ``nonebot.plugin.on_keyword``
|
2020-10-08 21:36:57 +08:00
|
|
|
|
- ``on_command`` => ``nonebot.plugin.on_command``
|
2021-02-02 12:15:20 +08:00
|
|
|
|
- ``on_shell_command`` => ``nonebot.plugin.on_shell_command``
|
2020-10-08 21:36:57 +08:00
|
|
|
|
- ``on_regex`` => ``nonebot.plugin.on_regex``
|
|
|
|
|
- ``CommandGroup`` => ``nonebot.plugin.CommandGroup``
|
2020-12-05 01:43:58 +08:00
|
|
|
|
- ``Matchergroup`` => ``nonebot.plugin.MatcherGroup``
|
2020-10-08 21:36:57 +08:00
|
|
|
|
- ``load_plugin`` => ``nonebot.plugin.load_plugin``
|
|
|
|
|
- ``load_plugins`` => ``nonebot.plugin.load_plugins``
|
2021-02-19 15:15:46 +08:00
|
|
|
|
- ``load_all_plugins`` => ``nonebot.plugin.load_all_plugins``
|
2021-02-24 17:48:08 +08:00
|
|
|
|
- ``load_from_json`` => ``nonebot.plugin.load_from_json``
|
|
|
|
|
- ``load_from_toml`` => ``nonebot.plugin.load_from_toml``
|
2020-10-08 21:36:57 +08:00
|
|
|
|
- ``load_builtin_plugins`` => ``nonebot.plugin.load_builtin_plugins``
|
2020-11-21 20:50:33 +08:00
|
|
|
|
- ``get_plugin`` => ``nonebot.plugin.get_plugin``
|
2020-10-08 21:36:57 +08:00
|
|
|
|
- ``get_loaded_plugins`` => ``nonebot.plugin.get_loaded_plugins``
|
2020-11-21 20:50:33 +08:00
|
|
|
|
- ``export`` => ``nonebot.plugin.export``
|
|
|
|
|
- ``require`` => ``nonebot.plugin.require``
|
2020-10-08 21:36:57 +08:00
|
|
|
|
"""
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
2020-07-04 22:51:10 +08:00
|
|
|
|
import importlib
|
2020-11-15 16:18:23 +08:00
|
|
|
|
import pkg_resources
|
2020-12-06 02:30:19 +08:00
|
|
|
|
from typing import Dict, Type, Optional
|
|
|
|
|
|
2020-12-07 00:06:09 +08:00
|
|
|
|
from nonebot.adapters import Bot
|
|
|
|
|
from nonebot.drivers import Driver
|
2020-12-06 02:30:19 +08:00
|
|
|
|
from nonebot.utils import escape_tag
|
|
|
|
|
from nonebot.config import Env, Config
|
|
|
|
|
from nonebot.log import logger, default_filter
|
2020-07-04 22:51:10 +08:00
|
|
|
|
|
2021-02-22 10:44:42 +08:00
|
|
|
|
try:
|
|
|
|
|
_dist: pkg_resources.Distribution = pkg_resources.get_distribution(
|
|
|
|
|
"nonebot2")
|
|
|
|
|
__version__ = _dist.version
|
|
|
|
|
VERSION = _dist.parsed_version
|
|
|
|
|
except pkg_resources.DistributionNotFound:
|
|
|
|
|
__version__ = None
|
|
|
|
|
VERSION = None
|
2020-11-15 16:18:23 +08:00
|
|
|
|
|
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:
|
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-12-05 17:19:23 +08:00
|
|
|
|
def get_bots() -> 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-08-07 11:18:02 +08:00
|
|
|
|
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:
|
2021-04-19 21:15:10 +08:00
|
|
|
|
logger.success("NoneBot is initializing...")
|
2020-08-27 16:43:58 +08:00
|
|
|
|
env = Env()
|
|
|
|
|
config = Config(**kwargs,
|
2021-01-27 11:39:34 +08:00
|
|
|
|
_common_config=env.dict(),
|
2020-08-27 16:43:58 +08:00
|
|
|
|
_env_file=_env_file or f".env.{env.environment}")
|
2020-07-04 22:51:10 +08:00
|
|
|
|
|
2021-04-19 21:15:10 +08:00
|
|
|
|
default_filter.level = (
|
|
|
|
|
"DEBUG" if config.debug else
|
|
|
|
|
"INFO") if config.log_level is None else config.log_level
|
|
|
|
|
logger.opt(
|
|
|
|
|
colors=True).info(f"Current <y><b>Env: {env.environment}</b></y>")
|
2020-09-30 18:01:31 +08:00
|
|
|
|
logger.opt(colors=True).debug(
|
|
|
|
|
f"Loaded <y><b>Config</b></y>: {escape_tag(str(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-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)
|
|
|
|
|
|
|
|
|
|
"""
|
2021-04-19 21:15:10 +08:00
|
|
|
|
logger.success("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-12-04 01:55:03 +08:00
|
|
|
|
from nonebot.plugin import on_message, on_notice, on_request, on_metaevent, CommandGroup, MatcherGroup
|
2021-02-02 11:59:14 +08:00
|
|
|
|
from nonebot.plugin import on_startswith, on_endswith, on_keyword, on_command, on_shell_command, on_regex
|
2021-02-19 15:15:46 +08:00
|
|
|
|
from nonebot.plugin import load_plugin, load_plugins, load_all_plugins, load_builtin_plugins
|
2021-02-24 17:48:08 +08:00
|
|
|
|
from nonebot.plugin import load_from_json, load_from_toml
|
2020-11-21 20:40:09 +08:00
|
|
|
|
from nonebot.plugin import export, require, get_plugin, get_loaded_plugins
|