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: Add support for Automated Message nonce handling #10381

Merged
merged 20 commits into from
Aug 19, 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
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);
}
almeidx marked this conversation as resolved.
Show resolved Hide resolved
}

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
Loading