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,
) {
const botsHighestRole = await highestRole(guildID, botID);
if (
botsHighestRole &&
!higherRolePosition(guildID, botsHighestRole.id, roleID)
) {
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)
) {
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);
}
+2 -1
View File
@@ -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 */