i love rigor

This commit is contained in:
Skillz4Killz
2021-06-17 16:52:47 +00:00
committed by GitHub
parent 3a060aaa28
commit 43907d09a4
3 changed files with 33 additions and 29 deletions

View File

@@ -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));
}

View File

@@ -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)
);
}

View File

@@ -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));
}