async process msg

This commit is contained in:
XYenon 2022-05-28 16:14:48 +08:00
parent b86e558312
commit d97a2ab2bf

View File

@ -1,6 +1,5 @@
import asyncio import asyncio
import copy import copy
import functools
import logging import logging
import tempfile import tempfile
import threading import threading
@ -189,71 +188,70 @@ class GoCQHttp(BaseClient):
@self.coolq_bot.on_message @self.coolq_bot.on_message
async def handle_msg(context: Event): async def handle_msg(context: Event):
self.logger.debug(repr(context)) async def _handle_msg():
msg_elements = context["message"] self.logger.debug(repr(context))
qq_uid = context["user_id"] msg_elements = context["message"]
chat: Chat qq_uid = context["user_id"]
author: ChatMember chat: Chat
author: ChatMember
user = await self.get_user_info(qq_uid) user = await self.get_user_info(qq_uid)
if context["message_type"] == "private": if context["message_type"] == "private":
context["alias"] = user["remark"] context["alias"] = user["remark"]
chat: PrivateChat = await self.chat_manager.build_efb_chat_as_private(context) chat: PrivateChat = await self.chat_manager.build_efb_chat_as_private(context)
else:
chat = await self.chat_manager.build_efb_chat_as_group(context)
if "anonymous" not in context or context["anonymous"] is None:
if context["message_type"] == "group":
if context["sub_type"] == "notice":
context["event_description"] = "System Notification"
context["uid_prefix"] = "group_notification"
author = chat.add_system_member(
name=context["event_description"],
uid=ChatID("__{context[uid_prefix]}__".format(context=context)),
)
else:
user = await self.get_user_info(qq_uid, group_id=context["group_id"])
context["nickname"] = user["remark"]
if user["is_in_group"]:
context["alias"] = user["in_group_info"]["card"]
else:
context["alias"] = user["remark"]
author = await self.chat_manager.build_or_get_efb_member(chat, context)
elif context["message_type"] == "private":
author = chat.other
else: else:
author = await self.chat_manager.build_or_get_efb_member(chat, context) chat = await self.chat_manager.build_efb_chat_as_group(context)
else: # anonymous user in group
author = self.chat_manager.build_efb_chat_as_anonymous_user(chat, context)
main_text, messages, at_dict = await message_elements_wrapper(context, msg_elements, chat) if "anonymous" not in context or context["anonymous"] is None:
if context["message_type"] == "group":
if context["sub_type"] == "notice":
context["event_description"] = "System Notification"
context["uid_prefix"] = "group_notification"
author = chat.add_system_member(
name=context["event_description"],
uid=ChatID("__{context[uid_prefix]}__".format(context=context)),
)
else:
user = await self.get_user_info(qq_uid, group_id=context["group_id"])
context["nickname"] = user["remark"]
if user["is_in_group"]:
context["alias"] = user["in_group_info"]["card"]
else:
context["alias"] = user["remark"]
author = await self.chat_manager.build_or_get_efb_member(chat, context)
elif context["message_type"] == "private":
author = chat.other
else:
author = await self.chat_manager.build_or_get_efb_member(chat, context)
else: # anonymous user in group
author = self.chat_manager.build_efb_chat_as_anonymous_user(chat, context)
if main_text != "": main_text, messages, at_dict = await message_elements_wrapper(context, msg_elements, chat)
messages.append(self.msg_decorator.qq_text_simple_wrapper(main_text, at_dict))
coolq_msg_id = context["message_id"]
for i in range(len(messages)):
if not isinstance(messages[i], Message):
continue
efb_msg: Message = messages[i]
efb_msg.uid = (
f"{chat.uid.split('_')[-1]}_{coolq_msg_id}_{i}"
if i > 0
else f"{chat.uid.split('_')[-1]}_{coolq_msg_id}"
)
efb_msg.chat = chat
efb_msg.author = author
# if qq_uid != '80000000':
# Append discuss group into group list if main_text != "":
if context["message_type"] == "discuss" and efb_msg.chat not in self.discuss_list: messages.append(self.msg_decorator.qq_text_simple_wrapper(main_text, at_dict))
self.discuss_list.append(efb_msg.chat) coolq_msg_id = context["message_id"]
for i in range(len(messages)):
if not isinstance(messages[i], Message):
continue
efb_msg: Message = messages[i]
efb_msg.uid = (
f"{chat.uid.split('_')[-1]}_{coolq_msg_id}_{i}"
if i > 0
else f"{chat.uid.split('_')[-1]}_{coolq_msg_id}"
)
efb_msg.chat = chat
efb_msg.author = author
# if qq_uid != '80000000':
efb_msg.deliver_to = coordinator.master # Append discuss group into group list
if context["message_type"] == "discuss" and efb_msg.chat not in self.discuss_list:
self.discuss_list.append(efb_msg.chat)
def send_message_wrapper(*args, **kwargs): efb_msg.deliver_to = coordinator.master
threading.Thread(target=async_send_messages_to_master, args=args, kwargs=kwargs).start() async_send_messages_to_master(efb_msg)
send_message_wrapper(efb_msg) asyncio.create_task(_handle_msg())
@self.coolq_bot.on_notice("group_increase") @self.coolq_bot.on_notice("group_increase")
async def handle_group_increase_msg(context: Event): async def handle_group_increase_msg(context: Event):
@ -355,47 +353,54 @@ class GoCQHttp(BaseClient):
@self.coolq_bot.on_notice("offline_file") @self.coolq_bot.on_notice("offline_file")
async def handle_offline_file_upload_msg(context: Event): async def handle_offline_file_upload_msg(context: Event):
context["event_description"] = "\u2139 Offline File Upload Event" async def _handle_offline_file_upload_msg():
context["uid_prefix"] = "offline_file" context["event_description"] = "\u2139 Offline File Upload Event"
file_info_msg = ("Filename: {file[name]}\n" "File size: {file[size]}").format(file=context["file"]) context["uid_prefix"] = "offline_file"
user = await self.get_user_info(context["user_id"]) file_info_msg = ("Filename: {file[name]}\n" "File size: {file[size]}").format(file=context["file"])
text = "{remark}({nickname}) uploaded a file to you\n" user = await self.get_user_info(context["user_id"])
text = text.format(remark=user["remark"], nickname=user["nickname"]) + file_info_msg text = "{remark}({nickname}) uploaded a file to you\n"
context["message"] = text text = text.format(remark=user["remark"], nickname=user["nickname"]) + file_info_msg
self.send_msg_to_master(context) context["message"] = text
param_dict = { self.send_msg_to_master(context)
"context": context, param_dict = {
"download_url": context["file"]["url"], "context": context,
} "download_url": context["file"]["url"],
self.loop.run_in_executor(None, functools.partial(self.async_download_file, **param_dict)) }
self.async_download_file(**param_dict)
asyncio.create_task(_handle_offline_file_upload_msg())
@self.coolq_bot.on_notice("group_upload") @self.coolq_bot.on_notice("group_upload")
async def handle_group_file_upload_msg(context: Event): async def handle_group_file_upload_msg(context: Event):
context["event_description"] = "\u2139 Group File Upload Event" async def _handle_group_file_upload_msg():
context["uid_prefix"] = "group_upload" context["event_description"] = "\u2139 Group File Upload Event"
original_group = await self.get_group_info(context["group_id"], False) context["uid_prefix"] = "group_upload"
group_name = context["group_id"] original_group = await self.get_group_info(context["group_id"], False)
if original_group is not None and "group_name" in original_group: group_name = context["group_id"]
group_name = original_group["group_name"] if original_group is not None and "group_name" in original_group:
group_name = original_group["group_name"]
file_info_msg = ("File ID: {file[id]}\n" "Filename: {file[name]}\n" "File size: {file[size]}").format( file_info_msg = ("File ID: {file[id]}\n" "Filename: {file[name]}\n" "File size: {file[size]}").format(
file=context["file"] file=context["file"]
) )
member_info = (await self.get_user_info(context["user_id"], group_id=context["group_id"]))["in_group_info"] member_info = (await self.get_user_info(context["user_id"], group_id=context["group_id"]))[
group_card = member_info["card"] if member_info["card"] != "" else member_info["nickname"] "in_group_info"
text = "{member_card}({context[user_id]}) uploaded a file to group({group_name})\n" ]
text = text.format(member_card=group_card, context=context, group_name=group_name) + file_info_msg group_card = member_info["card"] if member_info["card"] != "" else member_info["nickname"]
context["message"] = text text = "{member_card}({context[user_id]}) uploaded a file to group({group_name})\n"
await self.send_efb_group_notice(context) text = text.format(member_card=group_card, context=context, group_name=group_name) + file_info_msg
context["message"] = text
await self.send_efb_group_notice(context)
param_dict = { param_dict = {
"context": context, "context": context,
"group_id": context["group_id"], "group_id": context["group_id"],
"file_id": context["file"]["id"], "file_id": context["file"]["id"],
"busid": context["file"]["busid"], "busid": context["file"]["busid"],
} }
self.async_download_group_file(**param_dict)
threading.Thread(target=self.async_download_group_file, args=[], kwargs=param_dict).start() asyncio.create_task(_handle_group_file_upload_msg())
@self.coolq_bot.on_notice("friend_add") @self.coolq_bot.on_notice("friend_add")
async def handle_friend_add_msg(context: Event): async def handle_friend_add_msg(context: Event):