diff --git a/src/helpers/channels/threads/start_private_thread.ts b/src/helpers/channels/threads/start_private_thread.ts new file mode 100644 index 000000000..f43078459 --- /dev/null +++ b/src/helpers/channels/threads/start_private_thread.ts @@ -0,0 +1,22 @@ +import { cacheHandlers } from "../../../cache.ts"; +import { rest } from "../../../rest/rest.ts"; +import { StartThread } from "../../../types/channels/threads/start_thread.ts"; +import { Errors } from "../../../types/discordeno/errors.ts"; +import { endpoints } from "../../../util/constants.ts"; +import { requireBotChannelPermissions } from "../../../util/permissions.ts"; +import { channelToThread } from "../../../util/transformers/channel_to_thread.ts"; +import { snakelize } from "../../../util/utils.ts"; + +/** Creates a new private thread. Returns a thread channel. */ +export async function startPrivateThread(channelId: bigint, options: StartThread) { + const channel = await cacheHandlers.get("channels", channelId); + if (channel) { + if (!channel.isGuildTextBasedChannel) throw new Error(Errors.INVALID_THREAD_PARENT_CHANNEL_TYPE); + + if (channel.isNewsChannel) throw new Error(Errors.GUILD_NEWS_CHANNEL_ONLY_SUPPORT_PUBLIC_THREADS); + + await requireBotChannelPermissions(channel, ["USE_PRIVATE_THREADS"]); + } + + return channelToThread(await rest.runMethod("post", endpoints.THREAD_START_PRIVATE(channelId), snakelize(options))); +} diff --git a/src/helpers/channels/threads/start_thread.ts b/src/helpers/channels/threads/start_thread.ts index fcb3e7117..7d76ab1e5 100644 --- a/src/helpers/channels/threads/start_thread.ts +++ b/src/helpers/channels/threads/start_thread.ts @@ -1,36 +1,25 @@ import { cacheHandlers } from "../../../cache.ts"; import { rest } from "../../../rest/rest.ts"; -import { ChannelTypes } from "../../../types/channels/channel_types.ts"; import { StartThread } from "../../../types/channels/threads/start_thread.ts"; import { Errors } from "../../../types/discordeno/errors.ts"; import { endpoints } from "../../../util/constants.ts"; import { requireBotChannelPermissions } from "../../../util/permissions.ts"; import { snakelize } from "../../../util/utils.ts"; -/** - * Creates a new public thread from an existing message. Returns a channel on success, and a 400 BAD REQUEST on invalid parameters. Fires a Thread Create Gateway event. - * @param messageId when provided the thread will be public - */ -export async function startThread(channelId: bigint, options: StartThread & { messageId?: bigint }) { +/** Creates a new public thread from an existing message. Returns a thread channel. */ +export async function startThread(channelId: bigint, options: StartThread & { messageId: bigint }) { const channel = await cacheHandlers.get("channels", channelId); if (channel) { - // TODO(threads): perm check - if (![ChannelTypes.GuildText, ChannelTypes.GuildNews].includes(channel.type)) { + if (!channel.isGuildTextBasedChannel) { throw new Error(Errors.INVALID_THREAD_PARENT_CHANNEL_TYPE); } - if (!options.messageId && channel.type === ChannelTypes.GuildNews) { - throw new Error(Errors.GUILD_NEWS_CHANNEL_ONLY_SUPPORT_PUBLIC_THREADS); - } - - await requireBotChannelPermissions(channel, options.messageId ? ["USE_PUBLIC_THREADS"] : ["USE_PRIVATE_THREADS"]); + await requireBotChannelPermissions(channel, ["USE_PUBLIC_THREADS"]); } return await rest.runMethod( "post", - options?.messageId - ? endpoints.THREAD_START_PUBLIC(channelId, options.messageId) - : endpoints.THREAD_START_PRIVATE(channelId), + endpoints.THREAD_START_PUBLIC(channelId, options.messageId), snakelize(options) ); }