From 43907d09a4b13110bd8d02b2a81ffe4688cbb885 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Thu, 17 Jun 2021 16:52:47 +0000 Subject: [PATCH] i love rigor --- src/helpers/channels/threads/leave_thread.ts | 12 ++++++++ .../channels/threads/remove_from_thread.ts | 29 ------------------- .../channels/threads/remove_thread_member.ts | 21 ++++++++++++++ 3 files changed, 33 insertions(+), 29 deletions(-) create mode 100644 src/helpers/channels/threads/leave_thread.ts delete mode 100644 src/helpers/channels/threads/remove_from_thread.ts create mode 100644 src/helpers/channels/threads/remove_thread_member.ts diff --git a/src/helpers/channels/threads/leave_thread.ts b/src/helpers/channels/threads/leave_thread.ts new file mode 100644 index 000000000..546309fb6 --- /dev/null +++ b/src/helpers/channels/threads/leave_thread.ts @@ -0,0 +1,12 @@ +import { cacheHandlers } from "../../../cache.ts"; +import { rest } from "../../../rest/rest.ts"; +import { Errors } from "../../../types/discordeno/errors.ts"; +import { endpoints } from "../../../util/constants.ts"; + +/** Removes the bot from a thread. Requires the thread is not archived. */ +export async function leaveThread(threadId: bigint) { + const thread = await cacheHandlers.get("threads", threadId); + if (thread?.archived) throw new Error(Errors.CANNOT_LEAVE_ARCHIVED_THREAD); + + return await rest.runMethod("delete", endpoints.THREAD_ME(threadId)); +} diff --git a/src/helpers/channels/threads/remove_from_thread.ts b/src/helpers/channels/threads/remove_from_thread.ts deleted file mode 100644 index 021503a92..000000000 --- a/src/helpers/channels/threads/remove_from_thread.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { botId } from "../../../bot.ts"; -import { cacheHandlers } from "../../../cache.ts"; -import { rest } from "../../../rest/rest.ts"; -import { ChannelTypes } from "../../../types/channels/channel_types.ts"; -import { Errors } from "../../../types/discordeno/errors.ts"; -import { endpoints } from "../../../util/constants.ts"; -import { botHasChannelPermissions } from "../../../util/permissions.ts"; - -/** Removes another user from a thread. Requires the MANAGE_THREADS permission or that you are the creator of the thread. Also requires the thread is not archived. Returns a 204 empty response on success. Fires a Thread Members Update Gateway event. */ -export async function removeFromThread(channelId: bigint, userId?: bigint) { - const channel = await cacheHandlers.get("channels", channelId); - if (channel) { - if ( - ![ChannelTypes.GuildNewsThread, ChannelTypes.GuildPivateThread, ChannelTypes.GuildPublicThread].includes( - channel.type - ) - ) { - throw new Error(Errors.NOT_A_THREAD_CHANNEL); - } - - if (channel.ownerId !== botId && !(await botHasChannelPermissions(channel, ["MANAGE_THREADS"]))) - throw new Error(Errors.HAVE_TO_BE_THE_CREATOR_OF_THE_THREAD_OR_HAVE_MANAGE_THREADS_TO_REMOVE_MEMBERS); - } - - return await rest.runMethod( - "delete", - userId ? endpoints.THREAD_USER(channelId, userId) : endpoints.THREAD_ME(channelId) - ); -} diff --git a/src/helpers/channels/threads/remove_thread_member.ts b/src/helpers/channels/threads/remove_thread_member.ts new file mode 100644 index 000000000..c418d78ef --- /dev/null +++ b/src/helpers/channels/threads/remove_thread_member.ts @@ -0,0 +1,21 @@ +import { botId } from "../../../bot.ts"; +import { cacheHandlers } from "../../../cache.ts"; +import { rest } from "../../../rest/rest.ts"; +import { Errors } from "../../../types/discordeno/errors.ts"; +import { endpoints } from "../../../util/constants.ts"; +import { requireBotChannelPermissions } from "../../../util/permissions.ts"; + +/** Removes a user from a thread. Requires the MANAGE_THREADS permission or that you are the creator of the thread. Also requires the thread is not archived. */ +export async function removeThreadMember(threadId: bigint, userId: bigint) { + const thread = await cacheHandlers.get("threads", threadId); + if (thread) { + if (thread.archived) throw new Error(Errors.CANNOT_REMOVE_FROM_ARCHIVED_THREAD); + + if (thread.ownerId !== botId) { + const channel = await cacheHandlers.get("channels", thread.channelId); + if (channel) await requireBotChannelPermissions(channel, ["MANAGE_THREADS"]); + } + } + + return await rest.runMethod("delete", endpoints.THREAD_USER(threadId, userId)); +}