Skip to content

Commit

Permalink
refactor(MessageAttachment): use Attachment instead (#7691)
Browse files Browse the repository at this point in the history
* fix: TOKEN_INVALID error not thrown at login with invalid token

* refactor(MessageAttachment): Use Attachment instead

* Delete a mistake

* Add WebSocketManager file, deleted by error

* add a new line on WebSocketManager file

* fix: imports

* fix: conflict with typings

* chore: update reference on GuildStickerManager
  • Loading branch information
Mateo-tem committed Apr 17, 2022
1 parent 1b2d8de commit ab4c608
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/discord.js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ exports.Invite = require('./structures/Invite');
exports.InviteStageInstance = require('./structures/InviteStageInstance');
exports.InviteGuild = require('./structures/InviteGuild');
exports.Message = require('./structures/Message').Message;
exports.MessageAttachment = require('./structures/MessageAttachment');
exports.Attachment = require('./structures/Attachment');
exports.ModalBuilder = require('./structures/ModalBuilder');
exports.MessageCollector = require('./structures/MessageCollector');
exports.MessageComponentInteraction = require('./structures/MessageComponentInteraction');
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/managers/GuildStickerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class GuildStickerManager extends CachedManager {

/**
* Creates a new custom sticker in the guild.
* @param {BufferResolvable|Stream|FileOptions|MessageAttachment} file The file for the sticker
* @param {BufferResolvable|Stream|FileOptions|Attachment} file The file for the sticker
* @param {string} name The name for the sticker
* @param {string} tags The Discord name of a unicode emoji representing the sticker's expression
* @param {GuildStickerCreateOptions} [options] Options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
const Util = require('../util/Util');

/**
* Represents an attachment in a message.
* Represents an attachment.
*/
class MessageAttachment {
class Attachment {
/**
* @param {BufferResolvable|Stream} attachment The file
* @param {string} [name=null] The name of the file, if any
Expand All @@ -24,7 +24,7 @@ class MessageAttachment {
/**
* Sets the description of this attachment.
* @param {string} description The description of the file
* @returns {MessageAttachment} This attachment
* @returns {Attachment} This attachment
*/
setDescription(description) {
this.description = description;
Expand All @@ -35,7 +35,7 @@ class MessageAttachment {
* Sets the file of this attachment.
* @param {BufferResolvable|Stream} attachment The file
* @param {string} [name=null] The name of the file, if any
* @returns {MessageAttachment} This attachment
* @returns {Attachment} This attachment
*/
setFile(attachment, name = null) {
this.attachment = attachment;
Expand All @@ -46,7 +46,7 @@ class MessageAttachment {
/**
* Sets the name of this attachment.
* @param {string} name The name of the file
* @returns {MessageAttachment} This attachment
* @returns {Attachment} This attachment
*/
setName(name) {
this.name = name;
Expand All @@ -56,7 +56,7 @@ class MessageAttachment {
/**
* Sets whether this attachment is a spoiler
* @param {boolean} [spoiler=true] Whether the attachment should be marked as a spoiler
* @returns {MessageAttachment} This attachment
* @returns {Attachment} This attachment
*/
setSpoiler(spoiler = true) {
if (spoiler === this.spoiler) return this;
Expand Down Expand Up @@ -163,7 +163,7 @@ class MessageAttachment {
}
}

module.exports = MessageAttachment;
module.exports = Attachment;

/**
* @external APIAttachment
Expand Down
10 changes: 5 additions & 5 deletions packages/discord.js/src/structures/CommandInteraction.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const { Collection } = require('@discordjs/collection');
const Attachment = require('./Attachment');
const Interaction = require('./Interaction');
const InteractionWebhook = require('./InteractionWebhook');
const MessageAttachment = require('./MessageAttachment');
const InteractionResponses = require('./interfaces/InteractionResponses');

/**
Expand Down Expand Up @@ -82,7 +82,7 @@ class CommandInteraction extends Interaction {
* @property {Collection<Snowflake, Role|APIRole>} [roles] The resolved roles
* @property {Collection<Snowflake, Channel|APIChannel>} [channels] The resolved channels
* @property {Collection<Snowflake, Message|APIMessage>} [messages] The resolved messages
* @property {Collection<Snowflake, MessageAttachment>} [attachments] The resolved attachments
* @property {Collection<Snowflake, Attachment>} [attachments] The resolved attachments
*/

/**
Expand Down Expand Up @@ -133,7 +133,7 @@ class CommandInteraction extends Interaction {
if (attachments) {
result.attachments = new Collection();
for (const attachment of Object.values(attachments)) {
const patched = new MessageAttachment(attachment.url, attachment.filename, attachment);
const patched = new Attachment(attachment.url, attachment.filename, attachment);
result.attachments.set(attachment.id, patched);
}
}
Expand All @@ -156,7 +156,7 @@ class CommandInteraction extends Interaction {
* @property {GuildMember|APIGuildMember} [member] The resolved member
* @property {GuildChannel|ThreadChannel|APIChannel} [channel] The resolved channel
* @property {Role|APIRole} [role] The resolved role
* @property {MessageAttachment} [attachment] The resolved attachment
* @property {Attachment} [attachment] The resolved attachment
*/

/**
Expand Down Expand Up @@ -189,7 +189,7 @@ class CommandInteraction extends Interaction {
if (role) result.role = this.guild?.roles._add(role) ?? role;

const attachment = resolved.attachments?.[option.value];
if (attachment) result.attachment = new MessageAttachment(attachment.url, attachment.filename, attachment);
if (attachment) result.attachment = new Attachment(attachment.url, attachment.filename, attachment);
}

return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class CommandInteractionOptionResolver {
* Gets an attachment option.
* @param {string} name The name of the option.
* @param {boolean} [required=false] Whether to throw an error if the option is not found.
* @returns {?MessageAttachment} The value of the option, or null if not set and not required.
* @returns {?Attachment} The value of the option, or null if not set and not required.
*/
getAttachment(name, required = false) {
const option = this._getTypedOption(name, ApplicationCommandOptionType.Attachment, ['attachment'], required);
Expand Down
10 changes: 5 additions & 5 deletions packages/discord.js/src/structures/Message.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const {
MessageFlags,
PermissionFlagsBits,
} = require('discord-api-types/v10');
const Attachment = require('./Attachment');
const Base = require('./Base');
const ClientApplication = require('./ClientApplication');
const Embed = require('./Embed');
const InteractionCollector = require('./InteractionCollector');
const MessageAttachment = require('./MessageAttachment');
const Mentions = require('./MessageMentions');
const MessagePayload = require('./MessagePayload');
const ReactionCollector = require('./ReactionCollector');
Expand Down Expand Up @@ -154,12 +154,12 @@ class Message extends Base {
if ('attachments' in data) {
/**
* A collection of attachments in the message - e.g. Pictures - mapped by their ids
* @type {Collection<Snowflake, MessageAttachment>}
* @type {Collection<Snowflake, Attachment>}
*/
this.attachments = new Collection();
if (data.attachments) {
for (const attachment of data.attachments) {
this.attachments.set(attachment.id, new MessageAttachment(attachment.url, attachment.filename, attachment));
this.attachments.set(attachment.id, new Attachment(attachment.url, attachment.filename, attachment));
}
}
} else {
Expand Down Expand Up @@ -642,9 +642,9 @@ class Message extends Base {
* @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content
* @property {MessageFlags} [flags] Which flags to set for the message.
* Only `MessageFlags.SuppressEmbeds` can be edited.
* @property {MessageAttachment[]} [attachments] An array of attachments to keep,
* @property {Attachment[]} [attachments] An array of attachments to keep,
* all attachments will be kept if omitted
* @property {FileOptions[]|BufferResolvable[]|MessageAttachment[]} [files] Files to add to the message
* @property {FileOptions[]|BufferResolvable[]|Attachment[]} [files] Files to add to the message
* @property {ActionRow[]|ActionRowOptions[]} [components]
* Action rows containing interactive components for the message (buttons, select menus)
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/MessagePayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class MessagePayload {

/**
* Resolves a single file into an object sendable to the API.
* @param {BufferResolvable|Stream|FileOptions|MessageAttachment} fileLike Something that could be resolved to a file
* @param {BufferResolvable|Stream|FileOptions|Attachment} fileLike Something that could be resolved to a file
* @returns {Promise<RawFile>}
*/
static async resolveFile(fileLike) {
Expand Down
4 changes: 2 additions & 2 deletions packages/discord.js/src/structures/Webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ class Webhook {
* @typedef {Object} WebhookEditMessageOptions
* @property {Embed[]|APIEmbed[]} [embeds] See {@link WebhookMessageOptions#embeds}
* @property {string} [content] See {@link BaseMessageOptions#content}
* @property {FileOptions[]|BufferResolvable[]|MessageAttachment[]} [files] See {@link BaseMessageOptions#files}
* @property {FileOptions[]|BufferResolvable[]|Attachment[]} [files] See {@link BaseMessageOptions#files}
* @property {MessageMentionOptions} [allowedMentions] See {@link BaseMessageOptions#allowedMentions}
* @property {MessageAttachment[]} [attachments] Attachments to send with the message
* @property {Attachment[]} [attachments] Attachments to send with the message
* @property {ActionRow[]|ActionRowOptions[]} [components]
* Action rows containing interactive components for the message (buttons, select menus)
* @property {Snowflake} [threadId] The id of the thread this message belongs to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ class TextBasedChannel {
* (see [here](https://discord.com/developers/docs/resources/channel#embed-object) for more details)
* @property {MessageMentionOptions} [allowedMentions] Which mentions should be parsed from the message content
* (see [here](https://discord.com/developers/docs/resources/channel#allowed-mentions-object) for more details)
* @property {FileOptions[]|BufferResolvable[]|MessageAttachment[]} [files] Files to send with the message
* @property {FileOptions[]|BufferResolvable[]|Attachment[]} [files] Files to send with the message
* @property {ActionRow[]|ActionRowOptions[]} [components]
* Action rows containing interactive components for the message (buttons, select menus)
* @property {MessageAttachment[]} [attachments] Attachments to send in the message
* @property {Attachment[]} [attachments] Attachments to send in the message
*/

/**
Expand Down
24 changes: 12 additions & 12 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ import {
RawInviteData,
RawInviteGuildData,
RawInviteStageInstance,
RawMessageAttachmentData,
RawAttachmentData,
RawMessageButtonInteractionData,
RawMessageComponentInteractionData,
RawMessageData,
Expand Down Expand Up @@ -1645,7 +1645,7 @@ export class Message<Cached extends boolean = boolean> extends Base {

public activity: MessageActivity | null;
public applicationId: Snowflake | null;
public attachments: Collection<Snowflake, MessageAttachment>;
public attachments: Collection<Snowflake, Attachment>;
public author: User;
public get channel(): If<Cached, GuildTextBasedChannel, TextBasedChannel>;
public channelId: Snowflake;
Expand Down Expand Up @@ -1710,8 +1710,8 @@ export class Message<Cached extends boolean = boolean> extends Base {
public inGuild(): this is Message<true> & this;
}

export class MessageAttachment {
public constructor(attachment: BufferResolvable | Stream, name?: string, data?: RawMessageAttachmentData);
export class Attachment {
public constructor(attachment: BufferResolvable | Stream, name?: string, data?: RawAttachmentData);

public attachment: BufferResolvable | Stream;
public contentType: string | null;
Expand Down Expand Up @@ -1847,7 +1847,7 @@ export class MessagePayload {
options: string | MessageOptions | WebhookMessageOptions,
extra?: MessageOptions | WebhookMessageOptions,
): MessagePayload;
public static resolveFile(fileLike: BufferResolvable | Stream | FileOptions | MessageAttachment): Promise<RawFile>;
public static resolveFile(fileLike: BufferResolvable | Stream | FileOptions | Attachment): Promise<RawFile>;

public makeContent(): string | undefined;
public resolveBody(): this;
Expand Down Expand Up @@ -3167,7 +3167,7 @@ export class GuildStickerManager extends CachedManager<Snowflake, Sticker, Stick
private constructor(guild: Guild, iterable?: Iterable<RawStickerData>);
public guild: Guild;
public create(
file: BufferResolvable | Stream | FileOptions | MessageAttachment,
file: BufferResolvable | Stream | FileOptions | Attachment,
name: string,
tags: string,
options?: GuildStickerCreateOptions,
Expand Down Expand Up @@ -3905,7 +3905,7 @@ export interface CommandInteractionOption<Cached extends CacheType = CacheType>
member?: CacheTypeReducer<Cached, GuildMember, APIInteractionDataResolvedGuildMember>;
channel?: CacheTypeReducer<Cached, GuildBasedChannel, APIInteractionDataResolvedChannel>;
role?: CacheTypeReducer<Cached, Role, APIRole>;
attachment?: MessageAttachment;
attachment?: Attachment;
message?: GuildCacheMessage<Cached>;
}

Expand All @@ -3915,7 +3915,7 @@ export interface CommandInteractionResolvedData<Cached extends CacheType = Cache
roles?: Collection<Snowflake, CacheTypeReducer<Cached, Role, APIRole>>;
channels?: Collection<Snowflake, CacheTypeReducer<Cached, AnyChannel, APIInteractionDataResolvedChannel>>;
messages?: Collection<Snowflake, CacheTypeReducer<Cached, Message, APIMessage>>;
attachments?: Collection<Snowflake, MessageAttachment>;
attachments?: Collection<Snowflake, Attachment>;
}

export declare const Colors: {
Expand Down Expand Up @@ -4765,10 +4765,10 @@ export type MessageChannelComponentCollectorOptions<T extends MessageComponentIn
>;

export interface MessageEditOptions {
attachments?: MessageAttachment[];
attachments?: Attachment[];
content?: string | null;
embeds?: (JSONEncodable<APIEmbed> | APIEmbed)[] | null;
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];
files?: (FileOptions | BufferResolvable | Stream | Attachment)[];
flags?: BitFieldResolvable<MessageFlagsString, number>;
allowedMentions?: MessageMentionOptions;
components?: (
Expand Down Expand Up @@ -4818,10 +4818,10 @@ export interface MessageOptions {
| APIActionRowComponent<APIMessageActionRowComponent>
)[];
allowedMentions?: MessageMentionOptions;
files?: (FileOptions | BufferResolvable | Stream | MessageAttachment)[];
files?: (FileOptions | BufferResolvable | Stream | Attachment)[];
reply?: ReplyOptions;
stickers?: StickerResolvable[];
attachments?: MessageAttachment[];
attachments?: Attachment[];
flags?: BitFieldResolvable<Extract<MessageFlagsString, 'SuppressEmbeds'>, number>;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/discord.js/typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import {
Interaction,
InteractionCollector,
Message,
MessageAttachment,
Attachment,
MessageCollector,
MessageComponentInteraction,
MessageReaction,
Expand Down Expand Up @@ -584,7 +584,7 @@ client.on('messageCreate', async message => {
assertIsMessage(channel.send({}));
assertIsMessage(channel.send({ embeds: [] }));

const attachment = new MessageAttachment('file.png');
const attachment = new Attachment('file.png');
const embed = new EmbedBuilder();
assertIsMessage(channel.send({ files: [attachment] }));
assertIsMessage(channel.send({ embeds: [embed] }));
Expand Down Expand Up @@ -1456,8 +1456,8 @@ expectNotAssignable<ActionRowData<MessageActionRowComponentData>>({

declare const chatInputInteraction: ChatInputCommandInteraction;

expectType<MessageAttachment>(chatInputInteraction.options.getAttachment('attachment', true));
expectType<MessageAttachment | null>(chatInputInteraction.options.getAttachment('attachment'));
expectType<Attachment>(chatInputInteraction.options.getAttachment('attachment', true));
expectType<Attachment | null>(chatInputInteraction.options.getAttachment('attachment'));

declare const modal: ModalBuilder;

Expand Down
2 changes: 1 addition & 1 deletion packages/discord.js/typings/rawDataTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export type RawInviteStageInstance = APIInviteStageInstance;
export type RawMessageData = APIMessage;
export type RawPartialMessageData = GatewayMessageUpdateDispatchData;

export type RawMessageAttachmentData = APIAttachment;
export type RawAttachmentData = APIAttachment;

export type RawMessagePayloadData =
| RESTPostAPIChannelMessageJSONBody
Expand Down

0 comments on commit ab4c608

Please sign in to comment.