mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat: Add support for Automated Message nonce handling (#10381)
* Add support for Automated Message nonce handling * Fix options property * Address PR feedback * Handled case where it was explicitly set to false for that iteration to not generate a nonce, and PR feedback * Fix lint issue * Fix lint issue * Move to MessagePayload.resolveBody instead * Fix test errors * Update packages/discord.js/src/structures/MessagePayload.js Co-authored-by: Almeida <github@almeidx.dev> * PR feedback * Merge * Let and not const --------- Co-authored-by: Almeida <github@almeidx.dev> Co-authored-by: Almeida <almeidx@pm.me>
This commit is contained in:
@@ -535,6 +535,9 @@ class Client extends BaseClient {
|
||||
if (typeof options.failIfNotExists !== 'boolean') {
|
||||
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'failIfNotExists', 'a boolean');
|
||||
}
|
||||
if (typeof options.enforceNonce !== 'boolean') {
|
||||
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'enforceNonce', 'a boolean');
|
||||
}
|
||||
if (
|
||||
(typeof options.allowedMentions !== 'object' && options.allowedMentions !== undefined) ||
|
||||
options.allowedMentions === null
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
const { Buffer } = require('node:buffer');
|
||||
const { lazy, isJSONEncodable } = require('@discordjs/util');
|
||||
const { DiscordSnowflake } = require('@sapphire/snowflake');
|
||||
const { MessageFlags } = require('discord-api-types/v10');
|
||||
const ActionRowBuilder = require('./ActionRowBuilder');
|
||||
const { DiscordjsError, DiscordjsRangeError, ErrorCodes } = require('../errors');
|
||||
@@ -133,9 +134,17 @@ class MessagePayload {
|
||||
}
|
||||
}
|
||||
|
||||
const enforce_nonce = Boolean(this.options.enforceNonce);
|
||||
if (enforce_nonce && nonce === undefined) {
|
||||
throw new DiscordjsError(ErrorCodes.MessageNonceRequired);
|
||||
let enforce_nonce = Boolean(this.options.enforceNonce);
|
||||
|
||||
// If `nonce` isn't provided, generate one & set `enforceNonce`
|
||||
// Unless `enforceNonce` is explicitly set to `false`(not just falsy)
|
||||
if (nonce === undefined) {
|
||||
if (this.options.enforceNonce !== false && this.client.options.enforceNonce) {
|
||||
nonce = DiscordSnowflake.generate().toString();
|
||||
enforce_nonce = true;
|
||||
} else if (enforce_nonce) {
|
||||
throw new DiscordjsError(ErrorCodes.MessageNonceRequired);
|
||||
}
|
||||
}
|
||||
|
||||
const components = this.options.components?.map(component =>
|
||||
|
||||
@@ -41,6 +41,7 @@ const { version } = require('../../package.json');
|
||||
* @property {WebsocketOptions} [ws] Options for the WebSocket
|
||||
* @property {RESTOptions} [rest] Options for the REST manager
|
||||
* @property {Function} [jsonTransformer] A function used to transform outgoing json data
|
||||
* @property {boolean} [enforceNonce=false] The default value for {@link MessageReplyOptions#enforceNonce}
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -117,6 +118,7 @@ class Options extends null {
|
||||
makeCache: this.cacheWithLimits(this.DefaultMakeCacheSettings),
|
||||
partials: [],
|
||||
failIfNotExists: true,
|
||||
enforceNonce: false,
|
||||
presence: {},
|
||||
sweepers: this.DefaultSweeperSettings,
|
||||
ws: {
|
||||
|
||||
1
packages/discord.js/typings/index.d.ts
vendored
1
packages/discord.js/typings/index.d.ts
vendored
@@ -5311,6 +5311,7 @@ export interface ClientOptions {
|
||||
ws?: WebSocketOptions;
|
||||
rest?: Partial<RESTOptions>;
|
||||
jsonTransformer?: (obj: unknown) => unknown;
|
||||
enforceNonce?: boolean;
|
||||
}
|
||||
|
||||
export type ClientPresenceStatus = 'online' | 'idle' | 'dnd';
|
||||
|
||||
Reference in New Issue
Block a user