Skip to content

Commit e7403ff

Browse files
authored
feat: add nonce and enforce_nonce to SendMixin (#1616)
* feat: add nonce and enforce_nonce to SendMixin * fix: oops
1 parent 353c417 commit e7403ff

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

interactions/client/mixins/send.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ async def send(
4747
silent: bool = False,
4848
flags: Optional[Union[int, "MessageFlags"]] = None,
4949
delete_after: Optional[float] = None,
50+
nonce: Optional[str | int] = None,
51+
enforce_nonce: bool = False,
5052
**kwargs: Any,
5153
) -> "Message":
5254
"""
@@ -67,6 +69,10 @@ async def send(
6769
silent: Should this message be sent without triggering a notification.
6870
flags: Message flags to apply.
6971
delete_after: Delete message after this many seconds.
72+
nonce: Used to verify a message was sent. Can be up to 25 characters.
73+
enforce_nonce: If enabled and nonce is present, it will be checked for uniqueness in the past few minutes. \
74+
If another message was created by the same author with the same nonce, that message will be returned \
75+
and no new message will be created.
7076
7177
Returns:
7278
New message object that was sent.
@@ -95,6 +101,9 @@ async def send(
95101
"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"
96102
)
97103

104+
if enforce_nonce and not nonce:
105+
raise ValueError("You must provide a nonce to use enforce_nonce.")
106+
98107
message_payload = models.discord.message.process_message_payload(
99108
content=content,
100109
embeds=embeds or embed,
@@ -104,6 +113,8 @@ async def send(
104113
reply_to=reply_to,
105114
tts=tts,
106115
flags=flags,
116+
nonce=nonce,
117+
enforce_nonce=enforce_nonce,
107118
**kwargs,
108119
)
109120

interactions/models/discord/message.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,8 @@ def process_message_payload(
926926
attachments: Optional[List[Union[Attachment, dict]]] = None,
927927
tts: bool = False,
928928
flags: Optional[Union[int, MessageFlags]] = None,
929+
nonce: Optional[str | int] = None,
930+
enforce_nonce: bool = False,
929931
**kwargs,
930932
) -> dict:
931933
"""
@@ -941,6 +943,10 @@ def process_message_payload(
941943
attachments: The attachments to keep, only used when editing message.
942944
tts: Should this message use Text To Speech.
943945
flags: Message flags to apply.
946+
nonce: Used to verify a message was sent.
947+
enforce_nonce: If enabled and nonce is present, it will be checked for uniqueness in the past few minutes. \
948+
If another message was created by the same author with the same nonce, that message will be returned \
949+
and no new message will be created.
944950
945951
Returns:
946952
Dictionary
@@ -969,6 +975,8 @@ def process_message_payload(
969975
"attachments": attachments,
970976
"tts": tts,
971977
"flags": flags,
978+
"nonce": nonce,
979+
"enforce_nonce": enforce_nonce,
972980
**kwargs,
973981
}
974982
)

0 commit comments

Comments
 (0)