From 2dbf3dfd8b6ae07c69cbd1c4774aad22a2243b7b Mon Sep 17 00:00:00 2001 From: Just a Badly Drawn TABS Dude <45307955+Suyashtnt@users.noreply.github.com> Date: Sun, 11 Oct 2020 12:46:04 +0200 Subject: [PATCH 1/8] Update discord.ts very minor --- src/constants/discord.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constants/discord.ts b/src/constants/discord.ts index fe9a98da6..0c62f6030 100644 --- a/src/constants/discord.ts +++ b/src/constants/discord.ts @@ -1,7 +1,7 @@ let API_VERSION = "v8"; export const baseEndpoints = { - /** Although, the version can be defaulted, keep the v6 as it can be changed to test newer versions when necessary. */ + /** Although, the version can be defaulted, keep the v8 as it can be changed to test newer versions when necessary. */ BASE_URL: `https://discord.com/api/${API_VERSION}`, CDN_URL: "https://cdn.discordapp.com", }; From 40059918bc7e131e44cdc2174ee9cb9062921818 Mon Sep 17 00:00:00 2001 From: Just a Badly Drawn TABS Dude <45307955+Suyashtnt@users.noreply.github.com> Date: Tue, 13 Oct 2020 20:37:52 +0200 Subject: [PATCH 2/8] Update discord.ts --- src/constants/discord.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/constants/discord.ts b/src/constants/discord.ts index 0c62f6030..3d2b1f881 100644 --- a/src/constants/discord.ts +++ b/src/constants/discord.ts @@ -1,15 +1,8 @@ -let API_VERSION = "v8"; - export const baseEndpoints = { - /** Although, the version can be defaulted, keep the v8 as it can be changed to test newer versions when necessary. */ - BASE_URL: `https://discord.com/api/${API_VERSION}`, + BASE_URL: `https://discord.com/api/V8`, CDN_URL: "https://cdn.discordapp.com", }; -export function changeAPIVersion(number = 7) { - API_VERSION = `v${number}`; -} - const GUILDS_BASE = (id: string) => `${baseEndpoints.BASE_URL}/guilds/${id}`; export const endpoints = { From e2afa7d6d61220a3168e1c16dfae48c852848d89 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Tue, 13 Oct 2020 14:53:28 -0400 Subject: [PATCH 3/8] Update src/constants/discord.ts --- src/constants/discord.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constants/discord.ts b/src/constants/discord.ts index 3d2b1f881..b4559f6ea 100644 --- a/src/constants/discord.ts +++ b/src/constants/discord.ts @@ -1,5 +1,5 @@ export const baseEndpoints = { - BASE_URL: `https://discord.com/api/V8`, + BASE_URL: `https://discord.com/api/v8`, CDN_URL: "https://cdn.discordapp.com", }; From 7566f64c94155b3a3edf5e0f059f1aae6353eb78 Mon Sep 17 00:00:00 2001 From: Just a Badly Drawn TABS Dude <45307955+Suyashtnt@users.noreply.github.com> Date: Wed, 14 Oct 2020 19:10:01 +0200 Subject: [PATCH 4/8] convert to qoutes we dont need a template string --- src/constants/discord.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constants/discord.ts b/src/constants/discord.ts index b4559f6ea..896aedb76 100644 --- a/src/constants/discord.ts +++ b/src/constants/discord.ts @@ -1,5 +1,5 @@ export const baseEndpoints = { - BASE_URL: `https://discord.com/api/v8`, + BASE_URL: "https://discord.com/api/v8", CDN_URL: "https://cdn.discordapp.com", }; From 47ede28f5d2f4705a4183ec9ed5583acf47191a3 Mon Sep 17 00:00:00 2001 From: ayyanm Date: Thu, 29 Oct 2020 10:38:40 -0700 Subject: [PATCH 5/8] Rename permission_overwrites to permissionOverwrites --- src/handlers/guild.ts | 2 +- src/structures/channel.ts | 2 +- src/types/guild.ts | 2 +- src/utils/permissions.ts | 2 +- tests/mod.test.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/handlers/guild.ts b/src/handlers/guild.ts index 385b2c017..f80780993 100644 --- a/src/handlers/guild.ts +++ b/src/handlers/guild.ts @@ -105,7 +105,7 @@ export async function createGuildChannel( (await RequestManager.post(endpoints.GUILD_CHANNELS(guild.id), { ...options, name, - permission_overwrites: options?.permission_overwrites?.map((perm) => ({ + permission_overwrites: options?.permissionOverwrites?.map((perm) => ({ ...perm, allow: perm.allow.reduce( diff --git a/src/structures/channel.ts b/src/structures/channel.ts index acc29ebb4..cdd6d347b 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -32,7 +32,7 @@ export async function createChannel( /** The last time when a message was pinned in this channel */ lastPinTimestamp, /** The permission overwrites for this channel */ - permissions: data.permission_overwrites + permissionOverwrites: data.permission_overwrites ? data.permission_overwrites.map((perm) => ({ ...perm, allow: calculatePermissions(BigInt(perm.allow)), diff --git a/src/types/guild.ts b/src/types/guild.ts index 7bcd4991e..590428f64 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -487,7 +487,7 @@ export interface ChannelCreateOptions { /** The sorting position of the channel */ position?: number; /** The channel's permission overwrites */ - permission_overwrites?: Overwrite[]; + permissionOverwrites?: Overwrite[]; /** The id of the parent category for the channel */ parent_id?: string; /** Whether the channel is nsfw */ diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index 0c4583a34..1902e3387 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -108,7 +108,7 @@ export async function hasChannelPermissions( let everyoneOverwrite: RawOverwrite | undefined; let rolesOverwrites: RawOverwrite[] = []; - for (const overwrite of channel.permission_overwrites || []) { + for (const overwrite of channel.permissionOverwrites || []) { // If the overwrite on this channel is specific to this member if (overwrite.id === memberID) memberOverwrite = overwrite; // If it is the everyone role overwrite diff --git a/tests/mod.test.ts b/tests/mod.test.ts index bfa3169e7..bd966602e 100644 --- a/tests/mod.test.ts +++ b/tests/mod.test.ts @@ -156,7 +156,7 @@ Deno.test({ async fn() { const channel = cache.channels.get(data.channelID); if (!channel) throw "Channel not found"; - assertArrayIncludes(channel.permission_overwrites!, [ + assertArrayIncludes(channel.permissionOverwrites!, [ { id: data.roleID, type: OverwriteType.ROLE, From b1903b1d8cc957fc6298db0d728b5d0a00cdac37 Mon Sep 17 00:00:00 2001 From: ayyanm Date: Thu, 29 Oct 2020 10:50:52 -0700 Subject: [PATCH 6/8] Add _rawPermissionOverwrites property to Channel --- src/structures/channel.ts | 3 +++ src/utils/permissions.ts | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/structures/channel.ts b/src/structures/channel.ts index cdd6d347b..0405dd03b 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -14,6 +14,7 @@ export async function createChannel( rate_limit_per_user: rateLimitPerUser, parent_id: parentID, last_pin_timestamp: lastPinTimestamp, + permission_overwrites, ...rest } = data; @@ -43,6 +44,8 @@ export async function createChannel( nsfw: data.nsfw || false, /** The mention of the channel */ mention: `<#${data.id}>`, + /** Raw permissions */ + _rawPermissionOverwrites: permission_overwrites, }; cacheHandlers.set("channels", data.id, channel); diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index 1902e3387..0a3a39543 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -108,7 +108,7 @@ export async function hasChannelPermissions( let everyoneOverwrite: RawOverwrite | undefined; let rolesOverwrites: RawOverwrite[] = []; - for (const overwrite of channel.permissionOverwrites || []) { + for (const overwrite of channel._rawPermissionOverwrites || []) { // If the overwrite on this channel is specific to this member if (overwrite.id === memberID) memberOverwrite = overwrite; // If it is the everyone role overwrite From 4e39f327a51ffb315e0da31c7cf61b131fa267f9 Mon Sep 17 00:00:00 2001 From: ayyanm Date: Thu, 29 Oct 2020 12:19:57 -0700 Subject: [PATCH 7/8] New PermissionOverwrite interface --- src/structures/channel.ts | 16 +++++++++------- src/types/guild.ts | 6 ++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/structures/channel.ts b/src/structures/channel.ts index 0405dd03b..fe8848cc8 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -1,5 +1,6 @@ import { cacheHandlers } from "../controllers/cache.ts"; import { ChannelCreatePayload } from "../types/channel.ts"; +import { PermissionOverwrite } from "../types/guild.ts"; import { Unpromise } from "../types/misc.ts"; import { calculatePermissions } from "../utils/permissions.ts"; @@ -33,13 +34,14 @@ export async function createChannel( /** The last time when a message was pinned in this channel */ lastPinTimestamp, /** The permission overwrites for this channel */ - permissionOverwrites: data.permission_overwrites - ? data.permission_overwrites.map((perm) => ({ - ...perm, - allow: calculatePermissions(BigInt(perm.allow)), - deny: calculatePermissions(BigInt(perm.deny)), - })) - : [], + permissionOverwrites: + (data.permission_overwrites + ? data.permission_overwrites.map((perm) => ({ + ...perm, + allow: calculatePermissions(BigInt(perm.allow)), + deny: calculatePermissions(BigInt(perm.deny)), + })) + : []) as PermissionOverwrite[], /** Whether this channel is nsfw or not */ nsfw: data.nsfw || false, /** The mention of the channel */ diff --git a/src/types/guild.ts b/src/types/guild.ts index 590428f64..ba52fedfd 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -473,6 +473,12 @@ export interface RawOverwrite { deny: number; } +export interface PermissionOverwrite + extends Omit { + allow: Permission[]; + deny: Permission[]; +} + export interface ChannelCreateOptions { /** The type of the channel */ type?: ChannelTypes; From 3060bb5b33c2cff5bb5ea8fea69b94d7f3dd7767 Mon Sep 17 00:00:00 2001 From: ayyanm Date: Thu, 29 Oct 2020 12:36:19 -0700 Subject: [PATCH 8/8] Remove Channel#_rawPermissionOverwrites --- src/structures/channel.ts | 2 -- src/utils/permissions.ts | 31 +++++++++++++++++++------------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/structures/channel.ts b/src/structures/channel.ts index fe8848cc8..b36662d2b 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -46,8 +46,6 @@ export async function createChannel( nsfw: data.nsfw || false, /** The mention of the channel */ mention: `<#${data.id}>`, - /** Raw permissions */ - _rawPermissionOverwrites: permission_overwrites, }; cacheHandlers.set("channels", data.id, channel); diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index 0a3a39543..2537f3cf2 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -2,7 +2,7 @@ import { cacheHandlers } from "../controllers/cache.ts"; import { botID } from "../module/client.ts"; import { Guild } from "../structures/guild.ts"; import { Role } from "../structures/role.ts"; -import { RawOverwrite } from "../types/guild.ts"; +import { PermissionOverwrite } from "../types/guild.ts"; import { Permission, Permissions } from "../types/permission.ts"; /** Checks if the member has this permission. If the member is an owner or has admin perms it will always be true. */ @@ -104,11 +104,11 @@ export async function hasChannelPermissions( const member = guild.members.get(memberID); if (!member) return false; - let memberOverwrite: RawOverwrite | undefined; - let everyoneOverwrite: RawOverwrite | undefined; - let rolesOverwrites: RawOverwrite[] = []; + let memberOverwrite: PermissionOverwrite | undefined; + let everyoneOverwrite: PermissionOverwrite | undefined; + let rolesOverwrites: PermissionOverwrite[] = []; - for (const overwrite of channel._rawPermissionOverwrites || []) { + for (const overwrite of channel.permissionOverwrites || []) { // If the overwrite on this channel is specific to this member if (overwrite.id === memberID) memberOverwrite = overwrite; // If it is the everyone role overwrite @@ -121,13 +121,16 @@ export async function hasChannelPermissions( // Member perms override everything so we must check them first if (memberOverwrite) { + const allowBits = calculateBits(memberOverwrite.allow); + const denyBits = calculateBits(memberOverwrite.deny); for (const perm of permissions) { // One of the necessary permissions is denied. Since this is main permission we can cancel if its denied. - if (BigInt(memberOverwrite.deny) & BigInt(perm)) return false; + if (BigInt(denyBits) & BigInt(perm)) return false; // Already allowed perm if (allowedPermissions.has(perm)) continue; + // This perm is allowed so we save it - if (BigInt(memberOverwrite.allow) & BigInt(perm)) { + if (BigInt(allowBits) & BigInt(perm)) { allowedPermissions.add(perm); } } @@ -139,17 +142,19 @@ export async function hasChannelPermissions( if (allowedPermissions.has(perm)) continue; for (const overwrite of rolesOverwrites) { + const allowBits = calculateBits(overwrite.allow); // This perm is allowed so we save it - if (BigInt(overwrite.allow) & BigInt(perm)) { + if (BigInt(allowBits) & BigInt(perm)) { allowedPermissions.add(perm); break; } + const denyBits = calculateBits(overwrite.deny); // If this role denies it we need to save and check if another role allows it, allows > deny - if (BigInt(overwrite.deny) & BigInt(perm)) { + if (BigInt(denyBits) & BigInt(perm)) { // This role denies his perm, but before denying we need to check all other roles if any allow as allow > deny const isAllowed = rolesOverwrites.some((o) => - BigInt(o.allow) & BigInt(perm) + BigInt(calculateBits(o.allow)) & BigInt(perm) ); if (isAllowed) continue; // This permission is in fact denied. Since Roles overrule everything below here we can cancel ou here @@ -159,13 +164,15 @@ export async function hasChannelPermissions( } if (everyoneOverwrite) { + const allowBits = calculateBits(everyoneOverwrite.allow); + const denyBits = calculateBits(everyoneOverwrite.deny); for (const perm of permissions) { // Already allowed perm if (allowedPermissions.has(perm)) continue; // One of the necessary permissions is denied. Since everyone overwrite overrides role perms we can cancel here - if (BigInt(everyoneOverwrite.deny) & BigInt(perm)) return false; + if (BigInt(denyBits) & BigInt(perm)) return false; // This perm is allowed so we save it - if (BigInt(everyoneOverwrite.allow) & BigInt(perm)) { + if (BigInt(allowBits) & BigInt(perm)) { allowedPermissions.add(perm); } }