mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-29 17:27:41 +00:00
⚗️ seperate configs
This commit is contained in:
@ -7,7 +7,6 @@ from typing import Any, Dict, Union, Optional, TYPE_CHECKING
|
||||
|
||||
import httpx
|
||||
from nonebot.log import logger
|
||||
from nonebot.config import Config
|
||||
from nonebot.typing import overrides
|
||||
from nonebot.message import handle_event
|
||||
from nonebot.adapters import Bot as BaseBot
|
||||
@ -20,6 +19,7 @@ from .event import Reply, Event, MessageEvent, get_event_model
|
||||
from .exception import NetworkError, ApiNotAvailable, ActionFailed
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nonebot.config import Config
|
||||
from nonebot.drivers import Driver, WebSocket
|
||||
|
||||
|
||||
@ -218,22 +218,15 @@ class Bot(BaseBot):
|
||||
"""
|
||||
CQHTTP 协议 Bot 适配。继承属性参考 `BaseBot <./#class-basebot>`_ 。
|
||||
"""
|
||||
cqhttp_config: CQHTTPConfig
|
||||
|
||||
def __init__(self,
|
||||
driver: "Driver",
|
||||
connection_type: str,
|
||||
config: Config,
|
||||
self_id: str,
|
||||
*,
|
||||
websocket: Optional["WebSocket"] = None):
|
||||
|
||||
self.cqhttp_config = CQHTTPConfig(**config.dict())
|
||||
|
||||
super().__init__(driver,
|
||||
connection_type,
|
||||
config,
|
||||
self_id,
|
||||
websocket=websocket)
|
||||
super().__init__(connection_type, self_id, websocket=websocket)
|
||||
|
||||
@property
|
||||
@overrides(BaseBot)
|
||||
@ -243,6 +236,11 @@ class Bot(BaseBot):
|
||||
"""
|
||||
return "cqhttp"
|
||||
|
||||
@classmethod
|
||||
def register(cls, driver: "Driver", config: "Config"):
|
||||
super().register(driver, config)
|
||||
cls.cqhttp_config = CQHTTPConfig(**config.dict())
|
||||
|
||||
@classmethod
|
||||
@overrides(BaseBot)
|
||||
async def check_permission(cls, driver: "Driver", connection_type: str,
|
||||
@ -268,7 +266,7 @@ class Bot(BaseBot):
|
||||
raise RequestDenied(400, "Missing X-Self-ID Header")
|
||||
|
||||
# 检查签名
|
||||
secret = cqhttp_config.cqhttp_secret
|
||||
secret = cqhttp_config.secret
|
||||
if secret and connection_type == "http":
|
||||
if not x_signature:
|
||||
log("WARNING", "Missing Signature Header")
|
||||
@ -279,7 +277,7 @@ class Bot(BaseBot):
|
||||
log("WARNING", "Signature Header is invalid")
|
||||
raise RequestDenied(403, "Signature is invalid")
|
||||
|
||||
access_token = cqhttp_config.cqhttp_access_token
|
||||
access_token = cqhttp_config.access_token
|
||||
if access_token and access_token != token:
|
||||
log(
|
||||
"WARNING", "Authorization Header is invalid"
|
||||
@ -378,9 +376,9 @@ class Bot(BaseBot):
|
||||
api_root += "/"
|
||||
|
||||
headers = {}
|
||||
if self.cqhttp_config.cqhttp_access_token is not None:
|
||||
if self.cqhttp_config.access_token is not None:
|
||||
headers[
|
||||
"Authorization"] = "Bearer " + self.cqhttp_config.cqhttp_access_token
|
||||
"Authorization"] = "Bearer " + self.cqhttp_config.access_token
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(headers=headers) as client:
|
||||
|
Reference in New Issue
Block a user