Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce memory usage of voice states #548

Merged
merged 1 commit into from Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Role = require("./Role");
const TextChannel = require("./TextChannel");
const NewsChannel = require("./NewsChannel");
const VoiceChannel = require("./VoiceChannel");
const VoiceState = require("./VoiceState");

/**
* Represents a guild
Expand All @@ -32,6 +33,7 @@ const VoiceChannel = require("./VoiceChannel");
* @prop {Boolean} unavailable Whether the guild is unavailable or not
* @prop {Boolean} large Whether the guild is "large" by "some Discord standard"
* @prop {Number} maxPresences The maximum number of people that can be online in a guild at once (returned from REST API only)
* @prop {Collection<VoiceState>} voiceStates Collection of voice states in the guild
* @prop {Collection<GuildChannel>} channels Collection of Channels in the guild
* @prop {Collection<Member>} members Collection of Members in the guild
* @prop {Number} memberCount Number of members in the guild
Expand All @@ -54,6 +56,7 @@ class Guild extends Base {
this.shard = client.shards.get(client.guildShardMap[this.id]);
this.unavailable = !!data.unavailable;
this.joinedAt = Date.parse(data.joined_at);
this.voiceStates = new Collection(VoiceState);
this.channels = new Collection(GuildChannel);
this.members = new Collection(Member);
this.memberCount = data.member_count;
Expand Down
18 changes: 15 additions & 3 deletions lib/structures/Member.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class Member extends Base {
} else {
this.user = null;
}
this.voiceState = new VoiceState(data);
this.update(data);
}

Expand All @@ -66,8 +65,15 @@ class Member extends Base {
this.activities = data.activities;
this.premiumSince = data.premium_since !== undefined ? data.premium_since : this.premiumSince;

if(data.mute !== undefined) {
this.voiceState.update(data);
if("mute" in data) {
const state = this.guild.voiceStates.get(this.id);
if(data.channel_id === null && !data.mute && !data.deaf && !data.suppress && !data.self_deaf && !data.self_mute) {
this.guild.voiceStates.delete(this.id);
} else if(state) {
state.update(data);
} else if(data.channel_id || data.mute || data.deaf || data.suppress || data.self_mute || data.self_deaf) {
this.guild.voiceStates.update(data);
}
}

this.nick = data.nick !== undefined ? data.nick : this.nick || null;
Expand All @@ -76,6 +82,12 @@ class Member extends Base {
}
}

get voiceState() {
return this.guild.voiceStates.get(this.id) || new VoiceState({
id: this.id
});
}

get permission() {
if(this.id === this.guild.ownerID) {
return new Permission(Permissions.all);
Expand Down