mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-12-18 17:35:46 +08:00
57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
|
from nonebot.typing import NoReturn
|
|||
|
from nonebot.typing import Union, Optional
|
|||
|
from nonebot.exception import RequestDenied
|
|||
|
from nonebot.utils import logger_wrapper
|
|||
|
|
|||
|
log = logger_wrapper("CQHTTP")
|
|||
|
|
|||
|
|
|||
|
def get_auth_bearer(
|
|||
|
access_token: Optional[str] = None) -> Union[Optional[str], NoReturn]:
|
|||
|
if not access_token:
|
|||
|
return None
|
|||
|
scheme, _, param = access_token.partition(" ")
|
|||
|
if scheme.lower() not in ["bearer", "token"]:
|
|||
|
raise RequestDenied(401, "Not authenticated")
|
|||
|
return param
|
|||
|
|
|||
|
|
|||
|
def escape(s: str, *, escape_comma: bool = True) -> str:
|
|||
|
"""
|
|||
|
:说明:
|
|||
|
|
|||
|
对字符串进行 CQ 码转义。
|
|||
|
|
|||
|
:参数:
|
|||
|
|
|||
|
* ``s: str``: 需要转义的字符串
|
|||
|
* ``escape_comma: bool``: 是否转义逗号(``,``)。
|
|||
|
"""
|
|||
|
s = s.replace("&", "&") \
|
|||
|
.replace("[", "[") \
|
|||
|
.replace("]", "]")
|
|||
|
if escape_comma:
|
|||
|
s = s.replace(",", ",")
|
|||
|
return s
|
|||
|
|
|||
|
|
|||
|
def unescape(s: str) -> str:
|
|||
|
"""
|
|||
|
:说明:
|
|||
|
|
|||
|
对字符串进行 CQ 码去转义。
|
|||
|
|
|||
|
:参数:
|
|||
|
|
|||
|
* ``s: str``: 需要转义的字符串
|
|||
|
"""
|
|||
|
return s.replace(",", ",") \
|
|||
|
.replace("[", "[") \
|
|||
|
.replace("]", "]") \
|
|||
|
.replace("&", "&")
|
|||
|
|
|||
|
|
|||
|
def _b2s(b: Optional[bool]) -> Optional[str]:
|
|||
|
"""转换布尔值为字符串。"""
|
|||
|
return b if b is None else str(b).lower()
|