From 5da27d8244d1c4670c98586f132e3d26c4e4c408 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Sun, 31 Oct 2021 19:16:56 +0000 Subject: [PATCH] Closes #1119 .allow & deny optional --- src/helpers/channels/create_channel.ts | 4 +- src/helpers/channels/edit_channel.ts | 4 +- .../channels/edit_channel_overwrite.ts | 4 +- src/types/channels/overwrite.ts | 4 +- src/util/permissions.ts | 48 +++++++++++++++---- 5 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/helpers/channels/create_channel.ts b/src/helpers/channels/create_channel.ts index 48ac3b3d8..2b13cd6ac 100644 --- a/src/helpers/channels/create_channel.ts +++ b/src/helpers/channels/create_channel.ts @@ -29,8 +29,8 @@ export async function createChannel(bot: Bot, guildId: bigint, options?: CreateG permission_overwrites: options?.permissionOverwrites?.map((perm) => ({ id: perm.id.toString(), type: perm.type, - allow: bot.utils.calculateBits(perm.allow), - deny: bot.utils.calculateBits(perm.deny), + allow: perm.allow ? bot.utils.calculateBits(perm.allow) : "0", + deny: perm.deny ? bot.utils.calculateBits(perm.deny) : "0", })), type: options?.type || DiscordChannelTypes.GuildText, reason, diff --git a/src/helpers/channels/edit_channel.ts b/src/helpers/channels/edit_channel.ts index abeab8115..fa4d15f7b 100644 --- a/src/helpers/channels/edit_channel.ts +++ b/src/helpers/channels/edit_channel.ts @@ -53,8 +53,8 @@ export async function editChannel(bot: Bot, channelId: bigint, options: ModifyCh ? options.permissionOverwrites?.map((overwrite) => { return { ...overwrite, - allow: bot.utils.calculateBits(overwrite.allow), - deny: bot.utils.calculateBits(overwrite.deny), + allow: overwrite.allow ? bot.utils.calculateBits(overwrite.allow) : "0", + deny: overwrite.deny ? bot.utils.calculateBits(overwrite.deny) : "0", }; }) : undefined, diff --git a/src/helpers/channels/edit_channel_overwrite.ts b/src/helpers/channels/edit_channel_overwrite.ts index da1d489b0..686d1f170 100644 --- a/src/helpers/channels/edit_channel_overwrite.ts +++ b/src/helpers/channels/edit_channel_overwrite.ts @@ -16,8 +16,8 @@ export async function editChannelOverwrite( "put", bot.constants.endpoints.CHANNEL_OVERWRITE(channelId, overwriteId), { - allow: bot.utils.calculateBits(options.allow), - deny: bot.utils.calculateBits(options.deny), + allow: options.allow ? bot.utils.calculateBits(options.allow) : "0", + deny: options.deny ? bot.utils.calculateBits(options.deny) : "0", type: options.type, } ); diff --git a/src/types/channels/overwrite.ts b/src/types/channels/overwrite.ts index 9c498c385..a70cbf957 100644 --- a/src/types/channels/overwrite.ts +++ b/src/types/channels/overwrite.ts @@ -7,9 +7,9 @@ export interface Overwrite { /** Either 0 (role) or 1 (member) */ type: DiscordOverwriteTypes; /** Permission bit set */ - allow: PermissionStrings[]; + allow?: PermissionStrings[]; /** Permission bit set */ - deny: PermissionStrings[]; + deny?: PermissionStrings[]; } /** https://discord.com/developers/docs/resources/channel#overwrite-object */ diff --git a/src/util/permissions.ts b/src/util/permissions.ts index 757081752..ec7f9c59d 100644 --- a/src/util/permissions.ts +++ b/src/util/permissions.ts @@ -159,7 +159,11 @@ export async function hasChannelPermissions( } /** Checks if the bot has these permissions f0r the given channel */ -export function botHasChannelPermissions(bot: Bot, channel: bigint | DiscordenoChannel, permissions: PermissionStrings[]) { +export function botHasChannelPermissions( + bot: Bot, + channel: bigint | DiscordenoChannel, + permissions: PermissionStrings[] +) { // Since Bot is a normal member we can use the hasRolePermissions() function return bot.utils.hasChannelPermissions(bot, channel, bot.id, permissions); } @@ -212,7 +216,11 @@ export async function requireGuildPermissions( } /** Throws an error if the bot does not have all permissions */ -export function requireBotGuildPermissions(bot: Bot, guild: bigint | DiscordenoGuild, permissions: PermissionStrings[]) { +export function requireBotGuildPermissions( + bot: Bot, + guild: bigint | DiscordenoGuild, + permissions: PermissionStrings[] +) { // Since Bot is a normal member we can use the throwOnMissingGuildPermission() function return bot.utils.requireGuildPermissions(bot, guild, bot.id, permissions); } @@ -232,7 +240,11 @@ export async function requireChannelPermissions( } /** Throws an error if the bot has not all of the given channel permissions */ -export function requireBotChannelPermissions(bot: Bot, channel: bigint | DiscordenoChannel, permissions: PermissionStrings[]) { +export function requireBotChannelPermissions( + bot: Bot, + channel: bigint | DiscordenoChannel, + permissions: PermissionStrings[] +) { // Since Bot is a normal member we can use the throwOnMissingChannelPermission() function return bot.utils.requireChannelPermissions(bot, channel, bot.id, permissions); } @@ -258,12 +270,16 @@ export function calculateBits(permissions: PermissionStrings[]) { } /** Internal function to check if the bot has the permissions to set these overwrites */ -export async function requireOverwritePermissions(bot: Bot, guildOrId: bigint | DiscordenoGuild, overwrites: Overwrite[]) { +export async function requireOverwritePermissions( + bot: Bot, + guildOrId: bigint | DiscordenoGuild, + overwrites: Overwrite[] +) { let requiredPerms: Set = new Set(["MANAGE_CHANNELS"]); overwrites?.forEach((overwrite) => { - overwrite.allow.forEach(requiredPerms.add, requiredPerms); - overwrite.deny.forEach(requiredPerms.add, requiredPerms); + if (overwrite.allow) overwrite.allow.forEach(requiredPerms.add, requiredPerms); + if (overwrite.deny) overwrite.deny.forEach(requiredPerms.add, requiredPerms); }); // MANAGE_ROLES permission can only be set by administrators @@ -275,7 +291,11 @@ export async function requireOverwritePermissions(bot: Bot, guildOrId: bigint | } /** Gets the highest role from the member in this guild */ -export async function highestRole(bot: Bot, guildOrId: bigint | DiscordenoGuild, memberOrId: bigint | DiscordenoMember) { +export async function highestRole( + bot: Bot, + guildOrId: bigint | DiscordenoGuild, + memberOrId: bigint | DiscordenoMember +) { const guild = await bot.utils.getCached(bot, "guilds", guildOrId); if (!guild) throw new Error(Errors.GUILD_NOT_FOUND); @@ -308,7 +328,12 @@ export async function highestRole(bot: Bot, guildOrId: bigint | DiscordenoGuild, } /** Checks if the first role is higher than the second role */ -export async function higherRolePosition(bot: Bot, guildOrId: bigint | DiscordenoGuild, roleId: bigint, otherRoleId: bigint) { +export async function higherRolePosition( + bot: Bot, + guildOrId: bigint | DiscordenoGuild, + roleId: bigint, + otherRoleId: bigint +) { const guild = await bot.utils.getCached(bot, "guilds", guildOrId); if (!guild) return true; @@ -326,7 +351,12 @@ export async function higherRolePosition(bot: Bot, guildOrId: bigint | Discorden } /** Checks if the member has a higher position than the given role */ -export async function isHigherPosition(bot: Bot, guildOrId: bigint | DiscordenoGuild, memberId: bigint, compareRoleId: bigint) { +export async function isHigherPosition( + bot: Bot, + guildOrId: bigint | DiscordenoGuild, + memberId: bigint, + compareRoleId: bigint +) { const guild = await bot.utils.getCached(bot, "guilds", guildOrId); if (!guild || guild.ownerId === memberId) return true;