Skip to content

Commit 443faea

Browse files
committed
ErisPulse的基类进行了getattr重写,简化适配器的逻辑
1 parent 0edb482 commit 443faea

1 file changed

Lines changed: 0 additions & 61 deletions

File tree

OneBotAdapter/Core.py

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -37,72 +37,11 @@ class OneBotAdapter(sdk.BaseAdapter):
3737
class Send(sdk.BaseAdapter.Send):
3838
"""消息发送DSL实现"""
3939

40-
# 方法名映射表(全小写 -> 实际方法名)
41-
_METHOD_MAP = {
42-
# 消息发送方法
43-
"text": "Text",
44-
"image": "Image",
45-
"voice": "Voice",
46-
"video": "Video",
47-
"face": "Face",
48-
"file": "File",
49-
50-
# 批量和其他方法
51-
"recall": "Recall",
52-
53-
# 原始消息和转换
54-
"raw_ob12": "Raw_ob12",
55-
56-
# 链式修饰方法
57-
"at": "At",
58-
"atall": "AtAll",
59-
"reply": "Reply",
60-
}
61-
6240
def __init__(self, adapter, target_type=None, target_id=None, account_id=None):
6341
super().__init__(adapter, target_type, target_id, account_id)
6442
self._at_user_ids = [] # @的用户列表
6543
self._reply_message_id = None # 回复的消息ID
6644
self._at_all = False # 是否@全体
67-
68-
def __getattr__(self, name):
69-
"""
70-
处理未定义的发送方法(支持大小写不敏感)
71-
72-
当调用不存在的消息类型方法时:
73-
1. 通过映射表查找对应的方法
74-
2. 如果找到则调用该方法
75-
3. 如果找不到,则发送文本提示不支持
76-
"""
77-
name_lower = name.lower()
78-
79-
# 查找映射
80-
if name_lower in self._METHOD_MAP:
81-
actual_method_name = self._METHOD_MAP[name_lower]
82-
return getattr(self, actual_method_name)
83-
84-
# 方法不存在,返回文本提示
85-
def unsupported_method(*args, **kwargs):
86-
# 格式化参数信息
87-
params_info = []
88-
for i, arg in enumerate(args):
89-
if isinstance(arg, bytes):
90-
params_info.append(f"args[{i}]: <bytes: {len(arg)} bytes>")
91-
else:
92-
params_info.append(f"args[{i}]: {repr(arg)[:100]}")
93-
94-
for k, v in kwargs.items():
95-
if isinstance(v, bytes):
96-
params_info.append(f"{k}: <bytes: {len(v)} bytes>")
97-
else:
98-
params_info.append(f"{k}: {repr(v)[:100]}")
99-
100-
params_str = ", ".join(params_info)
101-
error_msg = f"[不支持的发送类型] 方法名: {name}, 参数: [{params_str}]"
102-
103-
return self.Text(error_msg)
104-
105-
return unsupported_method
10645

10746
def _get_msg_type_by_filetype(self, file: Union[str, bytes]) -> str:
10847
"""

0 commit comments

Comments
 (0)