Clean up Message#mentions and message updates

This commit is contained in:
Schuyler Cebulskie
2017-04-10 03:01:50 -04:00
parent 878e5d7c76
commit fa016b6b41
3 changed files with 194 additions and 89 deletions

View File

@@ -1,3 +1,4 @@
const Mentions = require('./MessageMentions');
const Attachment = require('./MessageAttachment');
const Embed = require('./MessageEmbed');
const MessageReaction = require('./MessageReaction');
@@ -110,69 +111,11 @@ class Message {
*/
this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp).getTime() : null;
/**
* An object containing a further users, roles or channels collections
* @type {Object}
* @property {Collection<Snowflake, User>} mentions.users Mentioned users, maps their ID to the user object.
* @property {Collection<Snowflake, GuildMember>} mentions.members Mentioned members, maps their ID
* to the member object.
* @property {Collection<Snowflake, Role>} mentions.roles Mentioned roles, maps their ID to the role object.
* @property {Collection<Snowflake, GuildChannel>} mentions.channels Mentioned channels,
* maps their ID to the channel object.
* @property {boolean} mentions.everyone Whether or not @everyone was mentioned.
*/
this.mentions = {
users: new Collection(),
roles: new Collection(),
channels: new Collection(),
everyone: data.mention_everyone,
};
// Add user mentions
for (const mention of data.mentions) {
let user = this.client.users.get(mention.id);
if (!user) user = this.client.dataManager.newUser(mention);
this.mentions.users.set(user.id, user);
}
// Add getter for member mentions
Object.defineProperty(this.mentions, 'members', {
get: () => {
if (this.channel.type !== 'text') return null;
const members = new Collection();
this.mentions.users.forEach(user => {
const member = this.client.resolver.resolveGuildMember(this.channel.guild, user);
if (member) members.set(member.id, member);
});
return members;
},
});
// Add role mentions
if (data.mention_roles) {
for (const mention of data.mention_roles) {
const role = this.channel.guild.roles.get(mention);
if (role) this.mentions.roles.set(role.id, role);
}
}
// Add channel mentions
if (this.channel.type === 'text') {
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);
}
}
this._edits = [];
/**
* A collection of reactions to this message, mapped by the reaction "id".
* @type {Collection<Snowflake, MessageReaction>}
*/
this.reactions = new Collection();
if (data.reactions && data.reactions.length > 0) {
for (const reaction of data.reactions) {
const id = reaction.emoji.id ? `${reaction.emoji.name}:${reaction.emoji.id}` : reaction.emoji.name;
@@ -180,6 +123,12 @@ class Message {
}
}
/**
* All valid mentions that the message contains
* @type {MessageMentions}
*/
this.mentions = new Mentions(this, data.mentions, data.mentions_roles, data.mention_everyone);
/**
* ID of the webhook that sent the message, if applicable
* @type {?Snowflake}
@@ -191,6 +140,44 @@ class Message {
* @type {?boolean}
*/
this.hit = typeof data.hit === 'boolean' ? data.hit : null;
/**
* The previous versions of the message, sorted with the most recent first
* @type {Message[]}
* @private
*/
this._edits = [];
}
/**
* Updates the message
* @param {Object} data Raw Discord message update data
* @private
*/
patch(data) {
const clone = Util.cloneObject(this);
this._edits.unshift(clone);
this.editedTimestamp = data.edited_timestamp;
if ('content' in data) this.content = data.content;
if ('pinned' in data) this.pinned = data.pinned;
if ('tts' in data) this.tts = data.tts;
if ('embeds' in data) this.embeds = data.embeds.map(e => new Embed(this, e));
else this.embeds = new Collection(this.embeds);
if ('attachments' in data) {
this.attachments = new Collection();
for (const attachment of data.attachments) this.attachments.set(attachment.id, new Attachment(this, attachment));
} else {
this.attachments = new Collection(this.attachments);
}
this.mentions = new Mentions(
this,
'mentions' in data ? data.mentions : this.mentions.users,
'mentions_roles' in data ? data.mentions_roles : this.mentions.roles,
'mention_everyone' in data ? data.mention_everyone : this.mentions.everyone
);
}
/**