From f6c95de85db09b4abf1f1de747019bf68ad8a366 Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Mon, 29 Aug 2016 15:35:39 +0100 Subject: [PATCH] Create MessageAttachment class --- src/structures/Message.js | 15 +++++--- src/structures/MessageAttachment.js | 56 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/structures/MessageAttachment.js diff --git a/src/structures/Message.js b/src/structures/Message.js index 918475f95..088566fd7 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -1,4 +1,5 @@ const Collection = require('../util/Collection'); +const Attachment = require('./MessageAttachment'); /** * Represents a Message on Discord */ @@ -76,10 +77,13 @@ class Message { */ this.embeds = data.embeds; /** - * A list of attachments in the message - e.g. Pictures - * @type {Array} + * A collection of attachments in the message - e.g. Pictures - mapped by their ID. + * @type {Collection} */ - this.attachments = data.attachments; + this.attachments = new Collection(); + for (const attachment of data.attachments) { + this.attachments.set(attachment.id, new Attachment(this, attachment)); + } /** * An object containing a further users, roles or channels collections * @type {Object} @@ -155,7 +159,10 @@ class Message { this.embeds = data.embeds; } if (data.attachments) { - this.attachments = data.attachments; + this.attachments = new Collection(); + for (const attachment of data.attachments) { + this.attachments.set(attachment.id, new Attachment(this, attachment)); + } } if (data.mentions) { for (const mention of data.mentions) { diff --git a/src/structures/MessageAttachment.js b/src/structures/MessageAttachment.js new file mode 100644 index 000000000..29add4abc --- /dev/null +++ b/src/structures/MessageAttachment.js @@ -0,0 +1,56 @@ +/** + * Represents an Attachment in a Message + */ +class MessageAttachment { + constructor(message, data) { + /** + * The Client that instantiated this Message. + */ + this.client = message.client; + /** + * The message this attachment is part of. + */ + this.message = message; + this.setup(data); + } + + setup(data) { + /** + * The ID of this attachment + * @type {String} + */ + this.id = data.id; + /** + * The file name of this attachment + * @type {String} + */ + this.filename = data.filename; + /** + * The size of this attachment in bytes + * @type {Number} + */ + this.filesize = data.size; + /** + * The URL to this attachment + * @type {String} + */ + this.url = data.url; + /** + * The Proxy URL to this attachment + * @type {String} + */ + this.proxyURL = data.url; + /** + * The height of this attachment (if an image) + * @type {?Number} + */ + this.height = data.height; + /** + * The width of this attachment (if an image) + * @type {?Number} + */ + this.width = data.width; + } +} + +module.exports = MessageAttachment;