Fix for #1089 & Message Reactions (#1236)

* [requires more testing] Fix #1089

* Clean up unshift

* Remove <Message>.patch(data)

Nothing calls this method any longer. It is also a private method, so this shouldn't be a breaking change.

* Fix Message Reactions

Purposely reference previous reaction collections, so collection is consistant accross all message edits (no unnecisary data duplication). Makes #1221 extranious.

* Some Data Packets come Incomplete

And several properties can be falsy, so instead of || opted for hasOwnProperty().

* No reason MessageTypes should be an object...

* Use `prop in obj` isntead of hasOwnProp

per @Gawdl3y
This commit is contained in:
bdistin
2017-04-01 00:51:12 -05:00
committed by Schuyler Cebulskie
parent 07740955cf
commit bca101aac8
2 changed files with 31 additions and 70 deletions

View File

@@ -1,6 +1,6 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
const Util = require('../../util/Util');
const Message = require('../../structures/Message');
class MessageUpdateAction extends Action {
handle(data) {
@@ -10,13 +10,14 @@ class MessageUpdateAction extends Action {
if (channel) {
const message = channel.messages.get(data.id);
if (message) {
const oldMessage = Util.cloneObject(message);
message.patch(data);
message._edits.unshift(oldMessage);
client.emit(Constants.Events.MESSAGE_UPDATE, oldMessage, message);
const newMessage = new Message(message.channel, this.patchDataPacket(data, message), client);
newMessage._edits.push(message, ...message._edits);
newMessage.reactions = message.reactions;
channel.messages.set(data.id, newMessage);
client.emit(Constants.Events.MESSAGE_UPDATE, message, newMessage);
return {
old: oldMessage,
updated: message,
old: message,
updated: newMessage,
};
}
@@ -31,6 +32,29 @@ class MessageUpdateAction extends Action {
updated: null,
};
}
patchDataPacket(data, message) {
data.type = 'type' in data ? data.type : Constants.MessageTypes.indexOf(message.type);
data.tts = 'tts' in data ? data.tts : message.tts;
data.timestamp = 'timestamp' in data ? data.timestamp : message.createdAt.toString();
data.pinned = 'pinned' in data ? data.pinned : message.pinned;
data.nonce = 'nonce' in data ? data.nonce : message.nonce;
data.mentions = 'mentions' in data ? data.mentions : message.mentions.users.keyArray();
data.mentions_roles = 'mentions_roles' in data ?
data.mentions_roles : message.mentions.roles.keyArray();
data.mention_everyone = 'mention_everyone' in data ? data.mention_everyone : message.mentions.everyone;
data.embeds = 'embeds' in data ? data.embeds : message.embeds;
data.content = 'content' in data ? data.content : message.content;
data.author = 'author' in data ? data.author : {
username: message.author.username,
id: message.author.id,
discriminator: message.author.discriminator,
avatar: message.author.avatar,
};
data.attachments = 'attachments' in data ? data.attachments : message.attachments.array();
return data;
}
}
/**

View File

@@ -193,69 +193,6 @@ class Message {
this.hit = typeof data.hit === 'boolean' ? data.hit : null;
}
patch(data) { // eslint-disable-line complexity
if (data.author) {
this.author = this.client.users.get(data.author.id);
if (this.guild) this.member = this.guild.member(this.author);
}
if (data.content) this.content = data.content;
if (data.timestamp) this.createdTimestamp = new Date(data.timestamp).getTime();
if (data.edited_timestamp) {
this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp).getTime() : null;
}
if ('tts' in data) this.tts = data.tts;
if ('mention_everyone' in data) this.mentions.everyone = data.mention_everyone;
if (data.nonce) this.nonce = data.nonce;
if (data.embeds) this.embeds = data.embeds.map(e => new Embed(this, e));
if (data.type > -1) {
this.system = false;
if (data.type === 6) this.system = true;
}
if (data.attachments) {
this.attachments.clear();
for (const attachment of data.attachments) {
this.attachments.set(attachment.id, new Attachment(this, attachment));
}
}
if (data.mentions) {
this.mentions.users.clear();
for (const mention of data.mentions) {
let user = this.client.users.get(mention.id);
if (user) {
this.mentions.users.set(user.id, user);
} else {
user = this.client.dataManager.newUser(mention);
this.mentions.users.set(user.id, user);
}
}
}
if (data.mention_roles) {
this.mentions.roles.clear();
for (const mention of data.mention_roles) {
const role = this.channel.guild.roles.get(mention);
if (role) this.mentions.roles.set(role.id, role);
}
}
if (data.id) this.id = data.id;
if (this.channel.guild && data.content) {
this.mentions.channels.clear();
const channMentionsRaw = data.content.match(/<#([0-9]{14,20})>/g) || [];
for (const raw of channMentionsRaw) {
const chan = this.channel.guild.channels.get(raw.match(/([0-9]{14,20})/g)[0]);
if (chan) this.mentions.channels.set(chan.id, chan);
}
}
if (data.reactions) {
this.reactions.clear();
if (data.reactions.length > 0) {
for (const reaction of data.reactions) {
const id = reaction.emoji.id ? `${reaction.emoji.name}:${reaction.emoji.id}` : reaction.emoji.name;
this.reactions.set(id, new MessageReaction(this, reaction.emoji, reaction.count, reaction.me));
}
}
}
}
/**
* The time the message was sent
* @type {Date}