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 <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-07-20 12:25:03 +05:00 committed by GitHub
parent e2bf083189
commit 685688bbe8
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23

View file

@ -64,13 +64,10 @@ public class UtilityService : IHostedService {
var currentUserResult = await _userApi.GetCurrentUserAsync(ct); var currentUserResult = await _userApi.GetCurrentUserAsync(ct);
if (!currentUserResult.IsDefined(out var currentUser)) if (!currentUserResult.IsDefined(out var currentUser))
return Result<string?>.FromError(currentUserResult); return Result<string?>.FromError(currentUserResult);
if (currentUser.ID == targetId)
return Result<string?>.FromSuccess($"UserCannot{action}Bot".Localized());
var guildResult = await _guildApi.GetGuildAsync(guildId, ct: ct); var guildResult = await _guildApi.GetGuildAsync(guildId, ct: ct);
if (!guildResult.IsDefined(out var guild)) if (!guildResult.IsDefined(out var guild))
return Result<string?>.FromError(guildResult); return Result<string?>.FromError(guildResult);
if (targetId == guild.OwnerID) return Result<string?>.FromSuccess($"UserCannot{action}Owner".Localized());
var targetMemberResult = await _guildApi.GetGuildMemberAsync(guildId, targetId, ct); var targetMemberResult = await _guildApi.GetGuildMemberAsync(guildId, targetId, ct);
if (!targetMemberResult.IsDefined(out var targetMember)) if (!targetMemberResult.IsDefined(out var targetMember))
@ -84,6 +81,25 @@ public class UtilityService : IHostedService {
if (!rolesResult.IsDefined(out var roles)) if (!rolesResult.IsDefined(out var roles))
return Result<string?>.FromError(rolesResult); return Result<string?>.FromError(rolesResult);
var interacterResult = await _guildApi.GetGuildMemberAsync(guildId, interacterId, ct);
return interacterResult.IsDefined(out var interacter)
? CheckInteractions(action, guild, roles, targetMember, currentMember, interacter)
: Result<string?>.FromError(interacterResult);
}
private static Result<string?> CheckInteractions(
string action, IGuild guild, IReadOnlyList<IRole> roles, IGuildMember targetMember, IGuildMember currentMember,
IGuildMember interacter) {
if (!targetMember.User.IsDefined(out var targetUser))
return Result<string?>.FromError(new ArgumentNullError(nameof(targetMember.User)));
if (!interacter.User.IsDefined(out var interacterUser))
return Result<string?>.FromError(new ArgumentNullError(nameof(interacter.User)));
if (currentMember.User == targetMember.User)
return Result<string?>.FromSuccess($"UserCannot{action}Bot".Localized());
if (targetUser.ID == guild.OwnerID) return Result<string?>.FromSuccess($"UserCannot{action}Owner".Localized());
var targetRoles = roles.Where(r => targetMember.Roles.Contains(r.ID)).ToList(); var targetRoles = roles.Where(r => targetMember.Roles.Contains(r.ID)).ToList();
var botRoles = roles.Where(r => currentMember.Roles.Contains(r.ID)); var botRoles = roles.Where(r => currentMember.Roles.Contains(r.ID));
@ -91,20 +107,15 @@ public class UtilityService : IHostedService {
if (targetBotRoleDiff >= 0) if (targetBotRoleDiff >= 0)
return Result<string?>.FromSuccess($"BotCannot{action}Target".Localized()); return Result<string?>.FromSuccess($"BotCannot{action}Target".Localized());
if (interacterId == guild.OwnerID) if (interacterUser.ID == guild.OwnerID)
return Result<string?>.FromSuccess(null); return Result<string?>.FromSuccess(null);
var interacterResult = await _guildApi.GetGuildMemberAsync(guildId, interacterId, ct);
if (!interacterResult.IsDefined(out var interacter))
return Result<string?>.FromError(interacterResult);
var interacterRoles = roles.Where(r => interacter.Roles.Contains(r.ID)); var interacterRoles = roles.Where(r => interacter.Roles.Contains(r.ID));
var targetInteracterRoleDiff var targetInteracterRoleDiff
= targetRoles.MaxOrDefault(r => r.Position) - interacterRoles.MaxOrDefault(r => r.Position); = targetRoles.MaxOrDefault(r => r.Position) - interacterRoles.MaxOrDefault(r => r.Position);
if (targetInteracterRoleDiff >= 0) return targetInteracterRoleDiff < 0
return Result<string?>.FromSuccess($"UserCannot{action}Target".Localized()); ? Result<string?>.FromSuccess(null)
: Result<string?>.FromSuccess($"UserCannot{action}Target".Localized());
return Result<string?>.FromSuccess(null);
} }
/// <summary> /// <summary>