From 30d2ba3cf85d3f177b205161410cc4ab972dce45 Mon Sep 17 00:00:00 2001 From: ayntee Date: Mon, 16 Nov 2020 19:43:11 +0400 Subject: [PATCH] Resolve promises --- src/handlers/member.ts | 67 +++++++++++++++++++++++++++++----------- src/utils/permissions.ts | 3 +- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/handlers/member.ts b/src/handlers/member.ts index 6b4e59d34..ad3aa8931 100644 --- a/src/handlers/member.ts +++ b/src/handlers/member.ts @@ -54,14 +54,19 @@ export async function addRole( reason?: string, ) { const botsHighestRole = await highestRole(guildID, botID); - if ( - botsHighestRole && - !higherRolePosition(guildID, botsHighestRole.id, roleID) - ) { - throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW); + if (botsHighestRole) { + const hasHigherRolePosition = await higherRolePosition( + guildID, + botsHighestRole.id, + roleID, + ); + if (!hasHigherRolePosition) { + throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW); + } } - if (!botHasPermission(guildID, [Permissions.MANAGE_ROLES])) { + const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]); + if (!hasPerm) { throw new Error(Errors.MISSING_MANAGE_ROLES); } @@ -79,16 +84,23 @@ export async function removeRole( reason?: string, ) { const botsHighestRole = await highestRole(guildID, botID); - if ( - botsHighestRole && - !higherRolePosition(guildID, botsHighestRole.id, roleID) - ) { - throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW); + + if (botsHighestRole) { + const hasHigherRolePosition = await higherRolePosition( + guildID, + botsHighestRole.id, + roleID, + ); + if (!hasHigherRolePosition) { + throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW); + } } - if (!botHasPermission(guildID, [Permissions.MANAGE_ROLES])) { + const hasPerm = await botHasPermission(guildID, [Permissions.MANAGE_ROLES]); + if (!hasPerm) { throw new Error(Errors.MISSING_MANAGE_ROLES); } + return RequestManager.delete( endpoints.GUILD_MEMBER_ROLE(guildID, memberID, roleID), { reason }, @@ -130,9 +142,11 @@ export async function kick(guildID: string, memberID: string, reason?: string) { throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW); } - if (!botHasPermission(guildID, [Permissions.KICK_MEMBERS])) { + const hasPerm = await botHasPermission(guildID, [Permissions.KICK_MEMBERS]); + if (!hasPerm) { throw new Error(Errors.MISSING_KICK_MEMBERS); } + return RequestManager.delete( endpoints.GUILD_MEMBER(guildID, memberID), { reason }, @@ -140,7 +154,7 @@ export async function kick(guildID: string, memberID: string, reason?: string) { } /** Edit the member */ -export function editMember( +export async function editMember( guildID: string, memberID: string, options: EditMemberOptions, @@ -149,30 +163,47 @@ export function editMember( if (options.nick.length > 32) { throw new Error(Errors.NICKNAMES_MAX_LENGTH); } - if (!botHasPermission(guildID, [Permissions.MANAGE_NICKNAMES])) { + + const hasManageNickPerm = await botHasPermission( + guildID, + [Permissions.MANAGE_NICKNAMES], + ); + if (!hasManageNickPerm) { throw new Error(Errors.MISSING_MANAGE_NICKNAMES); } } + const hasManageRolesPerm = await botHasPermission( + guildID, + [Permissions.MANAGE_ROLES], + ); if ( options.roles && - !botHasPermission(guildID, [Permissions.MANAGE_ROLES]) + !hasManageRolesPerm ) { throw new Error(Errors.MISSING_MANAGE_ROLES); } if (options.mute) { + const hasMuteMembersPerm = await botHasPermission( + guildID, + [Permissions.MUTE_MEMBERS], + ); // TODO: This should check if the member is in a voice channel if ( - !botHasPermission(guildID, [Permissions.MUTE_MEMBERS]) + !hasMuteMembersPerm ) { throw new Error(Errors.MISSING_MUTE_MEMBERS); } } + const hasDeafenMembersPerm = await botHasPermission( + guildID, + [Permissions.DEAFEN_MEMBERS], + ); if ( options.deaf && - !botHasPermission(guildID, [Permissions.DEAFEN_MEMBERS]) + !hasDeafenMembersPerm ) { throw new Error(Errors.MISSING_DEAFEN_MEMBERS); } diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index 0c4583a34..b2519f0f7 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -175,7 +175,8 @@ export async function hasChannelPermissions( if (permissions.every((perm) => allowedPermissions.has(perm))) return true; // Some permission was not explicitly allowed so we default to checking role perms directly - return botHasPermission(guild.id, permissions); + const hasPerms = await botHasPermission(guild.id, permissions); + return hasPerms; } /** This function converts a bitwise string to permission strings */