diff --git a/TeamOctolings.Octobot/Services/AccessControlService.cs b/TeamOctolings.Octobot/Services/AccessControlService.cs index b5b98ea..d39c9e5 100644 --- a/TeamOctolings.Octobot/Services/AccessControlService.cs +++ b/TeamOctolings.Octobot/Services/AccessControlService.cs @@ -20,18 +20,17 @@ public sealed class AccessControlService _userApi = userApi; } - private static bool CheckPermission(IEnumerable roles, GuildData data, Snowflake memberId, - IGuildMember member, + private static bool CheckPermission(IEnumerable roles, GuildData data, MemberData memberData, DiscordPermission permission) { var moderatorRole = GuildSettings.ModeratorRole.Get(data.Settings); - if (!moderatorRole.Empty() && data.GetOrCreateMemberData(memberId).Roles.Contains(moderatorRole.Value)) + if (!moderatorRole.Empty() && memberData.Roles.Contains(moderatorRole.Value)) { return true; } return roles - .Where(r => member.Roles.Contains(r.ID)) + .Where(r => memberData.Roles.Contains(r.ID.Value)) .Any(r => r.Permissions.HasPermission(permission) ); @@ -80,38 +79,23 @@ public sealed class AccessControlService return Result.FromError(botResult); } - var botMemberResult = await _guildApi.GetGuildMemberAsync(guildId, bot.ID, ct); - if (!botMemberResult.IsDefined(out var botMember)) - { - return Result.FromError(botMemberResult); - } - - var targetMemberResult = await _guildApi.GetGuildMemberAsync(guildId, targetId, ct); - if (!targetMemberResult.IsDefined(out var targetMember)) - { - return Result.FromSuccess(null); - } - var rolesResult = await _guildApi.GetGuildRolesAsync(guildId, ct); if (!rolesResult.IsDefined(out var roles)) { return Result.FromError(rolesResult); } + var data = await _data.GetData(guildId, ct); + var targetData = data.GetOrCreateMemberData(targetId); + var botData = data.GetOrCreateMemberData(bot.ID); + if (interacterId is null) { - return CheckInteractions(action, guild, roles, targetMember, botMember, botMember); + return CheckInteractions(action, guild, roles, targetData, botData, botData); } - var interacterResult = await _guildApi.GetGuildMemberAsync(guildId, interacterId.Value, ct); - if (!interacterResult.IsDefined(out var interacter)) - { - return Result.FromError(interacterResult); - } - - var data = await _data.GetData(guildId, ct); - - var hasPermission = CheckPermission(roles, data, interacterId.Value, interacter, + var interacterData = data.GetOrCreateMemberData(interacterId.Value); + var hasPermission = CheckPermission(roles, data, interacterData, action switch { "Ban" => DiscordPermission.BanMembers, @@ -121,31 +105,26 @@ public sealed class AccessControlService }); return hasPermission - ? CheckInteractions(action, guild, roles, targetMember, botMember, interacter) + ? CheckInteractions(action, guild, roles, targetData, botData, interacterData) : Result.FromSuccess($"UserCannot{action}Members".Localized()); } private static Result CheckInteractions( - string action, IGuild guild, IReadOnlyList roles, IGuildMember targetMember, IGuildMember botMember, - IGuildMember interacter) + string action, IGuild guild, IReadOnlyList roles, MemberData targetData, MemberData botData, + MemberData interacterData) { - if (!targetMember.User.IsDefined(out var targetUser)) - { - return new ArgumentNullError(nameof(targetMember.User)); - } - - if (botMember.User == targetMember.User) + if (botData.Id == targetData.Id) { return Result.FromSuccess($"UserCannot{action}Bot".Localized()); } - if (targetUser.ID == guild.OwnerID) + if (targetData.Id == guild.OwnerID) { return Result.FromSuccess($"UserCannot{action}Owner".Localized()); } - var targetRoles = roles.Where(r => targetMember.Roles.Contains(r.ID)).ToList(); - var botRoles = roles.Where(r => botMember.Roles.Contains(r.ID)); + var targetRoles = roles.Where(r => targetData.Roles.Contains(r.ID.Value)).ToList(); + var botRoles = roles.Where(r => botData.Roles.Contains(r.ID.Value)); var targetBotRoleDiff = targetRoles.MaxOrDefault(r => r.Position) - botRoles.MaxOrDefault(r => r.Position); if (targetBotRoleDiff >= 0) @@ -153,7 +132,7 @@ public sealed class AccessControlService return Result.FromSuccess($"BotCannot{action}Target".Localized()); } - var interacterRoles = roles.Where(r => interacter.Roles.Contains(r.ID)); + var interacterRoles = roles.Where(r => interacterData.Roles.Contains(r.ID.Value)); var targetInteracterRoleDiff = targetRoles.MaxOrDefault(r => r.Position) - interacterRoles.MaxOrDefault(r => r.Position); return targetInteracterRoleDiff < 0