Skip to content

Commit

Permalink
feat: Add support for Automated Message nonce handling (#10381)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Aug 19, 2024
1 parent 8fb4008 commit 2ca187b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/discord.js/src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions packages/discord.js/src/structures/MessagePayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 =>
Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/src/util/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/

/**
Expand Down Expand Up @@ -117,6 +118,7 @@ class Options extends null {
makeCache: this.cacheWithLimits(this.DefaultMakeCacheSettings),
partials: [],
failIfNotExists: true,
enforceNonce: false,
presence: {},
sweepers: this.DefaultSweeperSettings,
ws: {
Expand Down
1 change: 1 addition & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5311,6 +5311,7 @@ export interface ClientOptions {
ws?: WebSocketOptions;
rest?: Partial<RESTOptions>;
jsonTransformer?: (obj: unknown) => unknown;
enforceNonce?: boolean;
}

export type ClientPresenceStatus = 'online' | 'idle' | 'dnd';
Expand Down

0 comments on commit 2ca187b

Please sign in to comment.