mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 11:33:30 +01:00
feat(MessageCreateOptions): add enforceNonce (#10129)
Co-authored-by: almeidx <github@almeidx.dev> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
@@ -88,8 +88,9 @@
|
|||||||
* <warn>This property is deprecated.</warn>
|
* <warn>This property is deprecated.</warn>
|
||||||
|
|
||||||
* @property {'MessageBulkDeleteType'} MessageBulkDeleteType
|
* @property {'MessageBulkDeleteType'} MessageBulkDeleteType
|
||||||
* @property {'MessageNonceType'} MessageNonceType
|
|
||||||
* @property {'MessageContentType'} MessageContentType
|
* @property {'MessageContentType'} MessageContentType
|
||||||
|
* @property {'MessageNonceRequired'} MessageNonceRequired
|
||||||
|
* @property {'MessageNonceType'} MessageNonceType
|
||||||
|
|
||||||
* @property {'SplitMaxLen'} SplitMaxLen
|
* @property {'SplitMaxLen'} SplitMaxLen
|
||||||
* <warn>This property is deprecated.</warn>
|
* <warn>This property is deprecated.</warn>
|
||||||
@@ -244,8 +245,9 @@ const keys = [
|
|||||||
'ImageSize',
|
'ImageSize',
|
||||||
|
|
||||||
'MessageBulkDeleteType',
|
'MessageBulkDeleteType',
|
||||||
'MessageNonceType',
|
|
||||||
'MessageContentType',
|
'MessageContentType',
|
||||||
|
'MessageNonceRequired',
|
||||||
|
'MessageNonceType',
|
||||||
|
|
||||||
'SplitMaxLen',
|
'SplitMaxLen',
|
||||||
|
|
||||||
|
|||||||
@@ -74,8 +74,9 @@ const Messages = {
|
|||||||
[DjsErrorCodes.ImageSize]: size => `Invalid image size: ${size}`,
|
[DjsErrorCodes.ImageSize]: size => `Invalid image size: ${size}`,
|
||||||
|
|
||||||
[DjsErrorCodes.MessageBulkDeleteType]: 'The messages must be an Array, Collection, or number.',
|
[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.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.',
|
[DjsErrorCodes.SplitMaxLen]: 'Chunk exceeds the max length and contains no split characters.',
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ const { Buffer } = require('node:buffer');
|
|||||||
const { lazy, isJSONEncodable } = require('@discordjs/util');
|
const { lazy, isJSONEncodable } = require('@discordjs/util');
|
||||||
const { MessageFlags } = require('discord-api-types/v10');
|
const { MessageFlags } = require('discord-api-types/v10');
|
||||||
const ActionRowBuilder = require('./ActionRowBuilder');
|
const ActionRowBuilder = require('./ActionRowBuilder');
|
||||||
const { DiscordjsRangeError, ErrorCodes } = require('../errors');
|
const { DiscordjsError, DiscordjsRangeError, ErrorCodes } = require('../errors');
|
||||||
const { resolveFile } = require('../util/DataResolver');
|
const { resolveFile } = require('../util/DataResolver');
|
||||||
const MessageFlagsBitField = require('../util/MessageFlagsBitField');
|
const MessageFlagsBitField = require('../util/MessageFlagsBitField');
|
||||||
const { basename, verifyString } = require('../util/Util');
|
const { basename, verifyString } = require('../util/Util');
|
||||||
@@ -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 =>
|
const components = this.options.components?.map(component =>
|
||||||
(isJSONEncodable(component) ? component : new ActionRowBuilder(component)).toJSON(),
|
(isJSONEncodable(component) ? component : new ActionRowBuilder(component)).toJSON(),
|
||||||
);
|
);
|
||||||
@@ -201,6 +206,7 @@ class MessagePayload {
|
|||||||
content,
|
content,
|
||||||
tts,
|
tts,
|
||||||
nonce,
|
nonce,
|
||||||
|
enforce_nonce,
|
||||||
embeds: this.options.embeds?.map(embed =>
|
embeds: this.options.embeds?.map(embed =>
|
||||||
isJSONEncodable(embed) ? embed.toJSON() : this.target.client.options.jsonTransformer(embed),
|
isJSONEncodable(embed) ? embed.toJSON() : this.target.client.options.jsonTransformer(embed),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -77,7 +77,11 @@ class TextBasedChannel {
|
|||||||
* The options for sending a message.
|
* The options for sending a message.
|
||||||
* @typedef {BaseMessageOptions} BaseMessageCreateOptions
|
* @typedef {BaseMessageOptions} BaseMessageCreateOptions
|
||||||
* @property {boolean} [tts=false] Whether the message should be spoken aloud
|
* @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 {StickerResolvable[]} [stickers=[]] The stickers to send in the message
|
||||||
* @property {MessageFlags} [flags] Which flags to set for the message.
|
* @property {MessageFlags} [flags] Which flags to set for the message.
|
||||||
* <info>Only `MessageFlags.SuppressEmbeds` and `MessageFlags.SuppressNotifications` can be set.</info>
|
* <info>Only `MessageFlags.SuppressEmbeds` and `MessageFlags.SuppressNotifications` can be set.</info>
|
||||||
|
|||||||
4
packages/discord.js/typings/index.d.ts
vendored
4
packages/discord.js/typings/index.d.ts
vendored
@@ -3828,8 +3828,9 @@ export enum DiscordjsErrorCodes {
|
|||||||
ImageSize = 'ImageSize',
|
ImageSize = 'ImageSize',
|
||||||
|
|
||||||
MessageBulkDeleteType = 'MessageBulkDeleteType',
|
MessageBulkDeleteType = 'MessageBulkDeleteType',
|
||||||
MessageNonceType = 'MessageNonceType',
|
|
||||||
MessageContentType = 'MessageContentType',
|
MessageContentType = 'MessageContentType',
|
||||||
|
MessageNonceRequired = 'MessageNonceRequired',
|
||||||
|
MessageNonceType = 'MessageNonceType',
|
||||||
|
|
||||||
/** @deprecated No longer in use */
|
/** @deprecated No longer in use */
|
||||||
SplitMaxLen = 'SplitMaxLen',
|
SplitMaxLen = 'SplitMaxLen',
|
||||||
@@ -6223,6 +6224,7 @@ export interface BaseMessageOptions {
|
|||||||
export interface MessageCreateOptions extends BaseMessageOptions {
|
export interface MessageCreateOptions extends BaseMessageOptions {
|
||||||
tts?: boolean;
|
tts?: boolean;
|
||||||
nonce?: string | number;
|
nonce?: string | number;
|
||||||
|
enforceNonce?: boolean;
|
||||||
reply?: ReplyOptions;
|
reply?: ReplyOptions;
|
||||||
stickers?: StickerResolvable[];
|
stickers?: StickerResolvable[];
|
||||||
flags?: BitFieldResolvable<
|
flags?: BitFieldResolvable<
|
||||||
|
|||||||
Reference in New Issue
Block a user