Skip to content

Commit

Permalink
✨ 使 Bot.send 也能返回应用命令响应结果
Browse files Browse the repository at this point in the history
  • Loading branch information
CMHopeSunshine committed Dec 21, 2023
1 parent 3a15a6e commit d689569
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
20 changes: 16 additions & 4 deletions nonebot/adapters/discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -193,7 +194,7 @@ async def send(
at_sender: Optional[bool] = None,
reply_message: bool = False,
**params: Any,
) -> Optional[MessageGet]:
) -> MessageGet:
"""send message.
Args:
Expand All @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions nonebot/adapters/discord/commands/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand All @@ -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,
)
Expand All @@ -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,
)

Expand All @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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,
Expand All @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion nonebot/adapters/discord/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit d689569

Please sign in to comment.