nonebot2/nonebot/__init__.py
2020-08-13 18:18:00 +08:00

65 lines
1.5 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import importlib
from ipaddress import IPv4Address
from nonebot.log import logger
from nonebot.config import Env, Config
from nonebot.drivers import BaseDriver
from nonebot.adapters.cqhttp import Bot as CQBot
from nonebot.typing import Union, Optional, NoReturn
try:
import nonebot_test
except ImportError:
nonebot_test = None
_driver: Optional[BaseDriver] = None
def get_driver() -> Union[NoReturn, BaseDriver]:
if _driver is None:
raise ValueError("NoneBot has not been initialized.")
return _driver
def get_app():
driver = get_driver()
return driver.server_app
def get_asgi():
driver = get_driver()
return driver.asgi
def init(*, _env_file: Optional[str] = None, **kwargs):
global _driver
env = Env()
config = Config(**kwargs, _env_file=_env_file or f".env.{env.environment}")
logger.setLevel(logging.DEBUG if config.debug else logging.INFO)
logger.debug(f"Loaded config: {config.dict()}")
Driver = getattr(importlib.import_module(config.driver), "Driver")
_driver = Driver(env, config)
# register build-in adapters
_driver.register_adapter("cqhttp", CQBot)
# load nonebot test frontend if debug
if config.debug and nonebot_test:
nonebot_test.init()
def run(host: Optional[IPv4Address] = None,
port: Optional[int] = None,
*args,
**kwargs):
get_driver().run(host, port, *args, **kwargs)
from nonebot.plugin import load_plugins, get_loaded_plugins