mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-16 03:23:29 +01:00
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:
@@ -28,7 +28,6 @@ module.exports = {
|
|||||||
|
|
||||||
// Structures
|
// Structures
|
||||||
Activity: require('./structures/Presence').Activity,
|
Activity: require('./structures/Presence').Activity,
|
||||||
Attachment: require('./structures/Attachment'),
|
|
||||||
Channel: require('./structures/Channel'),
|
Channel: require('./structures/Channel'),
|
||||||
ClientUser: require('./structures/ClientUser'),
|
ClientUser: require('./structures/ClientUser'),
|
||||||
ClientUserSettings: require('./structures/ClientUserSettings'),
|
ClientUserSettings: require('./structures/ClientUserSettings'),
|
||||||
|
|||||||
@@ -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;
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
const Mentions = require('./MessageMentions');
|
const Mentions = require('./MessageMentions');
|
||||||
const Attachment = require('./MessageAttachment');
|
const MessageAttachment = require('./MessageAttachment');
|
||||||
const Embed = require('./MessageEmbed');
|
const Embed = require('./MessageEmbed');
|
||||||
const ReactionCollector = require('./ReactionCollector');
|
const ReactionCollector = require('./ReactionCollector');
|
||||||
const ClientApplication = require('./ClientApplication');
|
const ClientApplication = require('./ClientApplication');
|
||||||
@@ -96,7 +96,11 @@ class Message extends Base {
|
|||||||
* @type {Collection<Snowflake, MessageAttachment>}
|
* @type {Collection<Snowflake, MessageAttachment>}
|
||||||
*/
|
*/
|
||||||
this.attachments = new 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
|
* The timestamp the message was sent at
|
||||||
@@ -180,7 +184,11 @@ class Message extends Base {
|
|||||||
|
|
||||||
if ('attachments' in data) {
|
if ('attachments' in data) {
|
||||||
this.attachments = new 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
|
||||||
|
));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.attachments = new Collection(this.attachments);
|
this.attachments = new Collection(this.attachments);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,43 +1,88 @@
|
|||||||
/**
|
/**
|
||||||
* Represents an attachment in a message.
|
* Represents an attachment in a message.
|
||||||
|
* @param {BufferResolvable|Stream} file The file
|
||||||
|
* @param {string} [name] The name of the file, if any
|
||||||
*/
|
*/
|
||||||
class MessageAttachment {
|
class MessageAttachment {
|
||||||
constructor(message, data) {
|
constructor(file, name, data) {
|
||||||
/**
|
this.file = null;
|
||||||
* The client that instantiated this MessageAttachment
|
if (data) this._patch(data);
|
||||||
* @name MessageAttachment#client
|
if (name) this.setAttachment(file, name);
|
||||||
* @type {Client}
|
else this._attach(file);
|
||||||
* @readonly
|
|
||||||
*/
|
|
||||||
Object.defineProperty(this, 'client', { value: message.client });
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The message this attachment is part of
|
|
||||||
* @type {Message}
|
|
||||||
*/
|
|
||||||
this.message = message;
|
|
||||||
|
|
||||||
this.setup(data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
* The ID of this attachment
|
||||||
* @type {Snowflake}
|
* @type {Snowflake}
|
||||||
*/
|
*/
|
||||||
this.id = data.id;
|
this.id = data.id;
|
||||||
|
|
||||||
/**
|
|
||||||
* The file name of this attachment
|
|
||||||
* @type {string}
|
|
||||||
*/
|
|
||||||
this.filename = data.filename;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The size of this attachment in bytes
|
* The size of this attachment in bytes
|
||||||
* @type {number}
|
* @type {number}
|
||||||
*/
|
*/
|
||||||
this.filesize = data.size;
|
this.size = data.size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The URL to this attachment
|
* The URL to this attachment
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const Attachment = require('./Attachment');
|
const MessageAttachment = require('./MessageAttachment');
|
||||||
const Util = require('../util/Util');
|
const Util = require('../util/Util');
|
||||||
const { RangeError } = require('../errors');
|
const { RangeError } = require('../errors');
|
||||||
|
|
||||||
@@ -135,10 +135,10 @@ class MessageEmbed {
|
|||||||
/**
|
/**
|
||||||
* The files of this embed
|
* The files of this embed
|
||||||
* @type {?Object}
|
* @type {?Object}
|
||||||
* @property {Array<FileOptions|string|Attachment>} files Files to attach
|
* @property {Array<FileOptions|string|MessageAttachment>} files Files to attach
|
||||||
*/
|
*/
|
||||||
if (data.files) {
|
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; }
|
} 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
|
* 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.
|
* 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}
|
* @returns {MessageEmbed}
|
||||||
*/
|
*/
|
||||||
attachFiles(files) {
|
attachFiles(files) {
|
||||||
if (this.files) this.files = this.files.concat(files);
|
if (this.files) this.files = this.files.concat(files);
|
||||||
else this.files = files;
|
else this.files = files;
|
||||||
for (let file of files) {
|
for (let file of files) {
|
||||||
if (file instanceof Attachment) file = file.file;
|
if (file instanceof MessageAttachment) file = file.file;
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const Util = require('../util/Util');
|
const Util = require('../util/Util');
|
||||||
const Embed = require('./MessageEmbed');
|
const Embed = require('./MessageEmbed');
|
||||||
const Attachment = require('./Attachment');
|
const MessageAttachment = require('./MessageAttachment');
|
||||||
const MessageEmbed = require('./MessageEmbed');
|
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.
|
* 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.
|
* Send a message with this webhook.
|
||||||
* @param {StringResolvable} [content] The content to send
|
* @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>}
|
* @returns {Promise<Message|Object>}
|
||||||
* @example
|
* @example
|
||||||
* // Send a message
|
* // Send a message
|
||||||
@@ -96,6 +97,7 @@ class Webhook {
|
|||||||
* .then(message => console.log(`Sent message: ${message.content}`))
|
* .then(message => console.log(`Sent message: ${message.content}`))
|
||||||
* .catch(console.error);
|
* .catch(console.error);
|
||||||
*/
|
*/
|
||||||
|
/* eslint-enable max-len */
|
||||||
send(content, options) { // eslint-disable-line complexity
|
send(content, options) { // eslint-disable-line complexity
|
||||||
if (!options && typeof content === 'object' && !(content instanceof Array)) {
|
if (!options && typeof content === 'object' && !(content instanceof Array)) {
|
||||||
options = content;
|
options = content;
|
||||||
@@ -104,13 +106,13 @@ class Webhook {
|
|||||||
options = {};
|
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 instanceof MessageEmbed) options = { embeds: [options] };
|
||||||
if (options.embed) options = { embeds: [options.embed] };
|
if (options.embed) options = { embeds: [options.embed] };
|
||||||
|
|
||||||
if (content instanceof Array || options instanceof Array) {
|
if (content instanceof Array || options instanceof Array) {
|
||||||
const which = content instanceof Array ? content : options;
|
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);
|
const embeds = which.filter(item => item instanceof MessageEmbed);
|
||||||
if (attachments.length) options = { files: attachments };
|
if (attachments.length) options = { files: attachments };
|
||||||
if (embeds.length) options = { embeds };
|
if (embeds.length) options = { embeds };
|
||||||
@@ -154,12 +156,12 @@ class Webhook {
|
|||||||
file.name = path.basename(file.attachment);
|
file.name = path.basename(file.attachment);
|
||||||
} else if (file.attachment && file.attachment.path) {
|
} else if (file.attachment && file.attachment.path) {
|
||||||
file.name = path.basename(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' };
|
file = { attachment: file.file, name: path.basename(file.file) || 'file.jpg' };
|
||||||
} else {
|
} else {
|
||||||
file.name = 'file.jpg';
|
file.name = 'file.jpg';
|
||||||
}
|
}
|
||||||
} else if (file instanceof Attachment) {
|
} else if (file instanceof MessageAttachment) {
|
||||||
file = file.file;
|
file = file.file;
|
||||||
}
|
}
|
||||||
options.files[i] = file;
|
options.files[i] = file;
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ const Shared = require('../shared');
|
|||||||
const MessageStore = require('../../stores/MessageStore');
|
const MessageStore = require('../../stores/MessageStore');
|
||||||
const Snowflake = require('../../util/Snowflake');
|
const Snowflake = require('../../util/Snowflake');
|
||||||
const Collection = require('../../util/Collection');
|
const Collection = require('../../util/Collection');
|
||||||
const Attachment = require('../../structures/Attachment');
|
const MessageAttachment = require('../../structures/MessageAttachment');
|
||||||
const MessageEmbed = require('../../structures/MessageEmbed');
|
const MessageEmbed = require('../../structures/MessageEmbed');
|
||||||
const { RangeError, TypeError } = require('../../errors');
|
const { RangeError, TypeError } = require('../../errors');
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ class TextBasedChannel {
|
|||||||
/**
|
/**
|
||||||
* Send a message to this channel.
|
* Send a message to this channel.
|
||||||
* @param {StringResolvable} [content] Text for the message
|
* @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[]>}
|
* @returns {Promise<Message|Message[]>}
|
||||||
* @example
|
* @example
|
||||||
* // Send a message
|
* // Send a message
|
||||||
@@ -85,11 +85,11 @@ class TextBasedChannel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (options instanceof MessageEmbed) options = { embed: options };
|
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) {
|
if (content instanceof Array || options instanceof Array) {
|
||||||
const which = content instanceof Array ? content : options;
|
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) {
|
if (attachments.length) {
|
||||||
options = { files: attachments };
|
options = { files: attachments };
|
||||||
if (content instanceof Array) content = '';
|
if (content instanceof Array) content = '';
|
||||||
@@ -112,12 +112,12 @@ class TextBasedChannel {
|
|||||||
file.name = path.basename(file.attachment);
|
file.name = path.basename(file.attachment);
|
||||||
} else if (file.attachment && file.attachment.path) {
|
} else if (file.attachment && file.attachment.path) {
|
||||||
file.name = path.basename(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' };
|
file = { attachment: file.file, name: path.basename(file.file) || 'file.jpg' };
|
||||||
} else {
|
} else {
|
||||||
file.name = 'file.jpg';
|
file.name = 'file.jpg';
|
||||||
}
|
}
|
||||||
} else if (file instanceof Attachment) {
|
} else if (file instanceof MessageAttachment) {
|
||||||
file = file.file;
|
file = file.file;
|
||||||
}
|
}
|
||||||
options.files[i] = file;
|
options.files[i] = file;
|
||||||
|
|||||||
Reference in New Issue
Block a user