From e57d42d938b5970fe2664af100debaff270230e2 Mon Sep 17 00:00:00 2001 From: xtex Date: Sun, 10 Dec 2023 11:09:18 +0800 Subject: [PATCH 1/6] feat: init quick confirm (#1046) Signed-off-by: xtex --- config/config.toml.example | 1 + core/builtins/message/__init__.py | 4 +++- core/builtins/tasks.py | 2 +- core/builtins/utils/__init__.py | 3 ++- core/types/message/__init__.py | 6 ++++++ 5 files changed, 13 insertions(+), 3 deletions(-) diff --git a/config/config.toml.example b/config/config.toml.example index c836126b60..fe3c2a7cfb 100644 --- a/config/config.toml.example +++ b/config/config.toml.example @@ -31,6 +31,7 @@ debug = false cache_path = "./cache/" command_prefix = ["~", "~",] confirm_command = ["是", "对", "對", "yes", "Yes", "YES", "y", "Y",] +quick_confirm = true disabled_bots = locale = "zh_cn" timezone_offset = "+8" diff --git a/core/builtins/message/__init__.py b/core/builtins/message/__init__.py index 20ad7aac26..7b64d7541c 100644 --- a/core/builtins/message/__init__.py +++ b/core/builtins/message/__init__.py @@ -7,7 +7,7 @@ from core.builtins.message.internal import * from core.builtins.tasks import MessageTaskManager from core.builtins.temp import ExecutionLockList -from core.builtins.utils import confirm_command +from core.builtins.utils import confirm_command, quick_confirm from core.exceptions import WaitCancelException from core.types.message import MessageSession as MessageSessionT, MsgInfo, Session from core.utils.i18n import Locale @@ -52,6 +52,8 @@ async def wait_confirm(self, message_chain=None, quote=True, delete=True, append await send.delete() if result.as_display(text_only=True) in confirm_command: return True + if quick_confirm and result.is_quick_confirm(): + return True return False else: raise WaitCancelException diff --git a/core/builtins/tasks.py b/core/builtins/tasks.py index 57cac4424f..2a2a12683e 100644 --- a/core/builtins/tasks.py +++ b/core/builtins/tasks.py @@ -23,7 +23,7 @@ def add_task(cls, session: MessageSession, flag, all_=False, reply=None): Logger.debug(cls._list) @classmethod - def get_result(cls, session: MessageSession): + def get_result(cls, session: MessageSession) -> MessageSession: if 'result' in cls._list[session.target.target_id][session.target.sender_id][session]: return cls._list[session.target.target_id][session.target.sender_id][session]['result'] else: diff --git a/core/builtins/utils/__init__.py b/core/builtins/utils/__init__.py index 895a396c05..eb36408bae 100644 --- a/core/builtins/utils/__init__.py +++ b/core/builtins/utils/__init__.py @@ -3,6 +3,7 @@ confirm_command = Config('confirm_command', default=["是", "对", "對", "yes", "Yes", "YES", "y", "Y"]) +quick_confirm = Config('quick_confirm', default=True) command_prefix = Config('command_prefix', default=['~', '~']) # 消息前缀 @@ -10,4 +11,4 @@ class EnableDirtyWordCheck: status = False -__all__ = ["confirm_command", "command_prefix", "EnableDirtyWordCheck", "PrivateAssets", "Secret"] +__all__ = ["confirm_command", "quick_confirm", "command_prefix", "EnableDirtyWordCheck", "PrivateAssets", "Secret"] diff --git a/core/types/message/__init__.py b/core/types/message/__init__.py index bb24b16a1b..f8605a6668 100644 --- a/core/types/message/__init__.py +++ b/core/types/message/__init__.py @@ -201,6 +201,12 @@ async def check_native_permission(self): """ raise NotImplementedError + def is_quick_confirm(self) -> bool: + """ + 用于检查消息是否可用作快速确认事件。 + """ + return False + async def fake_forward_msg(self, nodelist): """ 用于发送假转发消息(QQ)。 From 7201820940d0c0e6edb33eedefe71aeff1ee7145 Mon Sep 17 00:00:00 2001 From: xtex Date: Sun, 10 Dec 2023 12:12:55 +0800 Subject: [PATCH 2/6] build(deps): update matrix-nio Signed-off-by: xtex --- poetry.lock | 11 +++++------ pyproject.toml | 2 +- requirements.txt | 6 +++--- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/poetry.lock b/poetry.lock index edbbd1af98..f9ba55db97 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2276,23 +2276,22 @@ python-dateutil = ">=2.7" [[package]] name = "matrix-nio" -version = "0.21.2" +version = "0.22.1" description = "A Python Matrix client library, designed according to sans I/O principles." optional = false python-versions = ">=3.8.0,<4.0.0" files = [ - {file = "matrix_nio-0.21.2-py3-none-any.whl", hash = "sha256:95bec84dd0d4eca4f0ce252d2f630d435edbc41bb405448d88916a3783479237"}, - {file = "matrix_nio-0.21.2.tar.gz", hash = "sha256:414301fc25662af3e3436de5b955980474c03b0f3001f1b041c49743ede73232"}, + {file = "matrix_nio-0.22.1-py3-none-any.whl", hash = "sha256:36a7175a41b145026db7f3bf004577aa8906d09ed8c53f276452ac06ed8635e4"}, + {file = "matrix_nio-0.22.1.tar.gz", hash = "sha256:65956252c516f0b42b359d5816fbb66e2617a1f2c02ae45f2730257b815656d8"}, ] [package.dependencies] aiofiles = ">=23.1.0,<24.0.0" aiohttp = ">=3.8.3,<4.0.0" aiohttp-socks = ">=0.7.0,<0.8.0" -future = ">=0.18.2,<0.19.0" h11 = ">=0.14.0,<0.15.0" h2 = ">=4.0.0,<5.0.0" -jsonschema = ">=4.4.0,<5.0.0" +jsonschema = ">=4.14.0,<5.0.0" pycryptodome = ">=3.10.1,<4.0.0" unpaddedbase64 = ">=2.1.0,<3.0.0" @@ -4525,4 +4524,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = "^3.8.1" -content-hash = "831fc1e45a738ba83e525178c24db42d6d0833fa726c6d80d3abcc9738f310ee" +content-hash = "45b7149aa43113d2c3d981764613294d4eb50b7be221047c6e8c1e3ce7351197" diff --git a/pyproject.toml b/pyproject.toml index 693a1aa106..751800832b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,7 +54,7 @@ pycryptodome = "^3.18.0" langconv = "^0.2.0" toml = "^0.10.2" khl-py = "^0.3.16" -matrix-nio = "^0.21.2" +matrix-nio = "^0.22.0" attrs = "^23.1.0" uvicorn = {extras = ["standard"], version = "^0.23.2"} pyjwt = {extras = ["crypto"], version = "^2.8.0"} diff --git a/requirements.txt b/requirements.txt index 03ecfcd63a..8b361dca23 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1145,9 +1145,9 @@ matplotlib==3.7.2 ; python_full_version >= "3.8.1" and python_full_version < "4. --hash=sha256:f081c03f413f59390a80b3e351cc2b2ea0205839714dbc364519bcf51f4b56ca \ --hash=sha256:fdbb46fad4fb47443b5b8ac76904b2e7a66556844f33370861b4788db0f8816a \ --hash=sha256:fdcd28360dbb6203fb5219b1a5658df226ac9bebc2542a9e8f457de959d713d0 -matrix-nio==0.21.2 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0" \ - --hash=sha256:414301fc25662af3e3436de5b955980474c03b0f3001f1b041c49743ede73232 \ - --hash=sha256:95bec84dd0d4eca4f0ce252d2f630d435edbc41bb405448d88916a3783479237 +matrix-nio==0.22.1 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0" \ + --hash=sha256:36a7175a41b145026db7f3bf004577aa8906d09ed8c53f276452ac06ed8635e4 \ + --hash=sha256:65956252c516f0b42b359d5816fbb66e2617a1f2c02ae45f2730257b815656d8 monotonic==1.6 ; python_full_version >= "3.8.1" and python_full_version < "4.0.0" \ --hash=sha256:3a55207bcfed53ddd5c5bae174524062935efed17792e9de2ad0205ce9ad63f7 \ --hash=sha256:68687e19a14f11f26d140dd5c86f3dba4bf5df58003000ed467e0e2a69bca96c From 4050cdcdbd6a8a757c5e96a6618e3a662c7ddfcb Mon Sep 17 00:00:00 2001 From: xtex Date: Sun, 10 Dec 2023 12:27:24 +0800 Subject: [PATCH 3/6] feat: initial quick confirm Signed-off-by: xtex --- bots/matrix/bot.py | 34 ++++++++++++++++++++----------- bots/matrix/message.py | 29 +++++++++++++++++++++++++- core/builtins/message/__init__.py | 2 +- core/types/message/__init__.py | 5 +++-- modules/wiki/wiki.py | 6 ++++-- 5 files changed, 58 insertions(+), 18 deletions(-) diff --git a/bots/matrix/bot.py b/bots/matrix/bot.py index f31413815c..a2df205ec3 100644 --- a/bots/matrix/bot.py +++ b/bots/matrix/bot.py @@ -9,7 +9,7 @@ from bots.matrix import client from bots.matrix.client import bot from bots.matrix.info import client_name -from bots.matrix.message import MessageSession, FetchTarget +from bots.matrix.message import MessageSession, FetchTarget, ReactionMessageSession from core.builtins import PrivateAssets, Url from core.logger import Logger from core.parser.message import parser @@ -50,14 +50,14 @@ async def on_room_member(room: nio.MatrixRoom, event: nio.RoomMemberEvent): Logger.info(f"Left empty room {room.room_id}") -async def on_message(room: nio.MatrixRoom, event: nio.RoomMessageFormatted): +async def on_message(room: nio.MatrixRoom, event: nio.Event): if event.sender != bot.user_id and bot.olm: for device_id, olm_device in bot.device_store[event.sender].items(): if bot.olm.is_device_verified(olm_device): continue bot.verify_device(olm_device) Logger.info(f"trust olm device for device id {event.sender} -> {device_id}") - if event.source['content']['msgtype'] == 'm.notice': + if isinstance(event, nio.RoomMessageFormatted) and event.source['content']['msgtype'] == 'm.notice': # https://spec.matrix.org/v1.7/client-server-api/#mnotice return is_room = room.member_count != 2 or room.join_rule != 'invite' @@ -65,21 +65,30 @@ async def on_message(room: nio.MatrixRoom, event: nio.RoomMessageFormatted): reply_id = None if 'm.relates_to' in event.source['content'] and 'm.in_reply_to' in event.source['content']['m.relates_to']: reply_id = event.source['content']['m.relates_to']['m.in_reply_to']['event_id'] + resp = await bot.get_displayname(event.sender) if isinstance(resp, nio.ErrorResponse): Logger.error(f"Failed to get display name for {event.sender}") return sender_name = resp.displayname - msg = MessageSession(MsgInfo(target_id=f'Matrix|{target_id}', - sender_id=f'Matrix|{event.sender}', - target_from=f'Matrix', - sender_from='Matrix', - sender_name=sender_name, - client_name=client_name, - message_id=event.event_id, - reply_id=reply_id), - Session(message=event.source, target=room.room_id, sender=event.sender)) + target = MsgInfo(target_id=f'Matrix|{target_id}', + sender_id=f'Matrix|{event.sender}', + target_from=f'Matrix', + sender_from='Matrix', + sender_name=sender_name, + client_name=client_name, + message_id=event.event_id, + reply_id=reply_id) + session = Session(message=event.source, target=room.room_id, sender=event.sender) + + msg = None + if isinstance(event, nio.RoomMessageFormatted): + msg = MessageSession(target, session) + elif isinstance(event, nio.ReactionEvent): + msg = ReactionMessageSession(target, session) + else: + raise NotImplemented asyncio.create_task(parser(msg)) @@ -141,6 +150,7 @@ async def start(): bot.add_event_callback(on_invite, nio.InviteEvent) bot.add_event_callback(on_room_member, nio.RoomMemberEvent) bot.add_event_callback(on_message, nio.RoomMessageFormatted) + bot.add_event_callback(on_message, nio.ReactionEvent) bot.add_to_device_callback(on_verify, nio.KeyVerificationEvent) bot.add_event_callback(on_in_room_verify, nio.RoomMessageUnknown) diff --git a/bots/matrix/message.py b/bots/matrix/message.py index c45d9d30a2..4a47dd6a2e 100644 --- a/bots/matrix/message.py +++ b/bots/matrix/message.py @@ -198,7 +198,7 @@ async def to_message_chain(self): async def delete(self): try: - await bot.room_redact(self.session.target, self.session.message['event_id']) + await bot.room_redact(self.session.target, self.target.message_id) except Exception: Logger.error(traceback.format_exc()) @@ -221,6 +221,33 @@ async def __aexit__(self, exc_type, exc_val, exc_tb): pass +class ReactionMessageSession(MessageSession): + class Feature(MessageSession.Feature): + pass + + class Typing(MessageSession.Typing): + pass + + def as_display(self, text_only=False): + if text_only: + return '' + return self.session.message['content']['m.relates_to']['key'] + + async def to_message_chain(self): + return MessageChain([]) + + def is_quick_confirm(self, target: MessageSession) -> bool: + content = self.session.message['content']['m.relates_to'] + if content['rel_type'] == 'm.annotation': + if target is None or content['event_id'] == target.target.message_id: + if content['key'] in ['👍️', '✔️', '🎉']: # todo: move to config + return True + return False + + asDisplay = as_display + toMessageChain = to_message_chain + + class FetchedSession(Bot.FetchedSession): async def _resolve_matrix_room_(self): diff --git a/core/builtins/message/__init__.py b/core/builtins/message/__init__.py index 7b64d7541c..0ea3648579 100644 --- a/core/builtins/message/__init__.py +++ b/core/builtins/message/__init__.py @@ -52,7 +52,7 @@ async def wait_confirm(self, message_chain=None, quote=True, delete=True, append await send.delete() if result.as_display(text_only=True) in confirm_command: return True - if quick_confirm and result.is_quick_confirm(): + if quick_confirm and result.is_quick_confirm(self): return True return False else: diff --git a/core/types/message/__init__.py b/core/types/message/__init__.py index f8605a6668..58e2e426c0 100644 --- a/core/types/message/__init__.py +++ b/core/types/message/__init__.py @@ -1,5 +1,5 @@ import asyncio -from typing import List, Union, Dict +from typing import List, Self, Union, Dict from core.exceptions import FinishedException from .chain import MessageChain @@ -201,9 +201,10 @@ async def check_native_permission(self): """ raise NotImplementedError - def is_quick_confirm(self) -> bool: + def is_quick_confirm(self, target: Self = None) -> bool: """ 用于检查消息是否可用作快速确认事件。 + :param target: 确认的目标消息 """ return False diff --git a/modules/wiki/wiki.py b/modules/wiki/wiki.py index 9e314b87c1..cc3e1f0079 100644 --- a/modules/wiki/wiki.py +++ b/modules/wiki/wiki.py @@ -4,7 +4,7 @@ import filetype -from core.builtins import Bot, Plain, Image, Voice, Url, confirm_command +from core.builtins import Bot, Plain, Image, Voice, Url, confirm_command, quick_confirm from core.component import module from core.exceptions import AbuseWarning from core.logger import Logger @@ -25,7 +25,7 @@ @wiki.command(' [-l ] {{wiki.help}}', - options_desc={'-l': '{wiki.help.option.l}'}) + options_desc={'-l': '{wiki.help.option.l}'}) async def _(msg: Bot.MessageSession): get_lang = msg.parsed_msg.get('-l', False) if get_lang: @@ -351,6 +351,8 @@ async def wait_confirm(): index = 0 if confirm.as_display(text_only=True) in confirm_command: auto_index = True + if quick_confirm and confirm.is_quick_confirm(session): + auto_index = True elif confirm.as_display(text_only=True).isdigit(): index = int(confirm.as_display()) - 1 else: From 11eb5c15c411caa34af60491e773881029db920a Mon Sep 17 00:00:00 2001 From: xtex Date: Sun, 10 Dec 2023 12:36:12 +0800 Subject: [PATCH 4/6] fix: quick confirm in ~wiki Signed-off-by: xtex --- modules/wiki/wiki.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/wiki/wiki.py b/modules/wiki/wiki.py index cc3e1f0079..b61d0d8065 100644 --- a/modules/wiki/wiki.py +++ b/modules/wiki/wiki.py @@ -351,7 +351,7 @@ async def wait_confirm(): index = 0 if confirm.as_display(text_only=True) in confirm_command: auto_index = True - if quick_confirm and confirm.is_quick_confirm(session): + elif quick_confirm and confirm.is_quick_confirm(session): auto_index = True elif confirm.as_display(text_only=True).isdigit(): index = int(confirm.as_display()) - 1 From e0c411d5168635c6158e4e70edcd294b26c7ea58 Mon Sep 17 00:00:00 2001 From: xtex Date: Sun, 10 Dec 2023 13:14:02 +0800 Subject: [PATCH 5/6] fix: quick confirm in ~wiki Signed-off-by: xtex --- core/builtins/message/__init__.py | 8 ++++---- core/types/message/__init__.py | 4 ++-- modules/chemical_code/__init__.py | 23 ++++++++++++----------- modules/ncmusic/__init__.py | 12 ++++++------ modules/summary/__init__.py | 12 +++++------- modules/twenty_four/__init__.py | 2 +- modules/wiki/wiki.py | 4 ++-- 7 files changed, 32 insertions(+), 33 deletions(-) diff --git a/core/builtins/message/__init__.py b/core/builtins/message/__init__.py index 0ea3648579..d3aeca3c99 100644 --- a/core/builtins/message/__init__.py +++ b/core/builtins/message/__init__.py @@ -9,7 +9,7 @@ from core.builtins.temp import ExecutionLockList from core.builtins.utils import confirm_command, quick_confirm from core.exceptions import WaitCancelException -from core.types.message import MessageSession as MessageSessionT, MsgInfo, Session +from core.types.message import MessageSession as MessageSessionT, FinishedSession, MsgInfo, Session from core.utils.i18n import Locale from core.utils.text import parse_time_string from database import BotDBUtil @@ -52,14 +52,14 @@ async def wait_confirm(self, message_chain=None, quote=True, delete=True, append await send.delete() if result.as_display(text_only=True) in confirm_command: return True - if quick_confirm and result.is_quick_confirm(self): + if quick_confirm and result.is_quick_confirm(send): return True return False else: raise WaitCancelException async def wait_next_message(self, message_chain=None, quote=True, delete=False, - append_instruction=True) -> MessageSessionT: + append_instruction=True) -> (MessageSessionT, FinishedSession): sent = None ExecutionLockList.remove(self) if message_chain is not None: @@ -74,7 +74,7 @@ async def wait_next_message(self, message_chain=None, quote=True, delete=False, if delete and sent is not None: await sent.delete() if result is not None: - return result + return (result, sent) else: raise WaitCancelException diff --git a/core/types/message/__init__.py b/core/types/message/__init__.py index 58e2e426c0..8461a1f42f 100644 --- a/core/types/message/__init__.py +++ b/core/types/message/__init__.py @@ -141,13 +141,13 @@ async def wait_confirm(self, message_chain=None, quote=True, delete=True): """ raise NotImplementedError - async def wait_next_message(self, message_chain=None, quote=True, delete=False, append_instruction=True): + async def wait_next_message(self, message_chain=None, quote=True, delete=False, append_instruction=True) -> (Self, FinishedSession): """ 一次性模板,用于等待对象的下一条消息。 :param message_chain: 需要发送的确认消息,可不填 :param quote: 是否引用传入dict中的消息(默认为True) :param delete: 是否在触发后删除消息 - :return: 下一条消息的MessageChain对象 + :return: 下一条消息的MessageChain对象和发出的提示消息 """ raise NotImplementedError diff --git a/modules/chemical_code/__init__.py b/modules/chemical_code/__init__.py index 275961ba71..68107cd3ab 100644 --- a/modules/chemical_code/__init__.py +++ b/modules/chemical_code/__init__.py @@ -19,8 +19,9 @@ csr_link = 'https://www.chemspider.com' -special_id_path = os.path.abspath(f'./assets/chemical_code/special_id') # 去掉文件扩展名并存储在special_id列表中 -special_id = [os.path.splitext(filename)[0] for filename in os.listdir(special_id_path)] # 可能会导致识别问题的物质(如部分单质)ID,这些 ID 的图片将会在本地调用 +special_id_path = os.path.abspath(f'./assets/chemical_code/special_id') # 去掉文件扩展名并存储在special_id列表中 +special_id = [os.path.splitext(filename)[0] for filename in os.listdir( + special_id_path)] # 可能会导致识别问题的物质(如部分单质)ID,这些 ID 的图片将会在本地调用 element_lists = ['He', 'Li', 'Be', 'Ne', 'Na', 'Mg', 'Al', 'Si', 'Cl', 'Ar', 'Ca', 'Sc', 'Ti', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', @@ -58,7 +59,7 @@ def parse_elements(formula: str) -> dict: @retry(stop=stop_after_attempt(3), reraise=True) async def search_csr(id=None): - if id is not None: + if id is not None: answer_id = id else: answer_id = random.randint(1, 200000000) # 数据库增长速度很快,可手动在此修改ID区间 @@ -95,7 +96,7 @@ async def search_csr(id=None): play_state = {} # 创建一个空字典用于存放游戏状态 -@ccode.command('{{chemical_code.help}}') +@ccode.command('{{chemical_code.help}}') async def chemical_code_by_random(msg: Bot.MessageSession): await chemical_code(msg) @@ -124,7 +125,7 @@ async def s(msg: Bot.MessageSession): async def chemical_code_by_id(msg: Bot.MessageSession): id = msg.parsed_msg[''] if id.isdigit(): - if int(id) == 0: # 若 id 为 0,则随机 + if int(id) == 0: # 若 id 为 0,则随机 await chemical_code(msg) else: await chemical_code(msg, id, random_mode=False) @@ -143,7 +144,7 @@ async def chemical_code(msg: Bot.MessageSession, id=None, random_mode=True, capt play_state[msg.target.target_id]['active'] = False return await msg.finish(msg.locale.t('chemical_code.message.error')) # print(csr) - play_state[msg.target.target_id]['answer'] = csr['name'] + play_state[msg.target.target_id]['answer'] = csr['name'] Logger.info(f'Answer: {csr["name"]}') Logger.info(f'Image: {csr["image"]}') download = False @@ -226,7 +227,7 @@ async def ans(msg: Bot.MessageSession, answer, random_mode): play_state[msg.target.target_id]['active'] = False async def timer(start): - if play_state[msg.target.target_id]['active']: + if play_state[msg.target.target_id]['active']: if datetime.now().timestamp() - start > 60 * set_timeout: await msg.send_message( msg.locale.t('chemical_code.message.timeup', answer=play_state[msg.target.target_id]["answer"])) @@ -242,10 +243,10 @@ async def timer(start): await asyncio.gather(ans(msg, csr['name'], random_mode), timer(time_start)) else: - result = await msg.wait_next_message([Plain(msg.locale.t('chemical_code.message.showid', id=csr["id"])), - Image(newpath), Plain(msg.locale.t('chemical_code.message.captcha', - times=set_timeout))], append_instruction=False) - if play_state[msg.target.target_id]['active']: + result, _ = await msg.wait_next_message([Plain(msg.locale.t('chemical_code.message.showid', id=csr["id"])), + Image(newpath), Plain(msg.locale.t('chemical_code.message.captcha', + times=set_timeout))], append_instruction=False) + if play_state[msg.target.target_id]['active']: if result.as_display(text_only=True) == csr['name']: send_ = msg.locale.t('chemical_code.message.correct') if (g_msg := await gained_petal(wait, 1)): diff --git a/modules/ncmusic/__init__.py b/modules/ncmusic/__init__.py index 16c8c9b434..e3173a8eff 100644 --- a/modules/ncmusic/__init__.py +++ b/modules/ncmusic/__init__.py @@ -30,8 +30,8 @@ async def search(msg: Bot.MessageSession, keyword: str): f"{' / '.join(artist['name'] for artist in song['artists'])}", f"{song['album']['name']}" + (f" ({' / '.join(song['album']['transNames'])})" if 'transNames' in song['album'] else ''), f"{song['id']}" - ] for i, song in enumerate(songs, start=1) - ] + ] for i, song in enumerate(songs, start=1) + ] tables = ImageTable(data, [ msg.locale.t('ncmusic.message.search.table.header.id'), @@ -39,7 +39,7 @@ async def search(msg: Bot.MessageSession, keyword: str): msg.locale.t('ncmusic.message.search.table.header.artists'), msg.locale.t('ncmusic.message.search.table.header.album'), 'ID' - ]) + ]) img = await image_table_render(tables) if img: @@ -49,7 +49,7 @@ async def search(msg: Bot.MessageSession, keyword: str): if len(result['result']['songs']) > 10: send_msg.append(Plain(msg.locale.t('ncmusic.message.search.collapse'))) send_msg.append(Plain(msg.locale.t('ncmusic.message.search.prompt'))) - query = await msg.wait_next_message(send_msg) + query, _ = await msg.wait_next_message(send_msg) query = query.as_display(text_only=True) try: query = int(query) @@ -87,7 +87,7 @@ async def search(msg: Bot.MessageSession, keyword: str): send_msg += msg.locale.t('ncmusic.message.search.collapse') send_msg += '\n' send_msg += msg.locale.t('ncmusic.message.search.prompt') - query = await msg.wait_next_message(send_msg) + query, _ = await msg.wait_next_message(send_msg) query = query.as_display(text_only=True) try: query = int(query) @@ -108,7 +108,7 @@ async def search(msg: Bot.MessageSession, keyword: str): await msg.finish([Image(info['al']['picUrl']), Plain(send_msg)]) except Exception: await msg.finish(msg.locale.t('ncmusic.message.search.invalid.non_digital')) - + @ncmusic.handle('info {{ncmusic.help.info}}') async def info(msg: Bot.MessageSession, sid: str): diff --git a/modules/summary/__init__.py b/modules/summary/__init__.py index 9f432934fd..bc9e725979 100644 --- a/modules/summary/__init__.py +++ b/modules/summary/__init__.py @@ -11,10 +11,10 @@ openai.api_key = Config('openai_api_key') -s = module('summary', - developers=['Dianliang233', 'OasisAkari'], - desc='{summary.help.desc}', - available_for=['QQ', 'QQ|Group']) +s = module('summary', + developers=['Dianliang233', 'OasisAkari'], + desc='{summary.help.desc}', + available_for=['QQ', 'QQ|Group']) @s.handle('{{summary.help}}') @@ -28,7 +28,7 @@ async def _(msg: Bot.MessageSession): qc = CoolDown('call_openai', msg) c = qc.check(60) if c == 0 or msg.target.target_from == 'TEST|Console' or is_superuser: - f_msg = await msg.wait_next_message(msg.locale.t('summary.message'), append_instruction=False) + f_msg, _ = await msg.wait_next_message(msg.locale.t('summary.message'), append_instruction=False) try: f = re.search(r'\[Ke:forward,id=(.*?)\]', f_msg.as_display()).group(1) except AttributeError: @@ -86,5 +86,3 @@ async def _(msg: Bot.MessageSession): await msg.finish(output, disable_secret_check=True) else: await msg.finish(msg.locale.t('message.cooldown', time=int(c), cd_time='60')) - - diff --git a/modules/twenty_four/__init__.py b/modules/twenty_four/__init__.py index 0a9a791ecf..747c28c9c0 100644 --- a/modules/twenty_four/__init__.py +++ b/modules/twenty_four/__init__.py @@ -108,7 +108,7 @@ async def _(msg: Bot.MessageSession): numbers = [random.randint(1, 13) for _ in range(4)] has_solution_flag = await has_solution(numbers) - answer = await msg.wait_next_message(msg.locale.t('twenty_four.message', numbers=numbers), append_instruction=False) + answer, _ = await msg.wait_next_message(msg.locale.t('twenty_four.message', numbers=numbers), append_instruction=False) expression = answer.as_display(text_only=True) if play_state[msg.target.target_id]['active']: if expression.lower() in no_solution: diff --git a/modules/wiki/wiki.py b/modules/wiki/wiki.py index b61d0d8065..b8aa0f6c0b 100644 --- a/modules/wiki/wiki.py +++ b/modules/wiki/wiki.py @@ -346,12 +346,12 @@ async def image_and_voice(): async def wait_confirm(): if wait_msg_list and session.Feature.wait: - confirm = await session.wait_next_message(wait_msg_list, delete=True, append_instruction=False) + confirm, sent = await session.wait_next_message(wait_msg_list, delete=True, append_instruction=False) auto_index = False index = 0 if confirm.as_display(text_only=True) in confirm_command: auto_index = True - elif quick_confirm and confirm.is_quick_confirm(session): + elif quick_confirm and confirm.is_quick_confirm(sent): auto_index = True elif confirm.as_display(text_only=True).isdigit(): index = int(confirm.as_display()) - 1 From 282445a4ef91cecc333de4070e4980983419acb0 Mon Sep 17 00:00:00 2001 From: xtex Date: Sun, 10 Dec 2023 13:28:15 +0800 Subject: [PATCH 6/6] fix: quick confirm Signed-off-by: xtex --- bots/matrix/message.py | 10 +++++++--- core/types/message/__init__.py | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/bots/matrix/message.py b/bots/matrix/message.py index 4a47dd6a2e..1f974c4daa 100644 --- a/bots/matrix/message.py +++ b/bots/matrix/message.py @@ -236,12 +236,16 @@ def as_display(self, text_only=False): async def to_message_chain(self): return MessageChain([]) - def is_quick_confirm(self, target: MessageSession) -> bool: + def is_quick_confirm(self, target: Union[MessageSession, FinishedSession]) -> bool: content = self.session.message['content']['m.relates_to'] if content['rel_type'] == 'm.annotation': - if target is None or content['event_id'] == target.target.message_id: - if content['key'] in ['👍️', '✔️', '🎉']: # todo: move to config + if content['key'] in ['👍️', '✔️', '🎉']: # todo: move to config + if target is None: return True + else: + msg = [target.target.message_id] if isinstance(target, MessageSession) else target.message_id + if content['event_id'] in msg: + return True return False asDisplay = as_display diff --git a/core/types/message/__init__.py b/core/types/message/__init__.py index 8461a1f42f..37cb929b48 100644 --- a/core/types/message/__init__.py +++ b/core/types/message/__init__.py @@ -201,7 +201,7 @@ async def check_native_permission(self): """ raise NotImplementedError - def is_quick_confirm(self, target: Self = None) -> bool: + def is_quick_confirm(self, target: Union[Self, FinishedSession] = None) -> bool: """ 用于检查消息是否可用作快速确认事件。 :param target: 确认的目标消息