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 { cacheHandlers } from "../../../cache.ts";
|
||||||
import { rest } from "../../../rest/rest.ts";
|
import { rest } from "../../../rest/rest.ts";
|
||||||
import { ChannelTypes } from "../../../types/channels/channel_types.ts";
|
|
||||||
import { Errors } from "../../../types/discordeno/errors.ts";
|
import { Errors } from "../../../types/discordeno/errors.ts";
|
||||||
import { endpoints } from "../../../util/constants.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 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.
|
||||||
* 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
|
|
||||||
*/
|
*/
|
||||||
export async function addToThread(channelId: bigint, userId?: bigint) {
|
export async function addToThread(threadId: bigint, userId?: bigint) {
|
||||||
const channel = await cacheHandlers.get("channels", channelId);
|
const thread = await cacheHandlers.get("threads", threadId);
|
||||||
if (channel) {
|
if (thread) {
|
||||||
if (
|
if (thread.archived) {
|
||||||
![ChannelTypes.GuildNewsThread, ChannelTypes.GuildPivateThread, ChannelTypes.GuildPublicThread].includes(
|
throw new Error(Errors.CANNOT_ADD_USER_TO_ARCHIVED_THREADS);
|
||||||
channel.type
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
throw new Error(Errors.NOT_A_THREAD_CHANNEL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(await botHasChannelPermissions(channel, ["MANAGE_THREADS"])) && !channel.member) {
|
// If a user id is provided SEND_MESSAGES is required.
|
||||||
throw new Error(Errors.HAVE_TO_BE_PART_OF_THE_THREAD_OR_A_THREAD_MODERATOR_TO_ADD_OTHERS_TO_A_THREAD);
|
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(
|
return await rest.runMethod("put", userId ? endpoints.THREAD_USER(threadId, userId) : endpoints.THREAD_ME(threadId));
|
||||||
"put",
|
|
||||||
userId ? endpoints.THREAD_USER(channelId, userId) : endpoints.THREAD_ME(channelId)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user