This commit is contained in:
Elysia
2024-01-10 19:27:04 +07:00
parent af1f561cfd
commit 4a1d615673
6 changed files with 345 additions and 347 deletions

View File

@@ -113,14 +113,13 @@ class ChannelManager extends CachedManager {
}
const data = await this.client.api.channels(id).get();
// Delete in cache
this._remove(id);
return this._add(data, null, { cache, allowUnknownGuild });
}
/**
* Create Group DM
* @param {UserResolvable[]} recipients Array of recipients
* @returns {Promise<PartialGroupDMChannel>} Channel
* @returns {Promise<GroupDMChannel>} Channel
*/
async createGroupDM(recipients) {
// Check

View File

@@ -3,7 +3,7 @@
const ThreadManager = require('./ThreadManager');
const { TypeError } = require('../errors');
const MessagePayload = require('../structures/MessagePayload');
const { resolveAutoArchiveMaxLimit, getAttachments, uploadFile } = require('../util/Util');
const { resolveAutoArchiveMaxLimit, getUploadURL, uploadFile } = require('../util/Util');
/**
* Manages API methods for threads in forum channels and stores their cache.
@@ -20,7 +20,7 @@ class GuildForumThreadManager extends ThreadManager {
* @typedef {BaseMessageOptions} GuildForumThreadMessageCreateOptions
* @property {StickerResolvable} [stickers] The stickers to send with the message
* @property {BitFieldResolvable} [flags] The flags to send with the message.
* Only `SUPPRESS_EMBEDS`, `SUPPRESS_NOTIFICATIONS` and `IS_VOICE_MESSAGE` can be set.
* Only `SUPPRESS_EMBEDS` and `SUPPRESS_NOTIFICATIONS` can be set.
*/
/**
@@ -63,35 +63,29 @@ class GuildForumThreadManager extends ThreadManager {
let messagePayload;
if (message instanceof MessagePayload) {
messagePayload = await message.resolveData();
messagePayload = message.resolveData();
} else {
messagePayload = await MessagePayload.create(this, message).resolveData();
messagePayload = MessagePayload.create(this, message).resolveData();
}
let { data: body, files } = await messagePayload.resolveFiles();
const { 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,
description: files[attachment.id].description,
duration_secs: files[attachment.id].duration_secs,
waveform: files[attachment.id].waveform,
};
});
const attachmentsData = await Promise.all(requestPromises);
attachmentsData.sort((a, b) => parseInt(a.id) - parseInt(b.id));
body.attachments = attachmentsData;
files = [];
}
// New API
const attachments = await getUploadURL(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,
description: files[attachment.id].description,
duration_secs: files[attachment.id].duration_secs,
waveform: files[attachment.id].waveform,
};
});
const attachmentsData = await Promise.all(requestPromises);
attachmentsData.sort((a, b) => parseInt(a.id) - parseInt(b.id));
data.attachments = attachmentsData;
if (autoArchiveDuration === 'MAX') autoArchiveDuration = resolveAutoArchiveMaxLimit(this.channel.guild);
@@ -103,7 +97,7 @@ class GuildForumThreadManager extends ThreadManager {
applied_tags: appliedTags,
message: body,
},
files,
files: [],
reason,
});

View File

@@ -2,7 +2,7 @@
const { Collection } = require('@discordjs/collection');
const CachedManager = require('./CachedManager');
const { TypeError, Error } = require('../errors');
const { TypeError } = require('../errors');
const { Message } = require('../structures/Message');
const MessagePayload = require('../structures/MessagePayload');
const Util = require('../util/Util');
@@ -123,38 +123,32 @@ class MessageManager extends CachedManager {
const messageId = this.resolveId(message);
if (!messageId) throw new TypeError('INVALID_TYPE', 'message', 'MessageResolvable');
let messagePayload;
if (options instanceof MessagePayload) {
messagePayload = await options.resolveData();
} else {
messagePayload = await MessagePayload.create(message instanceof Message ? message : this, options).resolveData();
}
let { data, files } = await messagePayload.resolveFiles();
const { data, files } = await (options instanceof MessagePayload
? options
: MessagePayload.create(message instanceof Message ? message : this, options)
)
.resolveData()
.resolveFiles();
if (typeof options == 'object' && typeof options.usingNewAttachmentAPI !== 'boolean') {
options.usingNewAttachmentAPI = this.client.options.usingNewAttachmentAPI;
}
// New API
const attachments = await Util.getUploadURL(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,
description: files[attachment.id].description,
duration_secs: files[attachment.id].duration_secs,
waveform: files[attachment.id].waveform,
};
});
const attachmentsData = await Promise.all(requestPromises);
attachmentsData.sort((a, b) => parseInt(a.id) - parseInt(b.id));
data.attachments = attachmentsData;
// Empty Files
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,
description: files[attachment.id].description,
duration_secs: files[attachment.id].duration_secs,
waveform: files[attachment.id].waveform,
};
});
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 d = await this.client.api.channels[this.channel.id].messages[messageId].patch({ data });
const existing = this.cache.get(messageId);
if (existing) {
@@ -251,12 +245,16 @@ class MessageManager extends CachedManager {
const existing = this.cache.get(messageId);
if (existing && !existing.partial) return existing;
}
// https://discord.com/api/v9/channels/:id/messages?limit=50&around=:msgid
return new Promise((resolve, reject) => {
this._fetchMany({
around: messageId,
limit: 50,
})
this._fetchMany(
{
around: messageId,
limit: 50,
},
cache,
)
.then(data_ =>
data_.has(messageId) ? resolve(data_.get(messageId)) : reject(new Error('MESSAGE_ID_NOT_FOUND')),
)
@@ -264,13 +262,6 @@ class MessageManager extends CachedManager {
});
}
async _fetchMany(options = {}, cache) {
const data = await this.client.api.channels[this.channel.id].messages.get({ query: options });
const messages = new Collection();
for (const message of data) messages.set(message.id, this._add(message, cache));
return messages;
}
/**
* @typedef {object} MessageSearchOptions
* @property {Array<UserResolvable>} [authors] An array of author to filter by
@@ -388,6 +379,13 @@ class MessageManager extends CachedManager {
total: data.total_results,
};
}
async _fetchMany(options = {}, cache) {
const data = await this.client.api.channels[this.channel.id].messages.get({ query: options });
const messages = new Collection();
for (const message of data) messages.set(message.id, this._add(message, cache));
return messages;
}
}
module.exports = MessageManager;