refactor(Attachment): Merge MessageAttachment with Attachment (#1894)

* refactor(Attachment): Merge MessageAttachment with Attachment

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

* refactor(MessageAttachment): Global rename of Attachment class
This commit is contained in:
Robin B
2017-09-06 23:12:20 +02:00
committed by Crawl
parent 694f78cd43
commit d6b276bc29
7 changed files with 99 additions and 119 deletions

View File

@@ -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'),

View File

@@ -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;

View File

@@ -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<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
@@ -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);
}

View File

@@ -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

View File

@@ -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<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; }
}
@@ -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;
}

View File

@@ -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<Message|Object>}
* @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;

View File

@@ -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<Message|Message[]>}
* @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;