ayaka.driver.ayakabot.utils

 1from typing import Optional
 2from ayaka import logger
 3
 4
 5def bool_to_str(b: Optional[bool]) -> Optional[str]:
 6    """转换布尔值为字符串。"""
 7    return b if b is None else str(b).lower()
 8
 9
10def escape(s: str, *, escape_comma: bool = True) -> str:
11    """
12    :说明:
13
14      对字符串进行 CQ 码转义。
15
16    :参数:
17
18      * ``s: str``: 需要转义的字符串
19      * ``escape_comma: bool``: 是否转义逗号(``,``)。
20    """
21    s = s.replace("&", "&").replace("[", "[").replace("]", "]")
22    if escape_comma:
23        s = s.replace(",", ",")
24    return s
25
26
27def unescape(s: str) -> str:
28    """
29    :说明:
30
31      对字符串进行 CQ 码去转义。
32
33    :参数:
34
35      * ``s: str``: 需要转义的字符串
36    """
37    return s.replace(",", ",").replace("[", "[").replace("]", "]").replace("&", "&")
38    
39
40
41# def safe_cqhttp_utf8(api, data):
42#     if api in ["send_msg", "send_group_msg", "send_private_msg"]:
43#         data["message"] = cqhttp_fuck(
44#             data["message"], [119, 121, 123, 125])
45
46#     if api == "send_group_forward_msg":
47#         # 对每个node都要fuck一遍
48#         nodes = data['messages']
49#         for node in nodes:
50#             node['data']['content'] = cqhttp_fuck(
51#                 node['data']['content'], [58, 60])
52#         data['messages'] = nodes
53
54#     return data
55
56
57# def cqhttp_fuck(msg, ban_range: list):
58#     """ 傻逼风控
59#         该长度其实也不单是根据encode utf8长度来判断,还要根据html转义前的utf8长度判断。。傻逼
60#         比如[占5个长度,其实相当于1个长度
61#     """
62#     # 获取utf8长度
63#     s = str(msg)
64#     s = unescape(s)
65#     s = s.encode('utf8')
66#     length = len(s)
67
68#     logger.opt(colors=True).debug(f"转义前utf8字符长度 <y>{length}</y>")
69
70#     if length in ban_range:
71#         return str(msg) + " "
72#     return msg
def bool_to_str(b: Union[bool, NoneType]) -> Union[str, NoneType]:
6def bool_to_str(b: Optional[bool]) -> Optional[str]:
7    """转换布尔值为字符串。"""
8    return b if b is None else str(b).lower()

转换布尔值为字符串。

def escape(s: str, *, escape_comma: bool = True) -> str:
11def escape(s: str, *, escape_comma: bool = True) -> str:
12    """
13    :说明:
14
15      对字符串进行 CQ 码转义。
16
17    :参数:
18
19      * ``s: str``: 需要转义的字符串
20      * ``escape_comma: bool``: 是否转义逗号(``,``)。
21    """
22    s = s.replace("&", "&amp;").replace("[", "&#91;").replace("]", "&#93;")
23    if escape_comma:
24        s = s.replace(",", "&#44;")
25    return s

:说明:

对字符串进行 CQ 码转义。

:参数:

  • s: str: 需要转义的字符串
  • escape_comma: bool: 是否转义逗号(,)。
def unescape(s: str) -> str:
28def unescape(s: str) -> str:
29    """
30    :说明:
31
32      对字符串进行 CQ 码去转义。
33
34    :参数:
35
36      * ``s: str``: 需要转义的字符串
37    """
38    return s.replace("&#44;", ",").replace("&#91;", "[").replace("&#93;", "]").replace("&amp;", "&")

:说明:

对字符串进行 CQ 码去转义。

:参数:

  • s: str: 需要转义的字符串