feat: Using new attachment API

This commit is contained in:
March 7th
2022-11-27 19:45:58 +07:00
parent d97cf71661
commit c70dc733e2
8 changed files with 105 additions and 5 deletions

View File

@@ -3,7 +3,7 @@
const ThreadManager = require('./ThreadManager');
const { TypeError } = require('../errors');
const MessagePayload = require('../structures/MessagePayload');
const { resolveAutoArchiveMaxLimit } = require('../util/Util');
const { resolveAutoArchiveMaxLimit, getAttachments, uploadFile } = require('../util/Util');
/**
* Manages API methods for threads in forum channels and stores their cache.
@@ -67,7 +67,27 @@ class GuildForumThreadManager extends ThreadManager {
messagePayload = MessagePayload.create(this, message).resolveData();
}
const { data: body, files } = await messagePayload.resolveFiles();
let { data: body, files } = await messagePayload.resolveFiles();
if (typeof message == 'object' && typeof message.usingNewAttachmentAPI !== 'boolean') {
message.usingNewAttachmentAPI = this.client.options.usingNewAttachmentAPI;
}
if (message?.usingNewAttachmentAPI === true) {
const attachments = await getAttachments(this.client, this.channel.id, ...files);
const requestPromises = attachments.map(async attachment => {
await uploadFile(files[attachment.id].file, attachment.upload_url);
return {
id: attachment.id,
filename: files[attachment.id].name,
uploaded_filename: attachment.upload_filename,
};
});
const attachmentsData = await Promise.all(requestPromises);
attachmentsData.sort((a, b) => parseInt(a.id) - parseInt(b.id));
body.attachments = attachmentsData;
files = [];
}
if (autoArchiveDuration === 'MAX') autoArchiveDuration = resolveAutoArchiveMaxLimit(this.channel.guild);

View File

@@ -130,7 +130,28 @@ class MessageManager extends CachedManager {
} else {
messagePayload = await MessagePayload.create(message instanceof Message ? message : this, options).resolveData();
}
const { data, files } = await messagePayload.resolveFiles();
let { data, files } = await messagePayload.resolveFiles();
if (typeof options == 'object' && typeof options.usingNewAttachmentAPI !== 'boolean') {
options.usingNewAttachmentAPI = this.client.options.usingNewAttachmentAPI;
}
if (options?.usingNewAttachmentAPI === true) {
const attachments = await Util.getAttachments(this.client, this.channel.id, ...files);
const requestPromises = attachments.map(async attachment => {
await Util.uploadFile(files[attachment.id].file, attachment.upload_url);
return {
id: attachment.id,
filename: files[attachment.id].name,
uploaded_filename: attachment.upload_filename,
};
});
const attachmentsData = await Promise.all(requestPromises);
attachmentsData.sort((a, b) => parseInt(a.id) - parseInt(b.id));
data.attachments = attachmentsData;
files = [];
}
const d = await this.client.api.channels[this.channel.id].messages[messageId].patch({ data, files });
const existing = this.cache.get(messageId);