mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 19:28:17 +00:00
fix: add to thread perm checks
This commit is contained in:
@@ -1,32 +1,25 @@
|
||||
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";
|
||||
import { requireBotChannelPermissions } from "../../../util/permissions.ts";
|
||||
|
||||
/** Adds the current user to a thread. Returns a 204 empty response on success. Also requires the thread is not archived. Fires a Thread Members Update Gateway event.
|
||||
* Adds another user to a thread. Requires the ability to send messages in the thread. Also requires the thread is not archived. Returns a 204 empty response on success. Fires a Thread Members Update Gateway event.
|
||||
* @param userId the user to add to the thread defaults to bot
|
||||
/** Adds the bot to a thread. When a user id is provided, it adds that user to the thread. User id requires the ability to send messages in the thread. Both requires the thread is not archived.
|
||||
*/
|
||||
export async function addToThread(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);
|
||||
export async function addToThread(threadId: bigint, userId?: bigint) {
|
||||
const thread = await cacheHandlers.get("threads", threadId);
|
||||
if (thread) {
|
||||
if (thread.archived) {
|
||||
throw new Error(Errors.CANNOT_ADD_USER_TO_ARCHIVED_THREADS);
|
||||
}
|
||||
|
||||
if (!(await botHasChannelPermissions(channel, ["MANAGE_THREADS"])) && !channel.member) {
|
||||
throw new Error(Errors.HAVE_TO_BE_PART_OF_THE_THREAD_OR_A_THREAD_MODERATOR_TO_ADD_OTHERS_TO_A_THREAD);
|
||||
// If a user id is provided SEND_MESSAGES is required.
|
||||
if (userId) {
|
||||
const channel = await cacheHandlers.get("channels", thread.channelId);
|
||||
// TODO: does MANAGE_THREADS override this????
|
||||
if (channel) await requireBotChannelPermissions(channel, ["SEND_MESSAGES"]);
|
||||
}
|
||||
}
|
||||
|
||||
return await rest.runMethod(
|
||||
"put",
|
||||
userId ? endpoints.THREAD_USER(channelId, userId) : endpoints.THREAD_ME(channelId)
|
||||
);
|
||||
return await rest.runMethod("put", userId ? endpoints.THREAD_USER(threadId, userId) : endpoints.THREAD_ME(threadId));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user