mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user