Skip to content

Commit

Permalink
refactor(Attachment): Merge MessageAttachment with Attachment (#1894)
Browse files Browse the repository at this point in the history
* refactor(Attachment): Merge MessageAttachment with Attachment

* refactor(Attachment): Rename setup to _patch for consistency

* refactor(MessageAttachment): Global rename of Attachment class
  • Loading branch information
Robin B authored and iCrawl committed Sep 6, 2017
1 parent 694f78c commit d6b276b
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 117 deletions.
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
74 changes: 0 additions & 74 deletions src/structures/Attachment.js

This file was deleted.

14 changes: 11 additions & 3 deletions src/structures/Message.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -96,7 +96,11 @@ class Message extends Base {
* @type {Collection<Snowflake, MessageAttachment>}
*/
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
Expand Down Expand Up @@ -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);
}
Expand Down
89 changes: 67 additions & 22 deletions src/structures/MessageAttachment.js
Original file line number Diff line number Diff line change
@@ -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 });
constructor(file, name, data) {
this.file = null;
if (data) this._patch(data);
if (name) this.setAttachment(file, name);
else this._attach(file);
}

/**
* The message this attachment is part of
* @type {Message}
*/
this.message = message;
/**
* 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;
}

this.setup(data);
/**
* 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);
}

setup(data) {
_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
Expand Down
10 changes: 5 additions & 5 deletions src/structures/MessageEmbed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Attachment = require('./Attachment');
const MessageAttachment = require('./MessageAttachment');
const Util = require('../util/Util');
const { RangeError } = require('../errors');

Expand Down Expand Up @@ -135,10 +135,10 @@ class MessageEmbed {
/**
* The files of this embed
* @type {?Object}
* @property {Array<FileOptions|string|Attachment>} files Files to attach
* @property {Array<FileOptions|string|MessageAttachment>} 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; }
}

Expand Down Expand Up @@ -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<FileOptions|string|Attachment>} files Files to attach
* @param {Array<FileOptions|string|MessageAttachment>} 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;
}
Expand Down
14 changes: 8 additions & 6 deletions src/structures/Webhook.js
Original file line number Diff line number Diff line change
@@ -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');

/**
Expand Down Expand Up @@ -85,17 +85,19 @@ 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<Message|Object>}
* @example
* // Send a message
* webhook.send('hello!')
* .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;
Expand All @@ -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 };
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/structures/interfaces/TextBasedChannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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<Message|Message[]>}
* @example
* // Send a message
Expand All @@ -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 = '';
Expand All @@ -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;
Expand Down

0 comments on commit d6b276b

Please sign in to comment.