feat: Guild Forum channel
https://github.com/discordjs/discord.js/pull/8651
This commit is contained in:
@@ -9,10 +9,11 @@ const GuildChannel = require('../structures/GuildChannel');
|
||||
const PermissionOverwrites = require('../structures/PermissionOverwrites');
|
||||
const ThreadChannel = require('../structures/ThreadChannel');
|
||||
const Webhook = require('../structures/Webhook');
|
||||
const { ThreadChannelTypes, ChannelTypes, VideoQualityModes } = require('../util/Constants');
|
||||
const ChannelFlags = require('../util/ChannelFlags');
|
||||
const { ThreadChannelTypes, ChannelTypes, VideoQualityModes, SortOrderTypes } = require('../util/Constants');
|
||||
const DataResolver = require('../util/DataResolver');
|
||||
const Util = require('../util/Util');
|
||||
const { resolveAutoArchiveMaxLimit } = require('../util/Util');
|
||||
const { resolveAutoArchiveMaxLimit, transformGuildForumTag, transformGuildDefaultReaction } = require('../util/Util');
|
||||
|
||||
let cacheWarningEmitted = false;
|
||||
let storeChannelDeprecationEmitted = false;
|
||||
@@ -73,8 +74,9 @@ class GuildChannelManager extends CachedManager {
|
||||
* Data that can be resolved to give a Guild Channel object. This can be:
|
||||
* * A GuildChannel object
|
||||
* * A ThreadChannel object
|
||||
* * A ForumChannel object
|
||||
* * A Snowflake
|
||||
* @typedef {GuildChannel|ThreadChannel|Snowflake} GuildChannelResolvable
|
||||
* @typedef {GuildChannel|ThreadChannel|ForumChannel|Snowflake} GuildChannelResolvable
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -138,6 +140,10 @@ class GuildChannelManager extends CachedManager {
|
||||
position,
|
||||
rateLimitPerUser,
|
||||
rtcRegion,
|
||||
videoQualityMode,
|
||||
availableTags,
|
||||
defaultReactionEmoji,
|
||||
defaultSortOrder,
|
||||
reason,
|
||||
} = {},
|
||||
) {
|
||||
@@ -145,6 +151,10 @@ class GuildChannelManager extends CachedManager {
|
||||
permissionOverwrites &&= permissionOverwrites.map(o => PermissionOverwrites.resolve(o, this.guild));
|
||||
const intType = typeof type === 'number' ? type : ChannelTypes[type] ?? ChannelTypes.GUILD_TEXT;
|
||||
|
||||
const videoMode = typeof videoQualityMode === 'number' ? videoQualityMode : VideoQualityModes[videoQualityMode];
|
||||
|
||||
const sortMode = typeof defaultSortOrder === 'number' ? defaultSortOrder : SortOrderTypes[defaultSortOrder];
|
||||
|
||||
if (intType === ChannelTypes.GUILD_STORE && !storeChannelDeprecationEmitted) {
|
||||
storeChannelDeprecationEmitted = true;
|
||||
process.emitWarning(
|
||||
@@ -167,6 +177,10 @@ class GuildChannelManager extends CachedManager {
|
||||
permission_overwrites: permissionOverwrites,
|
||||
rate_limit_per_user: rateLimitPerUser,
|
||||
rtc_region: rtcRegion,
|
||||
video_quality_mode: videoMode,
|
||||
available_tags: availableTags?.map(availableTag => transformGuildForumTag(availableTag)),
|
||||
default_reaction_emoji: defaultReactionEmoji && transformGuildDefaultReaction(defaultReactionEmoji),
|
||||
default_sort_order: sortMode,
|
||||
},
|
||||
reason,
|
||||
});
|
||||
@@ -224,6 +238,11 @@ class GuildChannelManager extends CachedManager {
|
||||
* The default auto archive duration for all new threads in this channel
|
||||
* @property {?string} [rtcRegion] The RTC region of the channel
|
||||
* @property {?VideoQualityMode|number} [videoQualityMode] The camera video quality mode of the channel
|
||||
* @property {ChannelFlagsResolvable} [flags] The flags to set on the channel
|
||||
* @property {GuildForumTagData[]} [availableTags] The tags to set as available in a forum channel
|
||||
* @property {?DefaultReactionEmoji} [defaultReactionEmoji] The emoji to set as the default reaction emoji
|
||||
* @property {number} [defaultThreadRateLimitPerUser] The rate limit per user (slowmode) to set on forum posts
|
||||
* @property {?SortOrderType} [defaultSortOrder] The default sort order mode to set on the channel
|
||||
*/
|
||||
|
||||
/**
|
||||
@@ -282,6 +301,12 @@ class GuildChannelManager extends CachedManager {
|
||||
rate_limit_per_user: data.rateLimitPerUser,
|
||||
default_auto_archive_duration: defaultAutoArchiveDuration,
|
||||
permission_overwrites,
|
||||
available_tags: data.availableTags?.map(availableTag => transformGuildForumTag(availableTag)),
|
||||
default_reaction_emoji: data.defaultReactionEmoji && transformGuildDefaultReaction(data.defaultReactionEmoji),
|
||||
default_thread_rate_limit_per_user: data.defaultThreadRateLimitPerUser,
|
||||
flags: 'flags' in data ? ChannelFlags.resolve(data.flags) : undefined,
|
||||
default_sort_order:
|
||||
typeof data.defaultSortOrder === 'string' ? SortOrderTypes[data.defaultSortOrder] : data.defaultSortOrder,
|
||||
},
|
||||
reason,
|
||||
});
|
||||
|
92
src/managers/GuildForumThreadManager.js
Normal file
92
src/managers/GuildForumThreadManager.js
Normal file
@@ -0,0 +1,92 @@
|
||||
'use strict';
|
||||
|
||||
const ThreadManager = require('./ThreadManager');
|
||||
const { TypeError } = require('../errors');
|
||||
const MessagePayload = require('../structures/MessagePayload');
|
||||
const { resolveAutoArchiveMaxLimit } = require('../util/Util');
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @property {stickers} [stickers] The stickers to send with the message
|
||||
* @property {BitFieldResolvable} [flags] The flags to send with the message
|
||||
*/
|
||||
|
||||
/**
|
||||
* 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,
|
||||
} = {}) {
|
||||
let path = this.client.api.channels(this.channel.id);
|
||||
|
||||
if (!message) {
|
||||
throw new TypeError('GUILD_FORUM_MESSAGE_REQUIRED');
|
||||
}
|
||||
|
||||
let messagePayload;
|
||||
|
||||
if (message instanceof MessagePayload) {
|
||||
messagePayload = message.resolveData();
|
||||
} else {
|
||||
messagePayload = MessagePayload.create(this, message).resolveData();
|
||||
}
|
||||
|
||||
const { data: body, files } = await messagePayload.resolveFiles();
|
||||
|
||||
if (autoArchiveDuration === 'MAX') autoArchiveDuration = resolveAutoArchiveMaxLimit(this.channel.guild);
|
||||
|
||||
const data = await path.threads.post({
|
||||
data: {
|
||||
name,
|
||||
auto_archive_duration: autoArchiveDuration,
|
||||
rate_limit_per_user: rateLimitPerUser,
|
||||
applied_tags: appliedTags,
|
||||
message: body,
|
||||
},
|
||||
files,
|
||||
reason,
|
||||
});
|
||||
|
||||
return this.client.actions.ThreadCreate.handle(data).thread;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GuildForumThreadManager;
|
98
src/managers/GuildTextThreadManager.js
Normal file
98
src/managers/GuildTextThreadManager.js
Normal file
@@ -0,0 +1,98 @@
|
||||
'use strict';
|
||||
|
||||
const ThreadManager = require('./ThreadManager');
|
||||
const { TypeError } = require('../errors');
|
||||
const { ChannelTypes } = require('../util/Constants');
|
||||
const { resolveAutoArchiveMaxLimit } = require('../util/Util');
|
||||
|
||||
/**
|
||||
* Manages API methods for {@link ThreadChannel} objects and stores their cache.
|
||||
* @extends {ThreadManager}
|
||||
*/
|
||||
class GuildTextThreadManager extends ThreadManager {
|
||||
/**
|
||||
* The channel this Manager belongs to
|
||||
* @name GuildTextThreadManager#channel
|
||||
* @type {TextChannel|NewsChannel}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Options for creating a thread. <warn>Only one of `startMessage` or `type` can be defined.</warn>
|
||||
* @typedef {StartThreadOptions} GuildTextThreadCreateOptions
|
||||
* @property {MessageResolvable} [startMessage] The message to start a thread from. <warn>If this is defined then type
|
||||
* of thread gets automatically defined and cannot be changed. The provided `type` field will be ignored</warn>
|
||||
* @property {ThreadChannelTypes|number} [type] The type of thread to create. Defaults to `GUILD_PUBLIC_THREAD` if
|
||||
* created in a {@link TextChannel} <warn>When creating threads in a {@link NewsChannel} this is ignored and is always
|
||||
* `GUILD_NEWS_THREAD`</warn>
|
||||
* @property {boolean} [invitable] Whether non-moderators can add other non-moderators to the thread
|
||||
* <info>Can only be set when type will be `GUILD_PRIVATE_THREAD`</info>
|
||||
* @property {number} [rateLimitPerUser] The rate limit per user (slowmode) for the new channel in seconds
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a new thread in the channel.
|
||||
* @param {GuildTextThreadCreateOptions} [options] Options to create a new thread
|
||||
* @returns {Promise<ThreadChannel>}
|
||||
* @example
|
||||
* // Create a new public thread
|
||||
* channel.threads
|
||||
* .create({
|
||||
* name: 'food-talk',
|
||||
* autoArchiveDuration: 60,
|
||||
* reason: 'Needed a separate thread for food',
|
||||
* })
|
||||
* .then(threadChannel => console.log(threadChannel))
|
||||
* .catch(console.error);
|
||||
* @example
|
||||
* // Create a new private thread
|
||||
* channel.threads
|
||||
* .create({
|
||||
* name: 'mod-talk',
|
||||
* autoArchiveDuration: 60,
|
||||
* type: 'GUILD_PRIVATE_THREAD',
|
||||
* reason: 'Needed a separate thread for moderation',
|
||||
* })
|
||||
* .then(threadChannel => console.log(threadChannel))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
async create({
|
||||
name,
|
||||
autoArchiveDuration = this.channel.defaultAutoArchiveDuration,
|
||||
startMessage,
|
||||
type,
|
||||
invitable,
|
||||
reason,
|
||||
rateLimitPerUser,
|
||||
} = {}) {
|
||||
let path = this.client.api.channels(this.channel.id);
|
||||
if (type && typeof type !== 'string' && typeof type !== 'number') {
|
||||
throw new TypeError('INVALID_TYPE', 'type', 'ThreadChannelType or Number');
|
||||
}
|
||||
let resolvedType =
|
||||
this.channel.type === 'GUILD_NEWS' ? ChannelTypes.GUILD_NEWS_THREAD : ChannelTypes.GUILD_PUBLIC_THREAD;
|
||||
if (startMessage) {
|
||||
const startMessageId = this.channel.messages.resolveId(startMessage);
|
||||
if (!startMessageId) throw new TypeError('INVALID_TYPE', 'startMessage', 'MessageResolvable');
|
||||
path = path.messages(startMessageId);
|
||||
} else if (this.channel.type !== 'GUILD_NEWS') {
|
||||
resolvedType = typeof type === 'string' ? ChannelTypes[type] : type ?? resolvedType;
|
||||
}
|
||||
|
||||
if (autoArchiveDuration === 'MAX') autoArchiveDuration = resolveAutoArchiveMaxLimit(this.channel.guild);
|
||||
|
||||
const data = await path.threads.post({
|
||||
data: {
|
||||
name,
|
||||
auto_archive_duration: autoArchiveDuration,
|
||||
type: resolvedType,
|
||||
invitable: resolvedType === ChannelTypes.GUILD_PRIVATE_THREAD ? invitable : undefined,
|
||||
rate_limit_per_user: rateLimitPerUser,
|
||||
},
|
||||
reason,
|
||||
});
|
||||
|
||||
return this.client.actions.ThreadCreate.handle(data).thread;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GuildTextThreadManager;
|
@@ -4,8 +4,6 @@ const { Collection } = require('@discordjs/collection');
|
||||
const CachedManager = require('./CachedManager');
|
||||
const { TypeError } = require('../errors');
|
||||
const ThreadChannel = require('../structures/ThreadChannel');
|
||||
const { ChannelTypes } = require('../util/Constants');
|
||||
const { resolveAutoArchiveMaxLimit } = require('../util/Util');
|
||||
|
||||
/**
|
||||
* Manages API methods for {@link ThreadChannel} objects and stores their cache.
|
||||
@@ -60,84 +58,6 @@ class ThreadManager extends CachedManager {
|
||||
* @returns {?Snowflake}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Options for creating a thread. <warn>Only one of `startMessage` or `type` can be defined.</warn>
|
||||
* @typedef {StartThreadOptions} ThreadCreateOptions
|
||||
* @property {MessageResolvable} [startMessage] The message to start a thread from. <warn>If this is defined then type
|
||||
* of thread gets automatically defined and cannot be changed. The provided `type` field will be ignored</warn>
|
||||
* @property {ThreadChannelTypes|number} [type] The type of thread to create. Defaults to `GUILD_PUBLIC_THREAD` if
|
||||
* created in a {@link TextChannel} <warn>When creating threads in a {@link NewsChannel} this is ignored and is always
|
||||
* `GUILD_NEWS_THREAD`</warn>
|
||||
* @property {boolean} [invitable] Whether non-moderators can add other non-moderators to the thread
|
||||
* <info>Can only be set when type will be `GUILD_PRIVATE_THREAD`</info>
|
||||
* @property {number} [rateLimitPerUser] The rate limit per user (slowmode) for the new channel in seconds
|
||||
*/
|
||||
|
||||
/**
|
||||
* Creates a new thread in the channel.
|
||||
* @param {ThreadCreateOptions} [options] Options to create a new thread
|
||||
* @returns {Promise<ThreadChannel>}
|
||||
* @example
|
||||
* // Create a new public thread
|
||||
* channel.threads
|
||||
* .create({
|
||||
* name: 'food-talk',
|
||||
* autoArchiveDuration: 60,
|
||||
* reason: 'Needed a separate thread for food',
|
||||
* })
|
||||
* .then(threadChannel => console.log(threadChannel))
|
||||
* .catch(console.error);
|
||||
* @example
|
||||
* // Create a new private thread
|
||||
* channel.threads
|
||||
* .create({
|
||||
* name: 'mod-talk',
|
||||
* autoArchiveDuration: 60,
|
||||
* type: 'GUILD_PRIVATE_THREAD',
|
||||
* reason: 'Needed a separate thread for moderation',
|
||||
* })
|
||||
* .then(threadChannel => console.log(threadChannel))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
async create({
|
||||
name,
|
||||
autoArchiveDuration = this.channel.defaultAutoArchiveDuration,
|
||||
startMessage,
|
||||
type,
|
||||
invitable,
|
||||
reason,
|
||||
rateLimitPerUser,
|
||||
} = {}) {
|
||||
let path = this.client.api.channels(this.channel.id);
|
||||
if (type && typeof type !== 'string' && typeof type !== 'number') {
|
||||
throw new TypeError('INVALID_TYPE', 'type', 'ThreadChannelType or Number');
|
||||
}
|
||||
let resolvedType =
|
||||
this.channel.type === 'GUILD_NEWS' ? ChannelTypes.GUILD_NEWS_THREAD : ChannelTypes.GUILD_PUBLIC_THREAD;
|
||||
if (startMessage) {
|
||||
const startMessageId = this.channel.messages.resolveId(startMessage);
|
||||
if (!startMessageId) throw new TypeError('INVALID_TYPE', 'startMessage', 'MessageResolvable');
|
||||
path = path.messages(startMessageId);
|
||||
} else if (this.channel.type !== 'GUILD_NEWS') {
|
||||
resolvedType = typeof type === 'string' ? ChannelTypes[type] : type ?? resolvedType;
|
||||
}
|
||||
|
||||
if (autoArchiveDuration === 'MAX') autoArchiveDuration = resolveAutoArchiveMaxLimit(this.channel.guild);
|
||||
|
||||
const data = await path.threads.post({
|
||||
data: {
|
||||
name,
|
||||
auto_archive_duration: autoArchiveDuration,
|
||||
type: resolvedType,
|
||||
invitable: resolvedType === ChannelTypes.GUILD_PRIVATE_THREAD ? invitable : undefined,
|
||||
rate_limit_per_user: rateLimitPerUser,
|
||||
},
|
||||
reason,
|
||||
});
|
||||
|
||||
return this.client.actions.ThreadCreate.handle(data).thread;
|
||||
}
|
||||
|
||||
/**
|
||||
* The options for fetching multiple threads, the properties are mutually exclusive
|
||||
* @typedef {Object} FetchThreadsOptions
|
||||
|
Reference in New Issue
Block a user