Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MessageAttachment): description (alt text) support #6871

Merged
merged 9 commits into from
Nov 11, 2021
4 changes: 2 additions & 2 deletions src/rest/APIRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class APIRequest {
let body;
if (this.options.files?.length) {
body = new FormData();
for (const file of this.options.files) {
if (file?.file) body.append(file.key ?? file.name, file.file, file.name);
for (const [index, file] of this.options.files.entries()) {
if (file?.file) body.append(`files[${index}]`, file.file, file.name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this break emoji create?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emoji creates seem to work fine but I went to test stickers and can't get sticker creation working even after reverting this.

Would appreciate if someone else who knows how that's meant to work could give it a try to make sure I didn't break it and am just being dumb

Copy link
Contributor

@advaith1 advaith1 Oct 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote sticker uploading and it uses file.key; can you just set the key to files[${index}] in the relevant message posting code instead of modifying this? (you can also remove ?? file.name after doing that if you want)

(emoji create uses a data uri in json)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After taking a look around, it seems adding file.key ?? should work but I'm still unable to get sticker creation working with or without my changes.

@advaith1 could you try with my latest changes and check to make sure sticker creation still works? I've checked sending a message with attachments and that works fine so all that's left to check (to my knowledge) is sticker creation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has this been resolved?

Copy link
Contributor

@advaith1 advaith1 Nov 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uploading stickers works for me, on this branch and on main (using the example code)

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd still prefer that the `files[${index}]` logic is moved to the message code (which would just set that as file.key, for consistency) instead of the request code, but that isn't really necessary

}
if (typeof this.options.data !== 'undefined') {
if (this.options.dontUsePayloadJSON) {
Expand Down
20 changes: 20 additions & 0 deletions src/structures/MessageAttachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class MessageAttachment {
if (data) this._patch(data);
}

/**
* Sets the description of this attachment.
* @param {string} description The description of the file
* @returns {MessageAttachment} This attachment
*/
setDescription(description) {
this.description = description;
return this;
}

/**
* Sets the file of this attachment.
* @param {BufferResolvable|Stream} attachment The file
Expand Down Expand Up @@ -122,6 +132,16 @@ class MessageAttachment {
this.contentType ??= null;
}

if ('description' in data) {
/**
* This description (alt text) of this attachment
GamingGeek marked this conversation as resolved.
Show resolved Hide resolved
* @type {?string}
*/
this.description = data.description;
} else {
this.description ??= null;
}

/**
* Whether this attachment is ephemeral
* @type {boolean}
Expand Down
10 changes: 10 additions & 0 deletions src/structures/MessagePayload.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,16 @@ class MessagePayload {
}
}

const attachments = this.options.files?.map((file, index) => ({
id: index.toString(),
description: file.description,
}));
if (Array.isArray(this.options.attachments)) {
this.options.attachments.push(...attachments);
} else {
this.options.attachments = attachments;
}

this.data = {
content,
tts,
Expand Down
2 changes: 2 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,7 @@ export class MessageAttachment {

public attachment: BufferResolvable | Stream;
public contentType: string | null;
public description: string | null;
public ephemeral: boolean;
public height: number | null;
public id: Snowflake;
Expand All @@ -1384,6 +1385,7 @@ export class MessageAttachment {
public readonly spoiler: boolean;
public url: string;
public width: number | null;
public setDescription(description: string): this;
public setFile(attachment: BufferResolvable | Stream, name?: string): this;
public setName(name: string): this;
public setSpoiler(spoiler?: boolean): this;
Expand Down