diff --git a/src/index.js b/src/index.js index d3928d6bb..4848968b0 100644 --- a/src/index.js +++ b/src/index.js @@ -28,7 +28,6 @@ module.exports = { // Structures Activity: require('./structures/Presence').Activity, - Attachment: require('./structures/Attachment'), Channel: require('./structures/Channel'), ClientUser: require('./structures/ClientUser'), ClientUserSettings: require('./structures/ClientUserSettings'), diff --git a/src/structures/Attachment.js b/src/structures/Attachment.js deleted file mode 100644 index e4ccbb197..000000000 --- a/src/structures/Attachment.js +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Represents an attachment in a message. - * @param {BufferResolvable|Stream} file The file - * @param {string} [name] The name of the file, if any - */ -class Attachment { - constructor(file, name) { - this.file = null; - if (name) this.setAttachment(file, name); - else this._attach(file); - } - - /** - * The name of the file - * @type {?string} - * @readonly - */ - get name() { - return this.file.name; - } - - /** - * The file - * @type {?BufferResolvable|Stream} - * @readonly - */ - get attachment() { - return this.file.attachment; - } - - /** - * Set the file of this attachment. - * @param {BufferResolvable|Stream} file The file - * @param {string} name The name of the file - * @returns {Attachment} This attachment - */ - setAttachment(file, name) { - this.file = { attachment: file, name }; - return this; - } - - /** - * Set the file of this attachment. - * @param {BufferResolvable|Stream} attachment The file - * @returns {Attachment} This attachment - */ - setFile(attachment) { - this.file = { attachment }; - return this; - } - - /** - * Set the name of this attachment. - * @param {string} name The name of the image - * @returns {Attachment} This attachment - */ - setName(name) { - this.file.name = name; - return this; - } - - /** - * Set the file of this attachment. - * @param {BufferResolvable|Stream} file The file - * @param {string} name The name of the file - * @private - */ - _attach(file, name) { - if (typeof file === 'string') this.file = file; - else this.setAttachment(file, name); - } -} - -module.exports = Attachment; diff --git a/src/structures/Message.js b/src/structures/Message.js index 6490388cc..96dab137b 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -1,5 +1,5 @@ const Mentions = require('./MessageMentions'); -const Attachment = require('./MessageAttachment'); +const MessageAttachment = require('./MessageAttachment'); const Embed = require('./MessageEmbed'); const ReactionCollector = require('./ReactionCollector'); const ClientApplication = require('./ClientApplication'); @@ -96,7 +96,11 @@ class Message extends Base { * @type {Collection} */ this.attachments = new Collection(); - for (const attachment of data.attachments) this.attachments.set(attachment.id, new Attachment(this, attachment)); + for (const attachment of data.attachments) { + this.attachments.set(attachment.id, new MessageAttachment( + attachment.url, attachment.filename, attachment + )); + } /** * The timestamp the message was sent at @@ -180,7 +184,11 @@ class Message extends Base { if ('attachments' in data) { this.attachments = new Collection(); - for (const attachment of data.attachments) this.attachments.set(attachment.id, new Attachment(this, attachment)); + for (const attachment of data.attachments) { + this.attachments.set(attachment.id, new MessageAttachment( + attachment.url, attachment.filename, attachment + )); + } } else { this.attachments = new Collection(this.attachments); } diff --git a/src/structures/MessageAttachment.js b/src/structures/MessageAttachment.js index dd8b8a380..0a4fb1b7e 100644 --- a/src/structures/MessageAttachment.js +++ b/src/structures/MessageAttachment.js @@ -1,43 +1,88 @@ /** * Represents an attachment in a message. + * @param {BufferResolvable|Stream} file The file + * @param {string} [name] The name of the file, if any */ class MessageAttachment { - constructor(message, data) { - /** - * The client that instantiated this MessageAttachment - * @name MessageAttachment#client - * @type {Client} - * @readonly - */ - Object.defineProperty(this, 'client', { value: message.client }); - - /** - * The message this attachment is part of - * @type {Message} - */ - this.message = message; - - this.setup(data); + constructor(file, name, data) { + this.file = null; + if (data) this._patch(data); + if (name) this.setAttachment(file, name); + else this._attach(file); } - setup(data) { + /** + * The name of the file + * @type {?string} + * @readonly + */ + get name() { + return this.file.name; + } + + /** + * The file + * @type {?BufferResolvable|Stream} + * @readonly + */ + get attachment() { + return this.file.attachment; + } + + /** + * Set the file of this attachment. + * @param {BufferResolvable|Stream} file The file + * @param {string} name The name of the file + * @returns {MessageAttachment} This attachment + */ + setAttachment(file, name) { + this.file = { attachment: file, name }; + return this; + } + + /** + * Set the file of this attachment. + * @param {BufferResolvable|Stream} attachment The file + * @returns {MessageAttachment} This attachment + */ + setFile(attachment) { + this.file = { attachment }; + return this; + } + + /** + * Set the name of this attachment. + * @param {string} name The name of the image + * @returns {MessageAttachment} This attachment + */ + setName(name) { + this.file.name = name; + return this; + } + + /** + * Set the file of this attachment. + * @param {BufferResolvable|Stream} file The file + * @param {string} name The name of the file + * @private + */ + _attach(file, name) { + if (typeof file === 'string') this.file = file; + else this.setAttachment(file, name); + } + + _patch(data) { /** * The ID of this attachment * @type {Snowflake} */ 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; + this.size = data.size; /** * The URL to this attachment diff --git a/src/structures/MessageEmbed.js b/src/structures/MessageEmbed.js index ce49ee74e..2156735c8 100644 --- a/src/structures/MessageEmbed.js +++ b/src/structures/MessageEmbed.js @@ -1,4 +1,4 @@ -const Attachment = require('./Attachment'); +const MessageAttachment = require('./MessageAttachment'); const Util = require('../util/Util'); const { RangeError } = require('../errors'); @@ -135,10 +135,10 @@ class MessageEmbed { /** * The files of this embed * @type {?Object} - * @property {Array} files Files to attach + * @property {Array} files Files to attach */ if (data.files) { - for (let file of data.files) if (file instanceof Attachment) file = file.file; + for (let file of data.files) if (file instanceof MessageAttachment) file = file.file; } else { data.files = null; } } @@ -189,14 +189,14 @@ class MessageEmbed { /** * Sets the file to upload alongside the embed. This file can be accessed via `attachment://fileName.extension` when * setting an embed image or author/footer icons. Only one file may be attached. - * @param {Array} files Files to attach + * @param {Array} files Files to attach * @returns {MessageEmbed} */ attachFiles(files) { if (this.files) this.files = this.files.concat(files); else this.files = files; for (let file of files) { - if (file instanceof Attachment) file = file.file; + if (file instanceof MessageAttachment) file = file.file; } return this; } diff --git a/src/structures/Webhook.js b/src/structures/Webhook.js index 51e0dea3f..fd2c633a4 100644 --- a/src/structures/Webhook.js +++ b/src/structures/Webhook.js @@ -1,7 +1,7 @@ const path = require('path'); const Util = require('../util/Util'); const Embed = require('./MessageEmbed'); -const Attachment = require('./Attachment'); +const MessageAttachment = require('./MessageAttachment'); const MessageEmbed = require('./MessageEmbed'); /** @@ -85,10 +85,11 @@ class Webhook { * it exceeds the character limit. If an object is provided, these are the options for splitting the message. */ + /* eslint-disable max-len */ /** * Send a message with this webhook. * @param {StringResolvable} [content] The content to send - * @param {WebhookMessageOptions|MessageEmbed|Attachment|Attachment[]} [options={}] The options to provide + * @param {WebhookMessageOptions|MessageEmbed|MessageAttachment|MessageAttachment[]} [options={}] The options to provide * @returns {Promise} * @example * // Send a message @@ -96,6 +97,7 @@ class Webhook { * .then(message => console.log(`Sent message: ${message.content}`)) * .catch(console.error); */ + /* eslint-enable max-len */ send(content, options) { // eslint-disable-line complexity if (!options && typeof content === 'object' && !(content instanceof Array)) { options = content; @@ -104,13 +106,13 @@ class Webhook { options = {}; } - if (options instanceof Attachment) options = { files: [options.file] }; + if (options instanceof MessageAttachment) options = { files: [options.file] }; if (options instanceof MessageEmbed) options = { embeds: [options] }; if (options.embed) options = { embeds: [options.embed] }; if (content instanceof Array || options instanceof Array) { const which = content instanceof Array ? content : options; - const attachments = which.filter(item => item instanceof Attachment); + const attachments = which.filter(item => item instanceof MessageAttachment); const embeds = which.filter(item => item instanceof MessageEmbed); if (attachments.length) options = { files: attachments }; if (embeds.length) options = { embeds }; @@ -154,12 +156,12 @@ class Webhook { file.name = path.basename(file.attachment); } else if (file.attachment && file.attachment.path) { file.name = path.basename(file.attachment.path); - } else if (file instanceof Attachment) { + } else if (file instanceof MessageAttachment) { file = { attachment: file.file, name: path.basename(file.file) || 'file.jpg' }; } else { file.name = 'file.jpg'; } - } else if (file instanceof Attachment) { + } else if (file instanceof MessageAttachment) { file = file.file; } options.files[i] = file; diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index ac41e847c..f444f3970 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -4,7 +4,7 @@ const Shared = require('../shared'); const MessageStore = require('../../stores/MessageStore'); const Snowflake = require('../../util/Snowflake'); const Collection = require('../../util/Collection'); -const Attachment = require('../../structures/Attachment'); +const MessageAttachment = require('../../structures/MessageAttachment'); const MessageEmbed = require('../../structures/MessageEmbed'); const { RangeError, TypeError } = require('../../errors'); @@ -68,7 +68,7 @@ class TextBasedChannel { /** * Send a message to this channel. * @param {StringResolvable} [content] Text for the message - * @param {MessageOptions|MessageEmbed|Attachment|Attachment[]} [options={}] Options for the message + * @param {MessageOptions|MessageEmbed|MessageAttachment|MessageAttachment[]} [options={}] Options for the message * @returns {Promise} * @example * // Send a message @@ -85,11 +85,11 @@ class TextBasedChannel { } if (options instanceof MessageEmbed) options = { embed: options }; - if (options instanceof Attachment) options = { files: [options.file] }; + if (options instanceof MessageAttachment) options = { files: [options.file] }; if (content instanceof Array || options instanceof Array) { const which = content instanceof Array ? content : options; - const attachments = which.filter(item => item instanceof Attachment); + const attachments = which.filter(item => item instanceof MessageAttachment); if (attachments.length) { options = { files: attachments }; if (content instanceof Array) content = ''; @@ -112,12 +112,12 @@ class TextBasedChannel { file.name = path.basename(file.attachment); } else if (file.attachment && file.attachment.path) { file.name = path.basename(file.attachment.path); - } else if (file instanceof Attachment) { + } else if (file instanceof MessageAttachment) { file = { attachment: file.file, name: path.basename(file.file) || 'file.jpg' }; } else { file.name = 'file.jpg'; } - } else if (file instanceof Attachment) { + } else if (file instanceof MessageAttachment) { file = file.file; } options.files[i] = file;