Resolve promises

This commit is contained in:
ayntee
2020-11-16 19:43:11 +04:00
parent 9062bb1045
commit 30d2ba3cf8
2 changed files with 51 additions and 19 deletions
+47 -16
View File
@@ -54,14 +54,19 @@ export async function addRole(
reason?: string, reason?: string,
) { ) {
const botsHighestRole = await highestRole(guildID, botID); const botsHighestRole = await highestRole(guildID, botID);
if ( if (botsHighestRole) {
botsHighestRole && const hasHigherRolePosition = await higherRolePosition(
!higherRolePosition(guildID, botsHighestRole.id, roleID) guildID,
) { botsHighestRole.id,
roleID,
);
if (!hasHigherRolePosition) {
throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW); 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); throw new Error(Errors.MISSING_MANAGE_ROLES);
} }
@@ -79,16 +84,23 @@ export async function removeRole(
reason?: string, reason?: string,
) { ) {
const botsHighestRole = await highestRole(guildID, botID); const botsHighestRole = await highestRole(guildID, botID);
if (
botsHighestRole && if (botsHighestRole) {
!higherRolePosition(guildID, botsHighestRole.id, roleID) const hasHigherRolePosition = await higherRolePosition(
) { guildID,
botsHighestRole.id,
roleID,
);
if (!hasHigherRolePosition) {
throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW); 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); throw new Error(Errors.MISSING_MANAGE_ROLES);
} }
return RequestManager.delete( return RequestManager.delete(
endpoints.GUILD_MEMBER_ROLE(guildID, memberID, roleID), endpoints.GUILD_MEMBER_ROLE(guildID, memberID, roleID),
{ reason }, { reason },
@@ -130,9 +142,11 @@ export async function kick(guildID: string, memberID: string, reason?: string) {
throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW); 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); throw new Error(Errors.MISSING_KICK_MEMBERS);
} }
return RequestManager.delete( return RequestManager.delete(
endpoints.GUILD_MEMBER(guildID, memberID), endpoints.GUILD_MEMBER(guildID, memberID),
{ reason }, { reason },
@@ -140,7 +154,7 @@ export async function kick(guildID: string, memberID: string, reason?: string) {
} }
/** Edit the member */ /** Edit the member */
export function editMember( export async function editMember(
guildID: string, guildID: string,
memberID: string, memberID: string,
options: EditMemberOptions, options: EditMemberOptions,
@@ -149,30 +163,47 @@ export function editMember(
if (options.nick.length > 32) { if (options.nick.length > 32) {
throw new Error(Errors.NICKNAMES_MAX_LENGTH); 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); throw new Error(Errors.MISSING_MANAGE_NICKNAMES);
} }
} }
const hasManageRolesPerm = await botHasPermission(
guildID,
[Permissions.MANAGE_ROLES],
);
if ( if (
options.roles && options.roles &&
!botHasPermission(guildID, [Permissions.MANAGE_ROLES]) !hasManageRolesPerm
) { ) {
throw new Error(Errors.MISSING_MANAGE_ROLES); throw new Error(Errors.MISSING_MANAGE_ROLES);
} }
if (options.mute) { if (options.mute) {
const hasMuteMembersPerm = await botHasPermission(
guildID,
[Permissions.MUTE_MEMBERS],
);
// TODO: This should check if the member is in a voice channel // TODO: This should check if the member is in a voice channel
if ( if (
!botHasPermission(guildID, [Permissions.MUTE_MEMBERS]) !hasMuteMembersPerm
) { ) {
throw new Error(Errors.MISSING_MUTE_MEMBERS); throw new Error(Errors.MISSING_MUTE_MEMBERS);
} }
} }
const hasDeafenMembersPerm = await botHasPermission(
guildID,
[Permissions.DEAFEN_MEMBERS],
);
if ( if (
options.deaf && options.deaf &&
!botHasPermission(guildID, [Permissions.DEAFEN_MEMBERS]) !hasDeafenMembersPerm
) { ) {
throw new Error(Errors.MISSING_DEAFEN_MEMBERS); throw new Error(Errors.MISSING_DEAFEN_MEMBERS);
} }
+2 -1
View File
@@ -175,7 +175,8 @@ export async function hasChannelPermissions(
if (permissions.every((perm) => allowedPermissions.has(perm))) return true; if (permissions.every((perm) => allowedPermissions.has(perm))) return true;
// Some permission was not explicitly allowed so we default to checking role perms directly // 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 */ /** This function converts a bitwise string to permission strings */