Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MessageCreateOptions): add enforceNonce #10129

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
sdanialraza marked this conversation as resolved.
Show resolved Hide resolved

'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);
sdanialraza marked this conversation as resolved.
Show resolved Hide resolved
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
Loading