diff --git a/packages/discord.js/src/client/Client.js b/packages/discord.js/src/client/Client.js index 0cf4c2543fd6..705c8c937e54 100644 --- a/packages/discord.js/src/client/Client.js +++ b/packages/discord.js/src/client/Client.js @@ -152,6 +152,8 @@ class Client extends BaseClient { this.token = null; } + Object.defineProperty(this, '_censoredToken', { value: null, writable: true }); + /** * User that the client is logged in as * @type {?ClientUser} @@ -214,13 +216,7 @@ class Client extends BaseClient { if (!token || typeof token !== 'string') throw new DiscordjsError(ErrorCodes.TokenInvalid); this.token = token = token.replace(/^(Bot|Bearer)\s*/i, ''); this.rest.setToken(token); - this.emit( - Events.Debug, - `Provided token: ${token - .split('.') - .map((val, i) => (i > 1 ? val.replace(/./g, '*') : val)) - .join('.')}`, - ); + this.emit(Events.Debug, `Provided token: ${this._getCensoredToken()}`); if (this.options.presence) { this.options.ws.presence = this.presence._parse(this.options.presence); @@ -256,6 +252,7 @@ class Client extends BaseClient { this.sweepers.destroy(); this.ws.destroy(); this.token = null; + this._censoredToken = null; this.rest.setToken(null); } @@ -470,6 +467,27 @@ class Client extends BaseClient { return eval(script); } + /** + * Partially censors the client token for debug logging purposes. + * @returns {?string} + * @private + */ + _getCensoredToken() { + if (!this.token) return null; + + /** + * The cached censored client token + * @type {?string} + * @private + */ + this._censoredToken ??= this.token + .split('.') + .map((val, i) => (i > 1 ? val.replace(/./g, '*') : val)) + .join('.'); + + return this._censoredToken; + } + /** * Validates the client options. * @param {ClientOptions} [options=this.options] Options to validate diff --git a/packages/discord.js/src/client/websocket/WebSocketShard.js b/packages/discord.js/src/client/websocket/WebSocketShard.js index 5683938f939d..969fabe5c849 100644 --- a/packages/discord.js/src/client/websocket/WebSocketShard.js +++ b/packages/discord.js/src/client/websocket/WebSocketShard.js @@ -740,7 +740,12 @@ class WebSocketShard extends EventEmitter { */ _send(data) { if (this.connection?.readyState !== WebSocket.OPEN) { - this.debug(`Tried to send packet '${JSON.stringify(data)}' but no WebSocket is available!`); + this.debug( + `Tried to send packet '${JSON.stringify(data).replaceAll( + this.manager.client.token, + this.manager.client._getCensoredToken, + )}' but no WebSocket is available!`, + ); this.destroy({ closeCode: 4_000 }); return; } diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index e2586331a58d..f6cac6adab56 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -762,9 +762,11 @@ export type If = T extends true ? A : T extends export class Client extends BaseClient { public constructor(options: ClientOptions); + private _censoredToken: string | null; private actions: unknown; private presence: ClientPresence; private _eval(script: string): unknown; + private _getCensoredToken(): string | null; private _validateOptions(options: ClientOptions): void; public application: If;