feat : 全局消息监听

This commit is contained in:
Wbucket
2026-09-30 18:39:25 +08:00
parent 5db01d429d
commit 3d5ee01873
+30 -5
View File
@@ -39,6 +39,7 @@ class BotWebsocket_ex(BotWebSocket):
def __init__(self, session: Session, _connection: ConnectionSession):
super().__init__(session, _connection)
self._parser['group_member_add'] = lambda aaw:self._connection.dispatch("group_member_add",aaw)
self._parser['group_message_create'] = lambda aaw:self._connection.dispatch("group_message_create",aaw)
class djtuWebsocket (Client) :
def __init__(
@@ -53,6 +54,8 @@ class djtuWebsocket (Client) :
self.q = queue.Queue()
super().__init__(intents, timeout, is_sandbox, log_config, log_format, log_level, bot_log, ext_handlers)
async def bot_connect(self, session):
"""
newConnect 启动一个新的连接,如果连接在监听过程中报错了,或者被远端关闭了链接,需要识别关闭的原因,能否继续 resume
@@ -73,10 +76,10 @@ class djtuWebsocket (Client) :
async def on_group_at_message_create(self, message: Message):
await self.parseCommand(message)
async def on_group_message_create(self,message:Message):
async def on_group_message_create(self,message):
# 目前不启用
# return self.parseGlobalMessage(message)
return
return await self.parseGlobalMessage(message)
async def on_group_member_add(self,payload):
# await message.reply(content="awdwa")
@@ -115,8 +118,30 @@ class djtuWebsocket (Client) :
await msg.reply(content=reply)
# 触发概率大,以后要做好解析防止刷屏
async def parseGlobalMessage(self,msg:Message):
await msg.reply(content="awda")
async def parseGlobalMessage(self,payload):
print(payload)
try :
msg = payload['d']
gid = msg['group_id']
content = msg['content']
except:
return
commands:dict[str,types.MethodType|types.FunctionType] = {
r'吃什么' : lambda :"是啊吃什么"
}
required = content
reply = ""
for cmd,func in commands.items():
if required.find(cmd) != -1:
try:
reply = func()
except:
reply = "服务异常"
break
if reply.__len__() == 0:
return
await self.api.post_group_message(gid,content=reply)
# await msg.reply(content="awda")
# 监听队列,接受其他线程的消息
async def mcloop(self):