Skip to content

Commit

Permalink
fix(GuildEmojiRoleStore): do not prematurely patch roles
Browse files Browse the repository at this point in the history
Issue is the same as in #2312 and #2381, but for the GuildEmojiRoleStore.
Thanks to @KingDGrizzle for pointing this out.
  • Loading branch information
SpaceEEC committed Mar 16, 2018
1 parent acd1740 commit d041cb2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/client/actions/GuildEmojisUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class GuildEmojisUpdateAction extends Action {
const cachedEmoji = guild.emojis.get(emoji.id);
if (cachedEmoji) {
deletions.delete(emoji.id);
if (!cachedEmoji.equals(emoji, true)) {
if (!cachedEmoji.equals(emoji)) {
// Emoji updated
this.client.actions.GuildEmojiUpdate.handle(cachedEmoji, emoji);
}
Expand Down
18 changes: 10 additions & 8 deletions src/stores/GuildEmojiRoleStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ class GuildEmojiRoleStore extends DataStore {
if (roleOrRoles.includes(null)) {
return Promise.reject(new TypeError('INVALID_TYPE', 'roles',
'Array or Collection of Roles or Snowflakes', true));
} else {
for (const role of roleOrRoles) super.set(role.id, role);
}

return this.set(this);
const newRoles = [...new Set(roleOrRoles.concat(this.array()))];
return this.set(newRoles);
}

/**
Expand All @@ -48,11 +46,9 @@ class GuildEmojiRoleStore extends DataStore {
if (roleOrRoles.includes(null)) {
return Promise.reject(new TypeError('INVALID_TYPE', 'roles',
'Array or Collection of Roles or Snowflakes', true));
} else {
for (const role of roleOrRoles) super.remove(role);
}

return this.set(this);
const newRoles = this.keyArray().filter(role => !roleOrRoles.includes(role));
return this.set(newRoles);
}

/**
Expand All @@ -74,6 +70,12 @@ class GuildEmojiRoleStore extends DataStore {
return this.emoji.edit({ roles });
}

clone() {
const clone = new this.constructor(this.emoji);
clone._patch(this.keyArray());
return clone;
}

/**
* Patches the roles for this store
* @param {Snowflake[]} roles The new roles
Expand Down
14 changes: 13 additions & 1 deletion src/structures/GuildEmoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class GuildEmoji extends Emoji {
if (data.roles) this.roles._patch(data.roles);
}

_clone() {
const clone = super._clone();
clone.roles = this.roles.clone();
return clone;
}

/**
* The timestamp the emoji was created at
* @type {number}
Expand Down Expand Up @@ -94,7 +100,11 @@ class GuildEmoji extends Emoji {
name: data.name,
roles: data.roles ? data.roles.map(r => r.id ? r.id : r) : undefined,
}, reason })
.then(() => this);
.then(() => {
const clone = this._clone();
clone._patch(data);
return clone;
});
}

/**
Expand Down Expand Up @@ -129,12 +139,14 @@ class GuildEmoji extends Emoji {
other.name === this.name &&
other.managed === this.managed &&
other.requiresColons === this.requiresColons &&
other.roles.size === this.roles.size &&
other.roles.every(role => this.roles.has(role.id))
);
} else {
return (
other.id === this.id &&
other.name === this.name &&
other.roles.length === this.roles.size &&
other.roles.every(role => this.roles.has(role))
);
}
Expand Down

0 comments on commit d041cb2

Please sign in to comment.