Skip to content

Commit

Permalink
fix: don't patch set data with undefined (#6694)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImRodry committed Oct 3, 2021
1 parent 8b4456e commit 9eb9591
Show file tree
Hide file tree
Showing 33 changed files with 1,233 additions and 817 deletions.
63 changes: 37 additions & 26 deletions src/structures/AnonymousGuild.js
Expand Up @@ -15,36 +15,47 @@ class AnonymousGuild extends BaseGuild {
}

_patch(data) {
this.features = data.features;
/**
* The hash of the guild invite splash image
* @type {?string}
*/
this.splash = data.splash;
if ('features' in data) this.features = data.features;

/**
* The hash of the guild banner
* @type {?string}
*/
this.banner = data.banner;
if ('splash' in data) {
/**
* The hash of the guild invite splash image
* @type {?string}
*/
this.splash = data.splash;
}

if ('banner' in data) {
/**
* The hash of the guild banner
* @type {?string}
*/
this.banner = data.banner;
}

/**
* The description of the guild, if any
* @type {?string}
*/
this.description = data.description;
if ('description' in data) {
/**
* The description of the guild, if any
* @type {?string}
*/
this.description = data.description;
}

/**
* The verification level of the guild
* @type {VerificationLevel}
*/
this.verificationLevel = VerificationLevels[data.verification_level];
if ('verification_level' in data) {
/**
* The verification level of the guild
* @type {VerificationLevel}
*/
this.verificationLevel = VerificationLevels[data.verification_level];
}

/**
* The vanity invite code of the guild, if any
* @type {?string}
*/
this.vanityURLCode = data.vanity_url_code;
if ('vanity_url_code' in data) {
/**
* The vanity invite code of the guild, if any
* @type {?string}
*/
this.vanityURLCode = data.vanity_url_code;
}

if ('nsfw_level' in data) {
/**
Expand Down
62 changes: 37 additions & 25 deletions src/structures/ApplicationCommand.js
Expand Up @@ -54,35 +54,47 @@ class ApplicationCommand extends Base {
}

_patch(data) {
/**
* The name of this command
* @type {string}
*/
this.name = data.name;
if ('name' in data) {
/**
* The name of this command
* @type {string}
*/
this.name = data.name;
}

/**
* The description of this command
* @type {string}
*/
this.description = data.description;
if ('description' in data) {
/**
* The description of this command
* @type {string}
*/
this.description = data.description;
}

/**
* The options of this command
* @type {ApplicationCommandOption[]}
*/
this.options = data.options?.map(o => this.constructor.transformOption(o, true)) ?? [];
if ('options' in data) {
/**
* The options of this command
* @type {ApplicationCommandOption[]}
*/
this.options = data.options.map(o => this.constructor.transformOption(o, true));
} else {
this.options ??= [];
}

/**
* Whether the command is enabled by default when the app is added to a guild
* @type {boolean}
*/
this.defaultPermission = data.default_permission;
if ('default_permission' in data) {
/**
* Whether the command is enabled by default when the app is added to a guild
* @type {boolean}
*/
this.defaultPermission = data.default_permission;
}

/**
* Autoincrementing version identifier updated during substantial record changes
* @type {Snowflake}
*/
this.version = data.version;
if ('version' in data) {
/**
* Autoincrementing version identifier updated during substantial record changes
* @type {Snowflake}
*/
this.version = data.version;
}
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/structures/BaseGuildEmoji.js
Expand Up @@ -25,25 +25,25 @@ class BaseGuildEmoji extends Emoji {
}

_patch(data) {
if (data.name) this.name = data.name;
if ('name' in data) this.name = data.name;

if (typeof data.require_colons !== 'undefined') {
if ('require_colons' in data) {
/**
* Whether or not this emoji requires colons surrounding it
* @type {?boolean}
*/
this.requiresColons = data.require_colons;
}

if (typeof data.managed !== 'undefined') {
if ('managed' in data) {
/**
* Whether this emoji is managed by an external service
* @type {?boolean}
*/
this.managed = data.managed;
}

if (typeof data.available !== 'undefined') {
if ('available' in data) {
/**
* Whether this emoji is available
* @type {?boolean}
Expand Down
36 changes: 21 additions & 15 deletions src/structures/BaseGuildVoiceChannel.js
Expand Up @@ -12,23 +12,29 @@ class BaseGuildVoiceChannel extends GuildChannel {
_patch(data) {
super._patch(data);

/**
* The RTC region for this voice-based channel. This region is automatically selected if `null`.
* @type {?string}
*/
this.rtcRegion = data.rtc_region;
if ('rtc_region' in data) {
/**
* The RTC region for this voice-based channel. This region is automatically selected if `null`.
* @type {?string}
*/
this.rtcRegion = data.rtc_region;
}

/**
* The bitrate of this voice-based channel
* @type {number}
*/
this.bitrate = data.bitrate;
if ('bitrate' in data) {
/**
* The bitrate of this voice-based channel
* @type {number}
*/
this.bitrate = data.bitrate;
}

/**
* The maximum amount of users allowed in this channel.
* @type {number}
*/
this.userLimit = data.user_limit;
if ('user_limit' in data) {
/**
* The maximum amount of users allowed in this channel.
* @type {number}
*/
this.userLimit = data.user_limit;
}
}

/**
Expand Down
68 changes: 43 additions & 25 deletions src/structures/ClientApplication.js
Expand Up @@ -23,35 +23,53 @@ class ClientApplication extends Application {
_patch(data) {
super._patch(data);

/**
* The flags this application has
* @type {ApplicationFlags}
*/
this.flags = 'flags' in data ? new ApplicationFlags(data.flags).freeze() : this.flags;
if ('flags' in data) {
/**
* The flags this application has
* @type {ApplicationFlags}
*/
this.flags = new ApplicationFlags(data.flags).freeze();
}

/**
* The hash of the application's cover image
* @type {?string}
*/
this.cover = data.cover_image ?? this.cover ?? null;
if ('cover_image' in data) {
/**
* The hash of the application's cover image
* @type {?string}
*/
this.cover = data.cover_image;
} else {
this.cover ??= null;
}

/**
* The application's RPC origins, if enabled
* @type {string[]}
*/
this.rpcOrigins = data.rpc_origins ?? this.rpcOrigins ?? [];
if ('rpc_origins' in data) {
/**
* The application's RPC origins, if enabled
* @type {string[]}
*/
this.rpcOrigins = data.rpc_origins;
} else {
this.rpcOrigins ??= [];
}

/**
* If this application's bot requires a code grant when using the OAuth2 flow
* @type {?boolean}
*/
this.botRequireCodeGrant = data.bot_require_code_grant ?? this.botRequireCodeGrant ?? null;
if ('bot_require_code_grant' in data) {
/**
* If this application's bot requires a code grant when using the OAuth2 flow
* @type {?boolean}
*/
this.botRequireCodeGrant = data.bot_require_code_grant;
} else {
this.botRequireCodeGrant ??= null;
}

/**
* If this application's bot is public
* @type {?boolean}
*/
this.botPublic = data.bot_public ?? this.botPublic ?? null;
if ('bot_public' in data) {
/**
* If this application's bot is public
* @type {?boolean}
*/
this.botPublic = data.bot_public;
} else {
this.botPublic ??= null;
}

/**
* The owner of this OAuth application
Expand Down
2 changes: 1 addition & 1 deletion src/structures/ClientUser.js
Expand Up @@ -29,7 +29,7 @@ class ClientUser extends User {
this.mfaEnabled ??= null;
}

if (data.token) this.client.token = data.token;
if ('token' in data) this.client.token = data.token;
}

/**
Expand Down
26 changes: 16 additions & 10 deletions src/structures/DMChannel.js
Expand Up @@ -38,17 +38,23 @@ class DMChannel extends Channel {
this.recipient = this.client.users._add(data.recipients[0]);
}

/**
* The channel's last message id, if one was sent
* @type {?Snowflake}
*/
this.lastMessageId = data.last_message_id;
if ('last_message_id' in data) {
/**
* The channel's last message id, if one was sent
* @type {?Snowflake}
*/
this.lastMessageId = data.last_message_id;
}

/**
* The timestamp when the last pinned message was pinned, if there was one
* @type {?number}
*/
this.lastPinTimestamp = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null;
if ('last_pin_timestamp' in data) {
/**
* The timestamp when the last pinned message was pinned, if there was one
* @type {?number}
*/
this.lastPinTimestamp = new Date(data.last_pin_timestamp).getTime();
} else {
this.lastPinTimestamp ??= null;
}
}

/**
Expand Down

0 comments on commit 9eb9591

Please sign in to comment.