From 716c578dd4686cac26061dc8e9496c681f32028f Mon Sep 17 00:00:00 2001 From: AstreaTSS <25420078+AstreaTSS@users.noreply.github.com> Date: Wed, 21 Feb 2024 13:20:54 -0500 Subject: [PATCH 1/2] feat: add nonce and enforce_nonce to SendMixin --- interactions/client/mixins/send.py | 11 +++++++++++ interactions/models/discord/message.py | 10 +++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/interactions/client/mixins/send.py b/interactions/client/mixins/send.py index 6fa4a2ff3..5776c78d5 100644 --- a/interactions/client/mixins/send.py +++ b/interactions/client/mixins/send.py @@ -47,6 +47,8 @@ async def send( silent: bool = False, flags: Optional[Union[int, "MessageFlags"]] = None, delete_after: Optional[float] = None, + nonce: Optional[str | int] = None, + enforce_nonce: bool = False, **kwargs: Any, ) -> "Message": """ @@ -67,6 +69,10 @@ async def send( silent: Should this message be sent without triggering a notification. flags: Message flags to apply. delete_after: Delete message after this many seconds. + nonce: Used to verify a message was sent. Can be up to 25 characters. + enforce_nonce: If enabled and nonce is present, it will be checked for uniqueness in the past few minutes. \ + If another message was created by the same author with the same nonce, that message will be returned \ + and no new message will be created. Returns: New message object that was sent. @@ -95,6 +101,9 @@ async def send( "Attachments are not files. Attachments only contain metadata about the file, not the file itself - to send an attachment, you need to download it first. Check Attachment.url" ) + if enforce_nonce and not nonce: + raise ValueError("You must provide a nonce to use enforce_nonce.") + message_payload = models.discord.message.process_message_payload( content=content, embeds=embeds or embed, @@ -104,6 +113,8 @@ async def send( reply_to=reply_to, tts=tts, flags=flags, + nonce=nonce, + enforce_nonce=enforce_nonce, **kwargs, ) diff --git a/interactions/models/discord/message.py b/interactions/models/discord/message.py index dccecf023..333dbac0f 100644 --- a/interactions/models/discord/message.py +++ b/interactions/models/discord/message.py @@ -925,7 +925,9 @@ def process_message_payload( reply_to: Optional[Union[MessageReference, Message, dict, "Snowflake_Type"]] = None, attachments: Optional[List[Union[Attachment, dict]]] = None, tts: bool = False, - flags: Optional[Union[int, MessageFlags]] = None, + flags: Optional[Union[int, MessageFlags]] = None,\ + nonce: Optional[str | int] = None, + enforce_nonce: bool = False, **kwargs, ) -> dict: """ @@ -941,6 +943,10 @@ def process_message_payload( attachments: The attachments to keep, only used when editing message. tts: Should this message use Text To Speech. flags: Message flags to apply. + nonce: Used to verify a message was sent. + enforce_nonce: If enabled and nonce is present, it will be checked for uniqueness in the past few minutes. \ + If another message was created by the same author with the same nonce, that message will be returned \ + and no new message will be created. Returns: Dictionary @@ -969,6 +975,8 @@ def process_message_payload( "attachments": attachments, "tts": tts, "flags": flags, + "nonce": nonce, + "enforce_nonce": enforce_nonce, **kwargs, } ) From d933bc629c4ef1c5ec1e161225d61d7213626907 Mon Sep 17 00:00:00 2001 From: AstreaTSS <25420078+AstreaTSS@users.noreply.github.com> Date: Wed, 21 Feb 2024 13:22:18 -0500 Subject: [PATCH 2/2] fix: oops --- interactions/models/discord/message.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interactions/models/discord/message.py b/interactions/models/discord/message.py index 333dbac0f..94a5a6e97 100644 --- a/interactions/models/discord/message.py +++ b/interactions/models/discord/message.py @@ -925,7 +925,7 @@ def process_message_payload( reply_to: Optional[Union[MessageReference, Message, dict, "Snowflake_Type"]] = None, attachments: Optional[List[Union[Attachment, dict]]] = None, tts: bool = False, - flags: Optional[Union[int, MessageFlags]] = None,\ + flags: Optional[Union[int, MessageFlags]] = None, nonce: Optional[str | int] = None, enforce_nonce: bool = False, **kwargs,