2022-10-03 12:25:39 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const ThreadManager = require('./ThreadManager');
|
|
|
|
const { TypeError } = require('../errors');
|
|
|
|
const MessagePayload = require('../structures/MessagePayload');
|
2024-01-10 12:27:04 +00:00
|
|
|
const { resolveAutoArchiveMaxLimit, getUploadURL, uploadFile } = require('../util/Util');
|
2022-10-03 12:25:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages API methods for threads in forum channels and stores their cache.
|
|
|
|
* @extends {ThreadManager}
|
|
|
|
*/
|
|
|
|
class GuildForumThreadManager extends ThreadManager {
|
|
|
|
/**
|
|
|
|
* The channel this Manager belongs to
|
|
|
|
* @name GuildForumThreadManager#channel
|
|
|
|
* @type {ForumChannel}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {BaseMessageOptions} GuildForumThreadMessageCreateOptions
|
2022-12-18 06:18:42 +00:00
|
|
|
* @property {StickerResolvable} [stickers] The stickers to send with the message
|
2023-03-02 15:11:21 +00:00
|
|
|
* @property {BitFieldResolvable} [flags] The flags to send with the message.
|
2024-01-10 12:27:04 +00:00
|
|
|
* Only `SUPPRESS_EMBEDS` and `SUPPRESS_NOTIFICATIONS` can be set.
|
2022-10-03 12:25:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options for creating a thread.
|
|
|
|
* @typedef {StartThreadOptions} GuildForumThreadCreateOptions
|
|
|
|
* @property {GuildForumThreadMessageCreateOptions|MessagePayload} message The message associated with the thread post
|
|
|
|
* @property {Snowflake[]} [appliedTags] The tags to apply to the thread
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new thread in the channel.
|
|
|
|
* @param {GuildForumThreadCreateOptions} [options] Options to create a new thread
|
|
|
|
* @returns {Promise<ThreadChannel>}
|
|
|
|
* @example
|
|
|
|
* // Create a new forum post
|
|
|
|
* forum.threads
|
|
|
|
* .create({
|
|
|
|
* name: 'Food Talk',
|
|
|
|
* autoArchiveDuration: 60,
|
|
|
|
* message: {
|
|
|
|
* content: 'Discuss your favorite food!',
|
|
|
|
* },
|
|
|
|
* reason: 'Needed a separate thread for food',
|
|
|
|
* })
|
|
|
|
* .then(threadChannel => console.log(threadChannel))
|
|
|
|
* .catch(console.error);
|
|
|
|
*/
|
|
|
|
async create({
|
|
|
|
name,
|
|
|
|
autoArchiveDuration = this.channel.defaultAutoArchiveDuration,
|
|
|
|
message,
|
|
|
|
reason,
|
|
|
|
rateLimitPerUser,
|
|
|
|
appliedTags,
|
|
|
|
} = {}) {
|
|
|
|
if (!message) {
|
|
|
|
throw new TypeError('GUILD_FORUM_MESSAGE_REQUIRED');
|
|
|
|
}
|
|
|
|
|
|
|
|
let messagePayload;
|
|
|
|
|
|
|
|
if (message instanceof MessagePayload) {
|
2024-01-10 12:27:04 +00:00
|
|
|
messagePayload = message.resolveData();
|
2022-10-03 12:25:39 +00:00
|
|
|
} else {
|
2024-01-10 12:27:04 +00:00
|
|
|
messagePayload = MessagePayload.create(this, message).resolveData();
|
2022-10-03 12:25:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 12:27:04 +00:00
|
|
|
const { data: body, files } = await messagePayload.resolveFiles();
|
2022-11-27 12:45:58 +00:00
|
|
|
|
2024-01-10 12:27:04 +00:00
|
|
|
// 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;
|
2022-10-03 12:25:39 +00:00
|
|
|
|
|
|
|
if (autoArchiveDuration === 'MAX') autoArchiveDuration = resolveAutoArchiveMaxLimit(this.channel.guild);
|
|
|
|
|
2022-10-26 12:21:40 +00:00
|
|
|
const data = await this.client.api.channels(this.channel.id).threads.post({
|
2022-10-03 12:25:39 +00:00
|
|
|
data: {
|
|
|
|
name,
|
|
|
|
auto_archive_duration: autoArchiveDuration,
|
|
|
|
rate_limit_per_user: rateLimitPerUser,
|
|
|
|
applied_tags: appliedTags,
|
|
|
|
message: body,
|
|
|
|
},
|
2024-01-10 12:27:04 +00:00
|
|
|
files: [],
|
2022-10-03 12:25:39 +00:00
|
|
|
reason,
|
|
|
|
});
|
|
|
|
|
|
|
|
return this.client.actions.ThreadCreate.handle(data).thread;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = GuildForumThreadManager;
|