Skip to content

Commit

Permalink
refactor: rename ws.presence option to ws.initialPresence
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx committed Aug 4, 2024
1 parent f623e7a commit 9e68f34
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
17 changes: 11 additions & 6 deletions packages/discord.js/src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Client extends BaseClient {
* @private
* @type {ClientPresence}
*/
this.presence = new ClientPresence(this, this.options.ws.presence ?? this.options.presence);
this.presence = new ClientPresence(this, this.options.ws.initialPresence ?? this.options.presence);

Object.defineProperty(this, 'token', { writable: true });
if (!this.token && 'DISCORD_TOKEN' in process.env) {
Expand Down Expand Up @@ -219,17 +219,19 @@ class Client extends BaseClient {
this.rest.setToken(token);
this.emit(Events.Debug, `Provided token: ${this._censoredToken}`);

if (this.options.presence) {
if (!this.options.ws.initialPresence && this.options.presence && Object.keys(this.options.presence).length > 0) {
if (!deprecationEmittedForClientPresence) {
process.emitWarning(
'ClientOptions#presence is deprecated and will be removed. Use ClientOptions#ws#presence instead.',
'ClientOptions#presence is deprecated and will be removed. Use ClientOptions#ws#initialPresence instead.',
'DeprecationWarning',
);

deprecationEmittedForClientPresence = true;
}

this.options.ws.presence = this.presence._parse(this.options.presence);
this.options.ws.initialPresence = this.presence._parse(this.options.presence);
} else if (this.options.ws.initialPresence) {
this.options.ws.initialPresence = this.presence._parse(this.options.ws.initialPresence);
}

this.emit(Events.Debug, 'Preparing to connect to the gateway...');
Expand Down Expand Up @@ -557,8 +559,11 @@ class Client extends BaseClient {
if (typeof options.ws !== 'object' || options.ws === null) {
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'ws', 'an object');
}
if (typeof options.ws.presence !== 'object' || options.ws.presence === null) {
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'ws.presence', 'an object');
if (
'initialPresence' in options.ws &&
(typeof options.ws.initialPresence !== 'object' || options.ws.initialPresence === null)
) {
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'ws.initialPresence', 'an object');
}
if (typeof options.rest !== 'object' || options.rest === null) {
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'rest', 'an object');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class WebSocketManager extends EventEmitter {
version: ws.version,
shardIds: shards === 'auto' ? null : shards,
shardCount: shards === 'auto' ? null : shardCount,
initialPresence: ws.presence,
initialPresence: ws.initialPresence,
retrieveSessionInfo: shardId => this.shards.get(shardId).sessionInfo,
updateSessionInfo: (shardId, sessionInfo) => {
this.shards.get(shardId).sessionInfo = sessionInfo;
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/util/Options.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const { version } = require('../../package.json');
* only set this if you know what you are doing</warn>
* @property {BuildStrategyFunction} [buildStrategy] Builds the strategy to use for sharding
* @property {IdentifyThrottlerFunction} [buildIdentifyThrottler] Builds the identify throttler to use for sharding
* @property {PresenceData} [presence={}] Presence data to use upon login
* @property {PresenceData} [initialPresence] Presence data to use upon login
*/

/**
Expand Down
5 changes: 2 additions & 3 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ import {
APISelectMenuDefaultValue,
SelectMenuDefaultValueType,
InviteType,
GatewayPresenceUpdateData,
} from 'discord-api-types/v10';
import { ChildProcess } from 'node:child_process';
import { EventEmitter } from 'node:events';
Expand Down Expand Up @@ -5305,7 +5304,7 @@ export interface ClientOptions {
allowedMentions?: MessageMentionOptions;
partials?: readonly Partials[];
failIfNotExists?: boolean;
/** @deprecated Use {@link ClientOptions.ws.presence} instead */
/** @deprecated Use {@link ClientOptions.ws.initialPresence} instead */
presence?: PresenceData;
intents: BitFieldResolvable<GatewayIntentsString, number>;
waitGuildTimeout?: number;
Expand Down Expand Up @@ -6878,7 +6877,7 @@ export interface WebhookMessageCreateOptions extends Omit<MessageCreateOptions,
export interface WebSocketOptions {
large_threshold?: number;
version?: number;
presence?: GatewayPresenceUpdateData;
initialPresence?: PresenceData;
buildStrategy?(manager: WSWebSocketManager): IShardingStrategy;
buildIdentifyThrottler?(manager: WSWebSocketManager): Awaitable<IIdentifyThrottler>;
}
Expand Down

0 comments on commit 9e68f34

Please sign in to comment.