Skip to content

Commit

Permalink
Conditionally update struct properties (#568)
Browse files Browse the repository at this point in the history
  • Loading branch information
Khaaz authored and abalabahaha committed Nov 23, 2019
1 parent c323213 commit 3bdeb5b
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 59 deletions.
8 changes: 4 additions & 4 deletions lib/structures/ExtendedUser.js
Expand Up @@ -17,10 +17,10 @@ class ExtendedUser extends User {

update(data) {
super.update(data);
this.email = data.email !== undefined ? data.email : this.email;
this.verified = data.verified !== undefined ? data.verified : this.verified;
this.mfaEnabled = data.mfa_enabled !== undefined ? data.mfa_enabled : this.mfaEnabled;
this.premium = data.premium !== undefined ? data.premium : this.premium;
if(data.email !== undefined) this.email = data.email;
if(data.verified !== undefined) this.verified = data.verified;
if(data.mfa_enabled !== undefined) this.mfaEnabled = data.mfa_enabled;
if(data.premium !== undefined) this.premium = data.premium;
}

toJSON() {
Expand Down
6 changes: 3 additions & 3 deletions lib/structures/GroupChannel.js
Expand Up @@ -30,9 +30,9 @@ class GroupChannel extends PrivateChannel { // (╯°□°)╯︵ ┻━┻
}

update(data) {
this.name = data.name !== undefined ? data.name : this.name;
this.ownerID = data.owner_id !== undefined ? data.owner_id : this.ownerID;
this.icon = data.icon !== undefined ? data.icon : this.icon;
if(data.name !== undefined) this.name = data.name;
if(data.owner_id !== undefined) this.ownerID = data.owner_id;
if(data.icon !== undefined) this.icon = data.icon;
}

/**
Expand Down
46 changes: 23 additions & 23 deletions lib/structures/Guild.js
Expand Up @@ -122,29 +122,29 @@ class Guild extends Base {
}

update(data) {
this.name = data.name !== undefined ? data.name : this.name;
this.verificationLevel = data.verification_level !== undefined ? data.verification_level : this.verificationLevel;
this.splash = data.splash !== undefined ? data.splash : this.splash;
this.banner = data.banner !== undefined ? data.banner : this.banner;
this.region = data.region !== undefined ? data.region : this.region;
this.ownerID = data.owner_id !== undefined ? data.owner_id : this.ownerID;
this.icon = data.icon !== undefined ? data.icon : this.icon;
this.features = data.features !== undefined ? data.features : this.features;
this.emojis = data.emojis !== undefined ? data.emojis : this.emojis;
this.afkChannelID = data.afk_channel_id !== undefined ? data.afk_channel_id : this.afkChannelID;
this.afkTimeout = data.afk_timeout !== undefined ? data.afk_timeout : this.afkTimeout;
this.defaultNotifications = data.default_message_notifications !== undefined ? data.default_message_notifications : this.defaultNotifications;
this.mfaLevel = data.mfa_level !== undefined ? data.mfa_level : this.mfaLevel;
this.large = data.large !== undefined ? data.large : this.large;
this.maxPresences = data.max_presences !== undefined ? data.max_presences : this.maxPresences;
this.explicitContentFilter = data.explicit_content_filter !== undefined ? data.explicit_content_filter : this.explicitContentFilter;
this.systemChannelID = data.system_channel_id !== undefined ? data.system_channel_id : this.systemChannelID;
this.premiumTier = data.premium_tier !== undefined ? data.premium_tier : this.premiumTier;
this.premiumSubscriptionCount = data.premium_subscription_count !== undefined ? data.premium_subscription_count : this.premiumSubscriptionCount;
this.vanityURL = data.vanity_url_code !== undefined ? data.vanity_url_code : this.vanityURL;
this.preferredLocale = data.preferred_locale !== undefined ? data.preferred_locale : this.preferredLocale;
this.description = data.description !== undefined ? data.description : this.description;
this.maxMembers = data.max_members !== undefined ? data.max_members : this.maxMembers;
if(data.name !== undefined) this.name = data.name;
if(data.verification_level !== undefined) this.verificationLevel = data.verification_level;
if(data.splash !== undefined) this.splash = data.splash;
if(data.banner !== undefined) this.banner = data.banner;
if(data.region !== undefined) this.region = data.region;
if(data.owner_id !== undefined) this.ownerID = data.owner_id;
if(data.icon !== undefined) this.icon = data.icon;
if(data.features !== undefined) this.features = data.features;
if(data.emojis !== undefined) this.emojis = data.emojis;
if(data.afk_channel_id !== undefined) this.afkChannelID = data.afk_channel_id;
if(data.afk_timeout !== undefined) this.afkTimeout = data.afk_timeout;
if(data.default_message_notifications !== undefined) this.defaultNotifications = data.default_message_notifications;
if(data.mfa_level !== undefined) this.mfaLevel = data.mfa_level;
if(data.large !== undefined) this.large = data.large;
if(data.max_presences !== undefined) this.maxPresences = data.max_presences;
if(data.explicit_content_filter !== undefined) this.explicitContentFilter = data.explicit_content_filter;
if(data.system_channel_id !== undefined) this.systemChannelID = data.system_channel_id;
if(data.premium_tier !== undefined) this.premiumTier = data.premium_tier;
if(data.premium_subscription_count !== undefined) this.premiumSubscriptionCount = data.premium_subscription_count;
if(data.vanity_url_code !== undefined) this.vanityURL = data.vanity_url_code;
if(data.preferred_locale !== undefined) this.preferredLocale = data.preferred_locale;
if(data.description !== undefined) this.description = data.description;
if(data.max_members !== undefined) this.maxMembers = data.max_members;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions lib/structures/GuildChannel.js
Expand Up @@ -30,10 +30,10 @@ class GuildChannel extends Channel {
}

update(data) {
this.type = data.type !== undefined ? data.type : this.type;
this.name = data.name !== undefined ? data.name : this.name;
this.position = data.position !== undefined ? data.position : this.position;
this.parentID = data.parent_id !== undefined ? data.parent_id : this.parentID;
if(data.type !== undefined) this.type = data.type;
if(data.name !== undefined) this.name = data.name;
if(data.position !== undefined) this.position = data.position;
if(data.parent_id !== undefined) this.parentID = data.parent_id;
this.nsfw = (this.name.length === 4 ? this.name === "nsfw" : this.name.startsWith("nsfw-")) || data.nsfw;
if(data.permission_overwrites) {
this.permissionOverwrites = new Collection(PermissionOverwrite);
Expand Down
8 changes: 4 additions & 4 deletions lib/structures/Member.js
Expand Up @@ -60,10 +60,10 @@ class Member extends Base {
update(data) {
this.status = data.status !== undefined ? data.status : this.status || "offline";
this.game = data.game !== undefined ? data.game : this.game || null;
this.joinedAt = data.joined_at !== undefined ? Date.parse(data.joined_at) : this.joinedAt;
this.clientStatus = data.client_status !== undefined ? Object.assign({web: "offline", desktop: "offline", mobile: "offline"}, data.client_status) : this.clientStatus;
this.activities = data.activities !== undefined ? data.activities : this.activities;
this.premiumSince = data.premium_since !== undefined ? data.premium_since : this.premiumSince;
if(data.joined_at !== undefined) this.joinedAt = Date.parse(data.joined_at);
if(data.client_status !== undefined) this.clientStatus = Object.assign({web: "offline", desktop: "offline", mobile: "offline"}, data.client_status);
if(data.activities !== undefined) this.activities = data.activities;
if(data.premium_since !== undefined) this.premiumSince = data.premium_since;

if("mute" in data) {
const state = this.guild.voiceStates.get(this.id);
Expand Down
14 changes: 7 additions & 7 deletions lib/structures/Message.js
Expand Up @@ -150,13 +150,13 @@ class Message extends Base {
this.roleMentions = data.mention_roles;
}

this.pinned = data.pinned !== undefined ? !!data.pinned : this.pinned;
this.editedTimestamp = data.edited_timestamp != undefined ? Date.parse(data.edited_timestamp) : this.editedTimestamp;
this.tts = data.tts !== undefined ? data.tts : this.tts;
this.attachments = data.attachments !== undefined ? data.attachments : this.attachments;
this.embeds = data.embeds !== undefined ? data.embeds : this.embeds;
this.activity = data.activity !== undefined ? data.activity : this.activity;
this.application = data.application !== undefined ? data.application : this.application;
if(data.pinned !== undefined) this.pinned = !!data.pinned;
if(data.edited_timestamp != undefined) this.editedTimestamp = Date.parse(data.edited_timestamp);
if(data.tts !== undefined) this.tts = data.tts;
if(data.attachments !== undefined) this.attachments = data.attachments;
if(data.embeds !== undefined) this.embeds = data.embeds;
if(data.activity !== undefined) this.activity = data.activity;
if(data.application !== undefined) this.application = data.application;

if(data.reactions) {
data.reactions.forEach((reaction) => {
Expand Down
14 changes: 7 additions & 7 deletions lib/structures/Role.js
Expand Up @@ -26,13 +26,13 @@ class Role extends Base {
}

update(data) {
this.name = data.name !== undefined ? data.name : this.name;
this.mentionable = data.mentionable !== undefined ? data.mentionable : this.mentionable;
this.managed = data.managed !== undefined ? data.managed : this.managed;
this.hoist = data.hoist !== undefined ? data.hoist : this.hoist;
this.color = data.color !== undefined ? data.color : this.color;
this.position = data.position !== undefined ? data.position : this.position;
this.permissions = data.permissions !== undefined ? new Permission(data.permissions) : this.permissions;
if(data.name !== undefined) this.name = data.name;
if(data.mentionable !== undefined) this.mentionable = data.mentionable;
if(data.managed !== undefined) this.managed = data.managed;
if(data.hoist !== undefined) this.hoist = data.hoist;
if(data.color !== undefined) this.color = data.color;
if(data.position !== undefined) this.position = data.position;
if(data.permissions !== undefined) this.permissions = new Permission(data.permissions);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/structures/TextChannel.js
Expand Up @@ -34,8 +34,8 @@ class TextChannel extends GuildChannel {

update(data) {
super.update(data);
this.rateLimitPerUser = data.rate_limit_per_user !== undefined ? data.rate_limit_per_user : this.rateLimitPerUser;
this.topic = data.topic !== undefined ? data.topic : this.topic;
if(data.rate_limit_per_user !== undefined) this.rateLimitPerUser = data.rate_limit_per_user;
if(data.topic !== undefined) this.topic = data.topic;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/structures/User.js
Expand Up @@ -31,9 +31,9 @@ class User extends Base {
}

update(data) {
this.avatar = data.avatar !== undefined ? data.avatar : this.avatar;
this.username = data.username !== undefined ? data.username : this.username;
this.discriminator = data.discriminator !== undefined ? data.discriminator : this.discriminator;
if(data.avatar !== undefined) this.avatar = data.avatar;
if(data.username !== undefined) this.username = data.username;
if(data.discriminator !== undefined) this.discriminator = data.discriminator;
}

get mention() {
Expand Down
4 changes: 2 additions & 2 deletions lib/structures/VoiceChannel.js
Expand Up @@ -30,8 +30,8 @@ class VoiceChannel extends GuildChannel {
update(data) {
super.update(data);

this.bitrate = data.bitrate !== undefined ? data.bitrate : this.bitrate;
this.userLimit = data.user_limit !== undefined ? data.user_limit : this.userLimit;
if(data.bitrate !== undefined) this.bitrate = data.bitrate;
if(data.user_limit !== undefined) this.userLimit = data.user_limit;
}

/**
Expand Down

0 comments on commit 3bdeb5b

Please sign in to comment.