From 56aaef3d2fc6a4463864ce20add2146f4976604f Mon Sep 17 00:00:00 2001 From: ITOH Date: Wed, 9 Dec 2020 21:09:21 +0100 Subject: [PATCH] Replace bit perm calculating with function --- src/handlers/channel.ts | 25 ++++++++----------------- src/handlers/guild.ts | 23 ++++++++--------------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/handlers/channel.ts b/src/handlers/channel.ts index 41b16b036..937e63d27 100644 --- a/src/handlers/channel.ts +++ b/src/handlers/channel.ts @@ -21,7 +21,10 @@ import { } from "../types/types.ts"; import { cache } from "../utils/cache.ts"; import { endpoints } from "../utils/constants.ts"; -import { botHasChannelPermissions } from "../utils/permissions.ts"; +import { + botHasChannelPermissions, + calculateBits, +} from "../utils/permissions.ts"; /** Checks if a channel overwrite for a user id or a role id has permission in this channel */ export function channelOverwriteHasPermission( @@ -402,14 +405,8 @@ export async function editChannel( (overwrite) => { return { ...overwrite, - allow: overwrite.allow.reduce( - (bits, perm) => bits |= BigInt(Permissions[perm]), - BigInt(0), - ).toString(), - deny: overwrite.deny.reduce( - (bits, perm) => bits |= BigInt(Permissions[perm]), - BigInt(0), - ).toString(), + allow: calculateBits(overwrite.allow), + deny: calculateBits(overwrite.deny), }; }, ), @@ -452,14 +449,8 @@ export async function editChannelOverwrite( })), { ...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(), + allow: calculateBits(allow), + deny: calculateBits(deny), }, ], }; diff --git a/src/handlers/guild.ts b/src/handlers/guild.ts index 5da9d86bc..7392cc6f3 100644 --- a/src/handlers/guild.ts +++ b/src/handlers/guild.ts @@ -33,7 +33,6 @@ import { ImageSize, Intents, MemberCreatePayload, - Permissions, PositionSwap, PruneOptions, PrunePayload, @@ -131,14 +130,8 @@ export async function createGuildChannel( permission_overwrites: options?.permissionOverwrites?.map((perm) => ({ ...perm, - allow: perm.allow.reduce( - (bits, p) => bits |= BigInt(Permissions[p]), - BigInt(0), - ).toString(), - deny: perm.deny.reduce( - (bits, p) => bits |= BigInt(Permissions[p]), - BigInt(0), - ).toString(), + allow: calculateBits(perm.allow), + deny: calculateBits(perm.deny), })), type: options?.type || ChannelTypes.GUILD_TEXT, })) as ChannelCreatePayload; @@ -318,15 +311,13 @@ export async function createGuildRole( throw new Error(Errors.MISSING_MANAGE_ROLES); } + const bits = calculateBits(options.permissions || []); + const result = await RequestManager.post( endpoints.GUILD_ROLES(guildID), { ...options, - permissions: options.permissions - ?.reduce((subtotal, perm) => { - subtotal |= Permissions[perm]; - return subtotal; - }, 0), + permissions: calculateBits(options?.permissions || []), reason, }, ); @@ -448,7 +439,9 @@ export async function getAuditLogs( return RequestManager.get(endpoints.GUILD_AUDIT_LOGS(guildID), { ...options, - action_type: options.action_type ? AuditLogs[options.action_type] : undefined, + action_type: options.action_type + ? AuditLogs[options.action_type] + : undefined, limit: options.limit && options.limit >= 1 && options.limit <= 100 ? options.limit : 50,