From d68956953b47de0060dbac64d6e55993fc140196 Mon Sep 17 00:00:00 2001 From: CMHopeSunshine <277073121@qq.com> Date: Thu, 21 Dec 2023 21:43:45 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E4=BD=BF=20Bot.send=20=E4=B9=9F?= =?UTF-8?q?=E8=83=BD=E8=BF=94=E5=9B=9E=E5=BA=94=E7=94=A8=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot/adapters/discord/bot.py | 20 ++++++++++++++++---- nonebot/adapters/discord/commands/matcher.py | 14 +++++++------- nonebot/adapters/discord/event.py | 2 +- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/nonebot/adapters/discord/bot.py b/nonebot/adapters/discord/bot.py index 6da9e84..0808b6f 100644 --- a/nonebot/adapters/discord/bot.py +++ b/nonebot/adapters/discord/bot.py @@ -18,6 +18,7 @@ ) from .config import BotInfo from .event import Event, InteractionCreateEvent, MessageEvent +from .exception import ActionFailed from .message import Message, MessageSegment, parse_message from .utils import log @@ -193,7 +194,7 @@ async def send( at_sender: Optional[bool] = None, reply_message: bool = False, **params: Any, - ) -> Optional[MessageGet]: + ) -> MessageGet: """send message. Args: @@ -218,10 +219,21 @@ async def send( tts=tts, allowed_mentions=allowed_mentions, **message_data ), ) - return await self.create_interaction_response( - interaction_id=event.id, + try: + await self.create_interaction_response( + interaction_id=event.id, + interaction_token=event.token, + response=response, + ) + except ActionFailed: + return await self.create_followup_message( + application_id=event.application_id, + interaction_token=event.token, + **message_data, + ) + return await self.get_origin_interaction_response( + application_id=event.application_id, interaction_token=event.token, - response=response, ) if not isinstance(event, MessageEvent) or not event.channel_id or not event.id: diff --git a/nonebot/adapters/discord/commands/matcher.py b/nonebot/adapters/discord/commands/matcher.py index 195a822..f49f2e0 100644 --- a/nonebot/adapters/discord/commands/matcher.py +++ b/nonebot/adapters/discord/commands/matcher.py @@ -105,7 +105,7 @@ async def get_response(cls) -> MessageGet: ): raise ValueError("Invalid event or bot") return await bot.get_origin_interaction_response( - application_id=bot.application_id, + application_id=event.application_id, interaction_token=event.token, ) @@ -127,7 +127,7 @@ async def edit_response( _message = message message_data = parse_message(_message) await bot.edit_origin_interaction_response( - application_id=bot.application_id, + application_id=event.application_id, interaction_token=event.token, **message_data, ) @@ -141,7 +141,7 @@ async def delete_response(cls) -> None: ): raise ValueError("Invalid event or bot") await bot.delete_origin_interaction_response( - application_id=bot.application_id, + application_id=event.application_id, interaction_token=event.token, ) @@ -166,7 +166,7 @@ async def send_followup_msg( if flags: message_data["flags"] = int(flags) return await bot.create_followup_message( - application_id=bot.application_id, + application_id=event.application_id, interaction_token=event.token, **message_data, ) @@ -180,7 +180,7 @@ async def get_followup_msg(cls, message_id: SnowflakeType): ): raise ValueError("Invalid event or bot") return await bot.get_followup_message( - application_id=bot.application_id, + application_id=event.application_id, interaction_token=event.token, message_id=message_id, ) @@ -204,7 +204,7 @@ async def edit_followup_msg( _message = message message_data = parse_message(_message) return await bot.edit_followup_message( - application_id=bot.application_id, + application_id=event.application_id, interaction_token=event.token, message_id=message_id, **message_data, @@ -219,7 +219,7 @@ async def delete_followup_msg(cls, message_id: SnowflakeType) -> None: ): raise ValueError("Invalid event or bot") await bot.delete_followup_message( - application_id=bot.application_id, + application_id=event.application_id, interaction_token=event.token, message_id=message_id, ) diff --git a/nonebot/adapters/discord/event.py b/nonebot/adapters/discord/event.py index d63907e..29d8d2b 100644 --- a/nonebot/adapters/discord/event.py +++ b/nonebot/adapters/discord/event.py @@ -193,7 +193,7 @@ def message(self) -> Message: @property def original_message(self) -> Message: - return getattr(self, "_original_message", self.get_message()) + return getattr(self, "_original_message", self.get_message()) # type: ignore @override def get_type(self) -> str: