From af2a398318129c20711f234f98b774ac79caea22 Mon Sep 17 00:00:00 2001 From: ITOH Date: Wed, 9 Dec 2020 16:23:45 +0100 Subject: [PATCH] Add nicer Way to edit Channel Permission --- src/handlers/channel.ts | 51 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/handlers/channel.ts b/src/handlers/channel.ts index c3daa3ccd..7b930a76c 100644 --- a/src/handlers/channel.ts +++ b/src/handlers/channel.ts @@ -13,11 +13,13 @@ import { GetMessagesBefore, MessageContent, MessageCreateOptions, + Overwrite, Permission, Permissions, RawOverwrite, WebhookPayload, } from "../types/types.ts"; +import { cache } from "../utils/cache.ts"; import { endpoints } from "../utils/constants.ts"; import { botHasChannelPermissions } from "../utils/permissions.ts"; @@ -422,6 +424,55 @@ export async function editChannel( ); } +export async function editChannelOverwrite( + channelID: string, + overwrite: Overwrite, + reason?: string, +) { + const hasManageChannelPerm = await botHasChannelPermissions( + channelID, + ["MANAGE_CHANNELS"], + ); + if (!hasManageChannelPerm) { + throw new Error(Errors.MISSING_MANAGE_CHANNELS); + } + + const { allow, deny, ...info } = overwrite; + let channel = cache.channels.get(channelID); + + if (!channel) return Errors.CHANNEL_NOT_FOUND; + + const payload = { + permission_overwrites: [ + ...(channel?.permissionOverwrites || []).map((ro) => ({ + id: ro.id, + type: ro.type, + allow: ro.allow, + deny: ro.deny, + })), + { + ...info, + allow: allow.reduce( + (bits, perm) => bits |= BigInt(Permissions[perm]), + BigInt(0), + ).toString(), + deny: deny.reduce( + (bits, perm) => bits |= BigInt(Permissions[perm]), + BigInt(0), + ).toString(), + }, + ], + }; + + return RequestManager.patch( + endpoints.GUILD_CHANNEL(channelID), + { + ...payload, + reason, + }, + ); +} + /** Follow a News Channel to send messages to a target channel. Requires the `MANAGE_WEBHOOKS` permission in the target channel. Returns the webhook id. */ export async function followChannel( sourceChannelID: string,