Skip to content

Commit

Permalink
Use conditional assignment in more update() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
abalabahaha committed Dec 1, 2019
1 parent db4deb3 commit 3046b24
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 18 deletions.
20 changes: 16 additions & 4 deletions lib/structures/Call.js
Expand Up @@ -22,11 +22,17 @@ class Call extends Base {
this.channel = channel;
this.voiceStates = new Collection(VoiceState);
this.ringing = [];
this.participants = [];
this.region = null;
this.endedTimestamp = null;
this.unavailable = true;
this.update(data);
}

update(data) {
this.participants = data.participants !== undefined ? data.participants : this.participants || [];
if(data.participants !== undefined) {
this.participants = data.participants;
}
if(data.ringing !== undefined) {
if(!this.ringing.includes(this.channel._client.user.id) && (this.ringing = data.ringing).includes(this.channel._client.user.id)) {
/**
Expand All @@ -37,9 +43,15 @@ class Call extends Base {
this.channel._client.emit("callRing", this);
}
}
this.region = data.region !== undefined ? data.region : this.region || null;
this.endedTimestamp = data.ended_timestamp !== undefined ? Date.parse(data.ended_timestamp) : this.endedTimestamp || null;
this.unavailable = data.unavailable !== undefined ? data.unavailable : this.unavailable !== undefined ? this.unavailable : true;
if(data.region !== undefined) {
this.region = data.region;
}
if(data.ended_timestamp !== undefined) {
this.endedTimestamp = Date.parse(data.ended_timestamp);
}
if(data.unavailable !== undefined) {
this.unavailable = data.unavailable;
}
if(data.voice_states) {
data.voice_states.forEach((voiceState) => {
voiceState.id = voiceState.user_id;
Expand Down
19 changes: 13 additions & 6 deletions lib/structures/Member.js
Expand Up @@ -54,12 +54,19 @@ class Member extends Base {
} else {
this.user = null;
}
this.status = "offline";
this.game = null;
this.nick = null;
this.update(data);
}

update(data) {
this.status = data.status !== undefined ? data.status : this.status || "offline";
this.game = data.game !== undefined ? data.game : this.game || null;
if(data.status !== undefined) {
this.status = data.status;
}
if(data.game !== undefined) {
this.game = data.game;
}
if(data.joined_at !== undefined) {
this.joinedAt = Date.parse(data.joined_at);
}
Expand All @@ -72,8 +79,7 @@ class Member extends Base {
if(data.premium_since !== undefined) {
this.premiumSince = data.premium_since;
}

if("mute" in data) {
if(data.hasOwnProperty("mute")) {
const state = this.guild.voiceStates.get(this.id);
if(data.channel_id === null && !data.mute && !data.deaf && !data.suppress) {
this.guild.voiceStates.delete(this.id);
Expand All @@ -83,8 +89,9 @@ class Member extends Base {
this.guild.voiceStates.update(data);
}
}

this.nick = data.nick !== undefined ? data.nick : this.nick || null;
if(data.nick !== undefined) {
this.nick = data.nick;
}
if(data.roles !== undefined) {
this.roles = data.roles;
}
Expand Down
15 changes: 12 additions & 3 deletions lib/structures/Relationship.js
Expand Up @@ -16,13 +16,22 @@ class Relationship extends Base {
constructor(data, client) {
super(data.id);
this.user = client.users.add(data.user, client);
this.type = 0;
this.status = "offline";
this.game = null;
this.update(data);
}

update(data) {
this.type = data.type !== undefined ? data.type : this.type || 0;
this.status = data.status !== undefined ? data.status : this.status || "offline";
this.game = data.game !== undefined ? data.game : this.game || null;
if(data.type !== undefined) {
this.type = data.type;
}
if(data.status !== undefined) {
this.status = data.status;
}
if(data.game !== undefined) {
this.game = data.game;
}
}

toJSON(props = []) {
Expand Down
25 changes: 20 additions & 5 deletions lib/structures/VoiceState.js
Expand Up @@ -17,6 +17,11 @@ const Base = require("./Base");
class VoiceState extends Base {
constructor(data) {
super(data.id);
this.mute = false;
this.deaf = false;
this.suppress = false;
this.selfMute = false;
this.selfDeaf = false;
this.update(data);
}

Expand All @@ -27,11 +32,21 @@ class VoiceState extends Base {
} else if(this.channelID === undefined) {
this.channelID = this.sessionID = null;
}
this.mute = data.mute !== undefined ? data.mute : this.mute || false;
this.deaf = data.deaf !== undefined ? data.deaf : this.deaf || false;
this.suppress = data.suppress !== undefined ? data.suppress : this.suppress || false; // Bots ignore this
this.selfMute = data.self_mute !== undefined ? data.self_mute : this.selfMute || false;
this.selfDeaf = data.self_deaf !== undefined ? data.self_deaf : this.selfDeaf || false;
if(data.mute !== undefined) {
this.mute = data.mute;
}
if(data.deaf !== undefined) {
this.deaf = data.deaf;
}
if(data.suppress !== undefined) { // Bots ignore this
this.suppress = data.suppress;
}
if(data.self_mute !== undefined) {
this.selfMute = data.self_mute;
}
if(data.self_deaf !== undefined) {
this.selfDeaf = data.self_deaf;
}
this.selfStream = data.self_stream || false;
}

Expand Down

0 comments on commit 3046b24

Please sign in to comment.