From 685688bbe8a4ab477dc38ad565a4d5694bb2baf9 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Thu, 20 Jul 2023 12:25:03 +0500 Subject: [PATCH] Reduce method complexity of UtilityService#CheckInteractionsAsync (#55) This PR reduces method complexity in `UtilityService#CheckInteractionsAsync` by splitting the method into two parts, similar to `/ban` and `/unban`. Additionally, it converts the last `if` statement to a `return` with ternary. After this is merged, status checks on #54 should succeed and that PR should be merged. Signed-off-by: Octol1ttle --- src/Services/UtilityService.cs | 35 ++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/Services/UtilityService.cs b/src/Services/UtilityService.cs index b1ae347..44168fa 100644 --- a/src/Services/UtilityService.cs +++ b/src/Services/UtilityService.cs @@ -64,13 +64,10 @@ public class UtilityService : IHostedService { var currentUserResult = await _userApi.GetCurrentUserAsync(ct); if (!currentUserResult.IsDefined(out var currentUser)) return Result.FromError(currentUserResult); - if (currentUser.ID == targetId) - return Result.FromSuccess($"UserCannot{action}Bot".Localized()); var guildResult = await _guildApi.GetGuildAsync(guildId, ct: ct); if (!guildResult.IsDefined(out var guild)) return Result.FromError(guildResult); - if (targetId == guild.OwnerID) return Result.FromSuccess($"UserCannot{action}Owner".Localized()); var targetMemberResult = await _guildApi.GetGuildMemberAsync(guildId, targetId, ct); if (!targetMemberResult.IsDefined(out var targetMember)) @@ -84,6 +81,25 @@ public class UtilityService : IHostedService { if (!rolesResult.IsDefined(out var roles)) return Result.FromError(rolesResult); + var interacterResult = await _guildApi.GetGuildMemberAsync(guildId, interacterId, ct); + return interacterResult.IsDefined(out var interacter) + ? CheckInteractions(action, guild, roles, targetMember, currentMember, interacter) + : Result.FromError(interacterResult); + } + + private static Result CheckInteractions( + string action, IGuild guild, IReadOnlyList roles, IGuildMember targetMember, IGuildMember currentMember, + IGuildMember interacter) { + if (!targetMember.User.IsDefined(out var targetUser)) + return Result.FromError(new ArgumentNullError(nameof(targetMember.User))); + if (!interacter.User.IsDefined(out var interacterUser)) + return Result.FromError(new ArgumentNullError(nameof(interacter.User))); + + if (currentMember.User == targetMember.User) + return Result.FromSuccess($"UserCannot{action}Bot".Localized()); + + if (targetUser.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 => currentMember.Roles.Contains(r.ID)); @@ -91,20 +107,15 @@ public class UtilityService : IHostedService { if (targetBotRoleDiff >= 0) return Result.FromSuccess($"BotCannot{action}Target".Localized()); - if (interacterId == guild.OwnerID) + if (interacterUser.ID == guild.OwnerID) return Result.FromSuccess(null); - var interacterResult = await _guildApi.GetGuildMemberAsync(guildId, interacterId, ct); - if (!interacterResult.IsDefined(out var interacter)) - return Result.FromError(interacterResult); - var interacterRoles = roles.Where(r => interacter.Roles.Contains(r.ID)); var targetInteracterRoleDiff = targetRoles.MaxOrDefault(r => r.Position) - interacterRoles.MaxOrDefault(r => r.Position); - if (targetInteracterRoleDiff >= 0) - return Result.FromSuccess($"UserCannot{action}Target".Localized()); - - return Result.FromSuccess(null); + return targetInteracterRoleDiff < 0 + ? Result.FromSuccess(null) + : Result.FromSuccess($"UserCannot{action}Target".Localized()); } ///