Replace bit perm calculating with function

This commit is contained in:
ITOH
2020-12-09 21:09:21 +01:00
parent 28a92bdfdf
commit 56aaef3d2f
2 changed files with 16 additions and 32 deletions

View File

@@ -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),
},
],
};

View File

@@ -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,