Add nicer Way to edit Channel Permission

This commit is contained in:
ITOH
2020-12-09 16:23:45 +01:00
parent 6112b29129
commit af2a398318
+51
View File
@@ -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,