Skip to content

Commit 2ca187b

Browse files
JMTKalmeidx
andauthoredAug 19, 2024··
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>
1 parent 8fb4008 commit 2ca187b

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed
 

‎packages/discord.js/src/client/Client.js

+3
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,9 @@ class Client extends BaseClient {
535535
if (typeof options.failIfNotExists !== 'boolean') {
536536
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'failIfNotExists', 'a boolean');
537537
}
538+
if (typeof options.enforceNonce !== 'boolean') {
539+
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'enforceNonce', 'a boolean');
540+
}
538541
if (
539542
(typeof options.allowedMentions !== 'object' && options.allowedMentions !== undefined) ||
540543
options.allowedMentions === null

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

+12-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const { Buffer } = require('node:buffer');
44
const { lazy, isJSONEncodable } = require('@discordjs/util');
5+
const { DiscordSnowflake } = require('@sapphire/snowflake');
56
const { MessageFlags } = require('discord-api-types/v10');
67
const ActionRowBuilder = require('./ActionRowBuilder');
78
const { DiscordjsError, DiscordjsRangeError, ErrorCodes } = require('../errors');
@@ -133,9 +134,17 @@ class MessagePayload {
133134
}
134135
}
135136

136-
const enforce_nonce = Boolean(this.options.enforceNonce);
137-
if (enforce_nonce && nonce === undefined) {
138-
throw new DiscordjsError(ErrorCodes.MessageNonceRequired);
137+
let enforce_nonce = Boolean(this.options.enforceNonce);
138+
139+
// If `nonce` isn't provided, generate one & set `enforceNonce`
140+
// Unless `enforceNonce` is explicitly set to `false`(not just falsy)
141+
if (nonce === undefined) {
142+
if (this.options.enforceNonce !== false && this.client.options.enforceNonce) {
143+
nonce = DiscordSnowflake.generate().toString();
144+
enforce_nonce = true;
145+
} else if (enforce_nonce) {
146+
throw new DiscordjsError(ErrorCodes.MessageNonceRequired);
147+
}
139148
}
140149

141150
const components = this.options.components?.map(component =>

‎packages/discord.js/src/util/Options.js

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const { version } = require('../../package.json');
4141
* @property {WebsocketOptions} [ws] Options for the WebSocket
4242
* @property {RESTOptions} [rest] Options for the REST manager
4343
* @property {Function} [jsonTransformer] A function used to transform outgoing json data
44+
* @property {boolean} [enforceNonce=false] The default value for {@link MessageReplyOptions#enforceNonce}
4445
*/
4546

4647
/**
@@ -117,6 +118,7 @@ class Options extends null {
117118
makeCache: this.cacheWithLimits(this.DefaultMakeCacheSettings),
118119
partials: [],
119120
failIfNotExists: true,
121+
enforceNonce: false,
120122
presence: {},
121123
sweepers: this.DefaultSweeperSettings,
122124
ws: {

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

+1
Original file line numberDiff line numberDiff line change
@@ -5311,6 +5311,7 @@ export interface ClientOptions {
53115311
ws?: WebSocketOptions;
53125312
rest?: Partial<RESTOptions>;
53135313
jsonTransformer?: (obj: unknown) => unknown;
5314+
enforceNonce?: boolean;
53145315
}
53155316

53165317
export type ClientPresenceStatus = 'online' | 'idle' | 'dnd';

0 commit comments

Comments
 (0)
Please sign in to comment.