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

Fix(Structure): Message#delete() cause event messageDelete to fires twice #3252

Merged
merged 4 commits into from Aug 17, 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
65 changes: 35 additions & 30 deletions src/client/actions/Action.js
Expand Up @@ -23,47 +23,52 @@ class GenericAction {
return data;
}

getPayload(data, store, id, partialType, cache) {
const existing = store.get(id);
if (!existing && this.client.options.partials.includes(partialType)) {
return store.add(data, cache);
}
return existing;
}

getChannel(data) {
const id = data.channel_id || data.id;
return data.channel || (this.client.options.partials.includes(PartialTypes.CHANNEL) ?
this.client.channels.add({
id,
guild_id: data.guild_id,
}) :
this.client.channels.get(id));
return data.channel || this.getPayload({
id,
guild_id: data.guild_id,
}, this.client.channels, id, PartialTypes.CHANNEL);
}

getMessage(data, channel, cache = true) {
getMessage(data, channel, cache) {
const id = data.message_id || data.id;
return data.message || (this.client.options.partials.includes(PartialTypes.MESSAGE) ?
channel.messages.add({
id,
channel_id: channel.id,
guild_id: data.guild_id || (channel.guild ? channel.guild.id : null),
}, cache) :
channel.messages.get(id));
return data.message || this.getPayload({
id,
channel_id: channel.id,
guild_id: data.guild_id || (channel.guild ? channel.guild.id : null),
}, channel.messages, id, PartialTypes.MESSAGE, cache);
}

getReaction(data, message, user) {
const emojiID = data.emoji.id || decodeURIComponent(data.emoji.name);
const existing = message.reactions.get(emojiID);
if (!existing && this.client.options.partials.includes(PartialTypes.MESSAGE)) {
return message.reactions.add({
emoji: data.emoji,
count: 0,
me: user.id === this.client.user.id,
});
}
return existing;
const id = data.emoji.id || decodeURIComponent(data.emoji.name);
return this.getPayload({
emoji: data.emoji,
count: 0,
me: user.id === this.client.user.id,
}, message.reactions, id, PartialTypes.MESSAGE);
}

getMember(data, guild) {
const userID = data.user.id;
const existing = guild.members.get(userID);
if (!existing && this.client.options.partials.includes(PartialTypes.GUILD_MEMBER)) {
return guild.members.add({ user: { id: userID } });
}
return existing;
const id = data.user.id;
return this.getPayload({
user: {
id,
},
}, guild.members, id, PartialTypes.GUILD_MEMBER);
}

getUser(data) {
const id = data.user_id;
return data.user || this.getPayload({ id }, this.client.users, id, PartialTypes.USER);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/client/actions/MessageReactionAdd.js
Expand Up @@ -14,7 +14,7 @@ class MessageReactionAdd extends Action {
handle(data) {
if (!data.emoji) return false;

const user = data.user || this.client.users.get(data.user_id);
const user = this.getUser(data);
if (!user) return false;

// Verify channel
Expand Down
2 changes: 1 addition & 1 deletion src/client/actions/MessageReactionRemove.js
Expand Up @@ -14,7 +14,7 @@ class MessageReactionRemove extends Action {
handle(data) {
if (!data.emoji) return false;

const user = this.client.users.get(data.user_id);
const user = this.getUser(data);
if (!user) return false;

// Verify channel
Expand Down
6 changes: 1 addition & 5 deletions src/structures/Message.js
Expand Up @@ -454,11 +454,7 @@ class Message extends Base {
*/
delete({ timeout = 0, reason } = {}) {
if (timeout <= 0) {
return this.channel.messages.remove(this.id, reason).then(() =>
this.client.actions.MessageDelete.handle({
id: this.id,
channel_id: this.channel.id,
}).message);
return this.channel.messages.remove(this.id, reason).then(() => this);
} else {
return new Promise(resolve => {
this.client.setTimeout(() => {
Expand Down