diff --git a/src/helpers/channels/delete_channel.ts b/src/helpers/channels/delete_channel.ts index 8510207d7..894cb4883 100644 --- a/src/helpers/channels/delete_channel.ts +++ b/src/helpers/channels/delete_channel.ts @@ -12,8 +12,6 @@ export async function deleteChannel(channelId: bigint, reason?: string) { const guild = await cacheHandlers.get("guilds", channel.guildId); if (!guild) throw new Error(Errors.GUILD_NOT_FOUND); - // TODO(threads): check if this requires guild perms or channel is enough - await requireBotGuildPermissions(guild, channel.isThreadChannel ? ["MANAGE_THREADS"] : ["MANAGE_CHANNELS"]); if (guild.rulesChannelId === channelId) { throw new Error(Errors.RULES_CHANNEL_CANNOT_BE_DELETED); } @@ -21,6 +19,9 @@ export async function deleteChannel(channelId: bigint, reason?: string) { if (guild.publicUpdatesChannelId === channelId) { throw new Error(Errors.UPDATES_CHANNEL_CANNOT_BE_DELETED); } + + // TODO(threads): check if this requires guild perms or channel is enough + await requireBotGuildPermissions(guild, ["MANAGE_CHANNELS"]); } return await rest.runMethod("delete", endpoints.CHANNEL_BASE(channelId), { reason }); diff --git a/src/helpers/channels/edit_channel.ts b/src/helpers/channels/edit_channel.ts index 5cd8befb4..d4805f026 100644 --- a/src/helpers/channels/edit_channel.ts +++ b/src/helpers/channels/edit_channel.ts @@ -5,41 +5,22 @@ import type { DiscordenoChannel } from "../../structures/channel.ts"; import { structures } from "../../structures/mod.ts"; import type { Channel } from "../../types/channels/channel.ts"; import type { ModifyChannel } from "../../types/channels/modify_channel.ts"; -import type { ModifyThread } from "../../types/channels/threads/modify_thread.ts"; -import type { PermissionStrings } from "../../types/permissions/permission_strings.ts"; import { endpoints } from "../../util/constants.ts"; -import { calculateBits, requireBotChannelPermissions, requireOverwritePermissions } from "../../util/permissions.ts"; -import { hasOwnProperty, snakelize } from "../../util/utils.ts"; +import { calculateBits, requireOverwritePermissions } from "../../util/permissions.ts"; +import { snakelize } from "../../util/utils.ts"; //TODO: implement DM group channel edit -//TODO(threads): check thread perms /** Update a channel's settings. Requires the `MANAGE_CHANNELS` permission for the guild. */ -export async function editChannel(channelId: bigint, options: ModifyChannel | ModifyThread, reason?: string) { +export async function editChannel(channelId: bigint, options: ModifyChannel, reason?: string) { const channel = await cacheHandlers.get("channels", channelId); if (channel) { - if (channel.isThreadChannel) { - const permissions = new Set(); - - if (hasOwnProperty(options, "archive") && options.archive === false) { - permissions.add("SEND_MESSAGES"); - } - - // TODO(threads): change this to a better check - // hacky way of checking if more is being modified - if (Object.keys(options).length > 1) { - permissions.add("MANAGE_THREADS"); - } - - await requireBotChannelPermissions(channel.parentId ?? 0n, [...permissions]); - } - - if (hasOwnProperty(options, "permissionOverwrites") && Array.isArray(options.permissionOverwrites)) { + if (options.permissionOverwrites && Array.isArray(options.permissionOverwrites)) { await requireOverwritePermissions(channel.guildId, options.permissionOverwrites); } } - if (options.name || (options as ModifyChannel).topic) { + if (options.name || options.topic) { const request = editChannelNameTopicQueue.get(channelId); if (!request) { // If this hasnt been done before simply add 1 for it @@ -70,7 +51,7 @@ export async function editChannel(channelId: bigint, options: ModifyChannel | Mo endpoints.CHANNEL_BASE(channelId), snakelize({ ...options, - permissionOverwrites: hasOwnProperty(options, "permissionOverwrites") + permissionOverwrites: options.permissionOverwrites ? options.permissionOverwrites?.map((overwrite) => { return { ...overwrite, diff --git a/src/helpers/channels/threads/archive_thread.ts b/src/helpers/channels/threads/archive_thread.ts index 1a3260fc0..bb0254fb3 100644 --- a/src/helpers/channels/threads/archive_thread.ts +++ b/src/helpers/channels/threads/archive_thread.ts @@ -1,6 +1,6 @@ -import { editChannel } from "../edit_channel.ts"; +import { editThread } from "./edit_thread.ts"; /** Sets a thread channel to be archived. */ export function archiveThread(threadId: bigint) { - return editChannel(threadId, { archived: true }); + return editThread(threadId, { archived: true }); } diff --git a/src/helpers/channels/threads/delete_thread.ts b/src/helpers/channels/threads/delete_thread.ts new file mode 100644 index 000000000..cec0331de --- /dev/null +++ b/src/helpers/channels/threads/delete_thread.ts @@ -0,0 +1,15 @@ +import { cacheHandlers } from "../../../cache.ts"; +import { rest } from "../../../rest/rest.ts"; +import { endpoints } from "../../../util/constants.ts"; +import { requireBotGuildPermissions } from "../../../util/permissions.ts"; + +/** Delete a thread in your server. Bot needs MANAGE_THREADS permissions in the server. */ +export async function deleteThread(threadId: bigint, reason?: string) { + const thread = await cacheHandlers.get("threads", threadId); + if (thread) { + const channel = await cacheHandlers.get("channels", thread?.channelId); + if (channel?.guildId) await requireBotGuildPermissions(channel.guildId, ["MANAGE_THREADS"]); + } + + return await rest.runMethod("delete", endpoints.CHANNEL_BASE(threadId), { reason }); +} diff --git a/src/helpers/channels/threads/edit_thread.ts b/src/helpers/channels/threads/edit_thread.ts new file mode 100644 index 000000000..2bcbe621d --- /dev/null +++ b/src/helpers/channels/threads/edit_thread.ts @@ -0,0 +1,21 @@ +import { rest } from "../../../rest/rest.ts"; +import { ModifyThread } from "../../../types/channels/threads/modify_thread.ts"; +import { endpoints } from "../../../util/constants.ts"; +import { channelToThread } from "../../../util/transformers/channel_to_thread.ts"; +import { snakelize } from "../../../util/utils.ts"; + +/** Update a thread's settings. Requires the `MANAGE_CHANNELS` permission for the guild. */ +export async function editThread(threadId: bigint, options: ModifyThread, reason?: string) { + // const thread = await cacheHandlers.get("threads", threadId); + + const result = await rest.runMethod( + "patch", + endpoints.CHANNEL_BASE(threadId), + snakelize({ + ...options, + reason, + }) + ); + + return channelToThread(result); +} diff --git a/src/helpers/channels/threads/lock_thread.ts b/src/helpers/channels/threads/lock_thread.ts index f2ec4cdf0..3e2cace3c 100644 --- a/src/helpers/channels/threads/lock_thread.ts +++ b/src/helpers/channels/threads/lock_thread.ts @@ -1,6 +1,6 @@ -import { editChannel } from "../edit_channel.ts"; +import { editThread } from "./edit_thread.ts"; /** Sets a thread channel to be locked. */ export function lockThread(threadId: bigint) { - return editChannel(threadId, { locked: true }); + return editThread(threadId, { locked: true }); } diff --git a/src/helpers/channels/threads/unarchive_thread.ts b/src/helpers/channels/threads/unarchive_thread.ts index 981823946..03671aceb 100644 --- a/src/helpers/channels/threads/unarchive_thread.ts +++ b/src/helpers/channels/threads/unarchive_thread.ts @@ -1,6 +1,6 @@ -import { editChannel } from "../edit_channel.ts"; +import { editThread } from "./edit_thread.ts"; /** Sets a thread channel to be unarchived. */ export function unarchiveThread(threadId: bigint) { - return editChannel(threadId, { archived: false }); + return editThread(threadId, { archived: false }); } diff --git a/src/helpers/channels/threads/unlock_thread.ts b/src/helpers/channels/threads/unlock_thread.ts index 1e8b92cac..e410c77bc 100644 --- a/src/helpers/channels/threads/unlock_thread.ts +++ b/src/helpers/channels/threads/unlock_thread.ts @@ -1,6 +1,6 @@ -import { editChannel } from "../edit_channel.ts"; +import { editThread } from "./edit_thread.ts"; /** Sets a thread channel to be unlocked. */ export function unlockThread(threadId: bigint) { - return editChannel(threadId, { locked: false }); + return editThread(threadId, { locked: false }); } diff --git a/src/structures/channel.ts b/src/structures/channel.ts index 4797ee424..c7fb45fff 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -82,13 +82,6 @@ const baseChannel: Partial = { get isGuildTextBasedChannel() { return [DiscordChannelTypes.GuildNews, DiscordChannelTypes.GuildText].includes(this.type!); }, - get isThreadChannel() { - return [ - DiscordChannelTypes.GuildNewsThread, - DiscordChannelTypes.GuildPrivateThread, - DiscordChannelTypes.GuildPublicThread, - ].includes(this.type!); - }, send(content) { return sendMessage(this.id!, content); }, @@ -202,8 +195,6 @@ export interface DiscordenoChannel isNewsChannel: boolean; /** Whether the channel is a news or text channel in a guild. */ isGuildTextBasedChannel: boolean; - /** Whether the channel is a thread type channel. */ - isThreadChannel: boolean; // METHODS