Skip to content

Commit 992aa67

Browse files
sdanialrazaalmeidxkodiakhq[bot]
authored
feat(MessageCreateOptions): add enforceNonce (#10129)
Co-authored-by: almeidx <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent e9d6547 commit 992aa67

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

packages/discord.js/src/errors/ErrorCodes.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@
8888
* <warn>This property is deprecated.</warn>
8989
9090
* @property {'MessageBulkDeleteType'} MessageBulkDeleteType
91-
* @property {'MessageNonceType'} MessageNonceType
9291
* @property {'MessageContentType'} MessageContentType
92+
* @property {'MessageNonceRequired'} MessageNonceRequired
93+
* @property {'MessageNonceType'} MessageNonceType
9394
9495
* @property {'SplitMaxLen'} SplitMaxLen
9596
* <warn>This property is deprecated.</warn>
@@ -244,8 +245,9 @@ const keys = [
244245
'ImageSize',
245246

246247
'MessageBulkDeleteType',
247-
'MessageNonceType',
248248
'MessageContentType',
249+
'MessageNonceRequired',
250+
'MessageNonceType',
249251

250252
'SplitMaxLen',
251253

packages/discord.js/src/errors/Messages.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ const Messages = {
7474
[DjsErrorCodes.ImageSize]: size => `Invalid image size: ${size}`,
7575

7676
[DjsErrorCodes.MessageBulkDeleteType]: 'The messages must be an Array, Collection, or number.',
77-
[DjsErrorCodes.MessageNonceType]: 'Message nonce must be an integer or a string.',
7877
[DjsErrorCodes.MessageContentType]: 'Message content must be a string.',
78+
[DjsErrorCodes.MessageNonceRequired]: 'Message nonce is required when enforceNonce is true.',
79+
[DjsErrorCodes.MessageNonceType]: 'Message nonce must be an integer or a string.',
7980

8081
[DjsErrorCodes.SplitMaxLen]: 'Chunk exceeds the max length and contains no split characters.',
8182

packages/discord.js/src/structures/MessagePayload.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { Buffer } = require('node:buffer');
44
const { lazy, isJSONEncodable } = require('@discordjs/util');
55
const { MessageFlags } = require('discord-api-types/v10');
66
const ActionRowBuilder = require('./ActionRowBuilder');
7-
const { DiscordjsRangeError, ErrorCodes } = require('../errors');
7+
const { DiscordjsError, DiscordjsRangeError, ErrorCodes } = require('../errors');
88
const { resolveFile } = require('../util/DataResolver');
99
const MessageFlagsBitField = require('../util/MessageFlagsBitField');
1010
const { basename, verifyString } = require('../util/Util');
@@ -133,6 +133,11 @@ class MessagePayload {
133133
}
134134
}
135135

136+
const enforce_nonce = Boolean(this.options.enforceNonce);
137+
if (enforce_nonce && nonce === undefined) {
138+
throw new DiscordjsError(ErrorCodes.MessageNonceRequired);
139+
}
140+
136141
const components = this.options.components?.map(component =>
137142
(isJSONEncodable(component) ? component : new ActionRowBuilder(component)).toJSON(),
138143
);
@@ -201,6 +206,7 @@ class MessagePayload {
201206
content,
202207
tts,
203208
nonce,
209+
enforce_nonce,
204210
embeds: this.options.embeds?.map(embed =>
205211
isJSONEncodable(embed) ? embed.toJSON() : this.target.client.options.jsonTransformer(embed),
206212
),

packages/discord.js/src/structures/interfaces/TextBasedChannel.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ class TextBasedChannel {
7777
* The options for sending a message.
7878
* @typedef {BaseMessageOptions} BaseMessageCreateOptions
7979
* @property {boolean} [tts=false] Whether the message should be spoken aloud
80-
* @property {string} [nonce=''] The nonce for the message
80+
* @property {string} [nonce] The nonce for the message
81+
* <info>This property is required if `enforceNonce` set to `true`.</info>
82+
* @property {boolean} [enforceNonce] Whether the nonce should be checked for uniqueness in the past few minutes.
83+
* If another message was created by the same author with the same nonce,
84+
* that message will be returned and no new message will be created
8185
* @property {StickerResolvable[]} [stickers=[]] The stickers to send in the message
8286
* @property {MessageFlags} [flags] Which flags to set for the message.
8387
* <info>Only `MessageFlags.SuppressEmbeds` and `MessageFlags.SuppressNotifications` can be set.</info>

packages/discord.js/typings/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3828,8 +3828,9 @@ export enum DiscordjsErrorCodes {
38283828
ImageSize = 'ImageSize',
38293829

38303830
MessageBulkDeleteType = 'MessageBulkDeleteType',
3831-
MessageNonceType = 'MessageNonceType',
38323831
MessageContentType = 'MessageContentType',
3832+
MessageNonceRequired = 'MessageNonceRequired',
3833+
MessageNonceType = 'MessageNonceType',
38333834

38343835
/** @deprecated No longer in use */
38353836
SplitMaxLen = 'SplitMaxLen',
@@ -6223,6 +6224,7 @@ export interface BaseMessageOptions {
62236224
export interface MessageCreateOptions extends BaseMessageOptions {
62246225
tts?: boolean;
62256226
nonce?: string | number;
6227+
enforceNonce?: boolean;
62266228
reply?: ReplyOptions;
62276229
stickers?: StickerResolvable[];
62286230
flags?: BitFieldResolvable<

0 commit comments

Comments
 (0)