Skip to content

Commit

Permalink
feat(MessageCreateOptions): add enforceNonce (#10129)
Browse files Browse the repository at this point in the history
Co-authored-by: almeidx <github@almeidx.dev>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 17, 2024
1 parent e9d6547 commit 992aa67
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
6 changes: 4 additions & 2 deletions packages/discord.js/src/errors/ErrorCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@
* <warn>This property is deprecated.</warn>
* @property {'MessageBulkDeleteType'} MessageBulkDeleteType
* @property {'MessageNonceType'} MessageNonceType
* @property {'MessageContentType'} MessageContentType
* @property {'MessageNonceRequired'} MessageNonceRequired
* @property {'MessageNonceType'} MessageNonceType
* @property {'SplitMaxLen'} SplitMaxLen
* <warn>This property is deprecated.</warn>
Expand Down Expand Up @@ -244,8 +245,9 @@ const keys = [
'ImageSize',

'MessageBulkDeleteType',
'MessageNonceType',
'MessageContentType',
'MessageNonceRequired',
'MessageNonceType',

'SplitMaxLen',

Expand Down
3 changes: 2 additions & 1 deletion packages/discord.js/src/errors/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ const Messages = {
[DjsErrorCodes.ImageSize]: size => `Invalid image size: ${size}`,

[DjsErrorCodes.MessageBulkDeleteType]: 'The messages must be an Array, Collection, or number.',
[DjsErrorCodes.MessageNonceType]: 'Message nonce must be an integer or a string.',
[DjsErrorCodes.MessageContentType]: 'Message content must be a string.',
[DjsErrorCodes.MessageNonceRequired]: 'Message nonce is required when enforceNonce is true.',
[DjsErrorCodes.MessageNonceType]: 'Message nonce must be an integer or a string.',

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

Expand Down
8 changes: 7 additions & 1 deletion packages/discord.js/src/structures/MessagePayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { Buffer } = require('node:buffer');
const { lazy, isJSONEncodable } = require('@discordjs/util');
const { MessageFlags } = require('discord-api-types/v10');
const ActionRowBuilder = require('./ActionRowBuilder');
const { DiscordjsRangeError, ErrorCodes } = require('../errors');
const { DiscordjsError, DiscordjsRangeError, ErrorCodes } = require('../errors');
const { resolveFile } = require('../util/DataResolver');
const MessageFlagsBitField = require('../util/MessageFlagsBitField');
const { basename, verifyString } = require('../util/Util');
Expand Down Expand Up @@ -133,6 +133,11 @@ class MessagePayload {
}
}

const enforce_nonce = Boolean(this.options.enforceNonce);
if (enforce_nonce && nonce === undefined) {
throw new DiscordjsError(ErrorCodes.MessageNonceRequired);
}

const components = this.options.components?.map(component =>
(isJSONEncodable(component) ? component : new ActionRowBuilder(component)).toJSON(),
);
Expand Down Expand Up @@ -201,6 +206,7 @@ class MessagePayload {
content,
tts,
nonce,
enforce_nonce,
embeds: this.options.embeds?.map(embed =>
isJSONEncodable(embed) ? embed.toJSON() : this.target.client.options.jsonTransformer(embed),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ class TextBasedChannel {
* The options for sending a message.
* @typedef {BaseMessageOptions} BaseMessageCreateOptions
* @property {boolean} [tts=false] Whether the message should be spoken aloud
* @property {string} [nonce=''] The nonce for the message
* @property {string} [nonce] The nonce for the message
* <info>This property is required if `enforceNonce` set to `true`.</info>
* @property {boolean} [enforceNonce] Whether the nonce should 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
* @property {StickerResolvable[]} [stickers=[]] The stickers to send in the message
* @property {MessageFlags} [flags] Which flags to set for the message.
* <info>Only `MessageFlags.SuppressEmbeds` and `MessageFlags.SuppressNotifications` can be set.</info>
Expand Down
4 changes: 3 additions & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3828,8 +3828,9 @@ export enum DiscordjsErrorCodes {
ImageSize = 'ImageSize',

MessageBulkDeleteType = 'MessageBulkDeleteType',
MessageNonceType = 'MessageNonceType',
MessageContentType = 'MessageContentType',
MessageNonceRequired = 'MessageNonceRequired',
MessageNonceType = 'MessageNonceType',

/** @deprecated No longer in use */
SplitMaxLen = 'SplitMaxLen',
Expand Down Expand Up @@ -6223,6 +6224,7 @@ export interface BaseMessageOptions {
export interface MessageCreateOptions extends BaseMessageOptions {
tts?: boolean;
nonce?: string | number;
enforceNonce?: boolean;
reply?: ReplyOptions;
stickers?: StickerResolvable[];
flags?: BitFieldResolvable<
Expand Down

0 comments on commit 992aa67

Please sign in to comment.