pbf.statement
Statement使用方法
您可以通过pbf.statement.Statement快速构建新的Statement,仅需要通过继承pbf.statement.Statement类
下面是一个使用例子
from pbf.statement import Statement
class VideoStatement(Statement):
def __init__(self, id: int, video_url: str) -> None:
super().__init__("video", **{"video_url": video_url, "id": id})
# 或者像下面这样定义(更直观,但是IDE可能会要求你调用超类的__init__方法)
class VideoStatement(Statement):
cqtype: str = "video"
id: int = None
video_url: str = None
def __init__(self, id: int, video_url: str) -> None:
self.id = id
self.video_url = video_url
然后我们可以在Msg中传入该Statement进行发送
from pbf.controller.Client import Msg
from pbf.statement.TextStatement import TextStatement
from pbf.statement.FaceStatement import FaceStatement
from path.to.your.statement import VideoStatement
msg = [
TextStatement("这是一段文字"),
FaceStatement(114), # 这是一个表情,id为151
VideoStatement(514, "https://example.com")
]
Msg(*msg).send_to(group_id=114514)
这样做会发送这样的一段消息:
这是一段文字[CQ:face,id=114][CQ:video,id=514,video_url=https://example.com]
为FaceStatement适配Lagrange
如果你在使用Lagrange.OneBot,那你可能需要对FaceStatement进行一些修改来适配Lagrange
FaceStatement的构造是这样的[CQ:face,id=xxx],其中xxx为int类型,指明表情的ID。
这样做在go-cqhttp中没有任何问题,但是在Lagrange中会引起JSON解析错误,您需要覆写将id类型替换为str
from pbf.statement import Statement
class FaceStatement(Statement):
id: str = None
cqtype = "face"
def __init__(self, id: int):
self.id = str(id)
1""" 2# Statement使用方法 3您可以通过pbf.statement.Statement快速构建新的Statement,仅需要通过继承pbf.statement.Statement类 4 5下面是一个使用例子 6```python 7from pbf.statement import Statement 8 9class VideoStatement(Statement): 10 def __init__(self, id: int, video_url: str) -> None: 11 super().__init__("video", **{"video_url": video_url, "id": id}) 12 13# 或者像下面这样定义(更直观,但是IDE可能会要求你调用超类的__init__方法) 14 15class VideoStatement(Statement): 16 cqtype: str = "video" 17 id: int = None 18 video_url: str = None 19 20 def __init__(self, id: int, video_url: str) -> None: 21 self.id = id 22 self.video_url = video_url 23``` 24然后我们可以在Msg中传入该Statement进行发送 25```python 26from pbf.controller.Client import Msg 27from pbf.statement.TextStatement import TextStatement 28from pbf.statement.FaceStatement import FaceStatement 29from path.to.your.statement import VideoStatement 30 31msg = [ 32 TextStatement("这是一段文字"), 33 FaceStatement(114), # 这是一个表情,id为151 34 VideoStatement(514, "https://example.com") 35] 36Msg(*msg).send_to(group_id=114514) 37``` 38这样做会发送这样的一段消息: 39```text 40这是一段文字[CQ:face,id=114][CQ:video,id=514,video_url=https://example.com] 41``` 42 43 44# 为FaceStatement适配Lagrange 45如果你在使用Lagrange.OneBot,那你可能需要对FaceStatement进行一些修改来适配Lagrange 46 47FaceStatement的构造是这样的`[CQ:face,id=xxx]`,其中`xxx`为`int`类型,指明表情的ID。<br> 48这样做在go-cqhttp中没有任何问题,但是在Lagrange中会引起JSON解析错误,您需要覆写将`id`类型替换为`str` <br> 49```python 50from pbf.statement import Statement 51 52class FaceStatement(Statement): 53 id: str = None 54 cqtype = "face" 55 56 def __init__(self, id: int): 57 self.id = str(id) 58``` 59""" 60 61class Statement: 62 cqtype: str = None 63 statementFlag: bool = False 64 65 def __init__(self, cqtype: str, **kwargs) -> None: 66 """ 67 初始化Statement对象。 68 :param type: str CQ类型 69 :param kwargs: **dict CQ数据 70 """ 71 self.cqtype = cqtype 72 for i in kwargs: 73 setattr(self, i, kwargs[i]) 74 75 def get(self) -> dict: 76 """ 77 获取Statement对象转换为消息链的数据。 78 :return: dict 79 """ 80 arr = {'type': self.cqtype, 'data': {}} 81 for i in dir(self): 82 if not i.startswith('__') and not callable(getattr(self, i)) and i != 'cqtype': 83 arr['data'][i] = getattr(self, i) 84 85 return arr 86 87 def set(self, data: dict): 88 """ 89 设置Statement对象。 90 The structure of the message chain data: 91 { 92 "type": "cqtype", 93 "data": { 94 "key": "value" 95 } 96 } 97 :param data: dict 消息链数据 98 :return: 99 """ 100 self.cqtype = data.get('type') 101 data = data.get('data') 102 for key, value in data.items(): 103 setattr(self, key, value) 104 return self 105 106 def __str__(self): 107 return str(self.get()) 108 109 110if __name__ == '__main__': 111 stat = Statement('at', qq=123456789) 112 print(stat.get())
class
Statement:
62class Statement: 63 cqtype: str = None 64 statementFlag: bool = False 65 66 def __init__(self, cqtype: str, **kwargs) -> None: 67 """ 68 初始化Statement对象。 69 :param type: str CQ类型 70 :param kwargs: **dict CQ数据 71 """ 72 self.cqtype = cqtype 73 for i in kwargs: 74 setattr(self, i, kwargs[i]) 75 76 def get(self) -> dict: 77 """ 78 获取Statement对象转换为消息链的数据。 79 :return: dict 80 """ 81 arr = {'type': self.cqtype, 'data': {}} 82 for i in dir(self): 83 if not i.startswith('__') and not callable(getattr(self, i)) and i != 'cqtype': 84 arr['data'][i] = getattr(self, i) 85 86 return arr 87 88 def set(self, data: dict): 89 """ 90 设置Statement对象。 91 The structure of the message chain data: 92 { 93 "type": "cqtype", 94 "data": { 95 "key": "value" 96 } 97 } 98 :param data: dict 消息链数据 99 :return: 100 """ 101 self.cqtype = data.get('type') 102 data = data.get('data') 103 for key, value in data.items(): 104 setattr(self, key, value) 105 return self 106 107 def __str__(self): 108 return str(self.get())
Statement(cqtype: str, **kwargs)
66 def __init__(self, cqtype: str, **kwargs) -> None: 67 """ 68 初始化Statement对象。 69 :param type: str CQ类型 70 :param kwargs: **dict CQ数据 71 """ 72 self.cqtype = cqtype 73 for i in kwargs: 74 setattr(self, i, kwargs[i])
初始化Statement对象。
Parameters
- type: str CQ类型
- kwargs: **dict CQ数据
def
get(self) -> dict:
76 def get(self) -> dict: 77 """ 78 获取Statement对象转换为消息链的数据。 79 :return: dict 80 """ 81 arr = {'type': self.cqtype, 'data': {}} 82 for i in dir(self): 83 if not i.startswith('__') and not callable(getattr(self, i)) and i != 'cqtype': 84 arr['data'][i] = getattr(self, i) 85 86 return arr
获取Statement对象转换为消息链的数据。
Returns
dict
def
set(self, data: dict):
88 def set(self, data: dict): 89 """ 90 设置Statement对象。 91 The structure of the message chain data: 92 { 93 "type": "cqtype", 94 "data": { 95 "key": "value" 96 } 97 } 98 :param data: dict 消息链数据 99 :return: 100 """ 101 self.cqtype = data.get('type') 102 data = data.get('data') 103 for key, value in data.items(): 104 setattr(self, key, value) 105 return self
设置Statement对象。 The structure of the message chain data: { "type": "cqtype", "data": { "key": "value" } }
Parameters
- data: dict 消息链数据