From daa1dd41843e0c69cfe9ce2b07bc933e1da27cb6 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Thu, 20 Jul 2023 12:36:21 +0500 Subject: [PATCH] General fixes (#54) Depends on #55 These changes are really small and I don't know how to reasonably split them into multiple PRs, but here's the changelog: - The result of the `UtilityService#LogActionAsync` call in `BanCommandGroup#BanUserAsync` is no longer ignored and it's errors will now prevent feedback from being sent; - Converted `if` statement to a `return` with ternary in `LanguageOption`; - Slightly decreased method complexity of `MessageDeletedResponder#RespondAsync` by cleverly using Results; - Changed the order of parameters for `UtilityService#LogActionAsync` to flow better; - Removed the exemption for `ConvertIfToReturnStatement` for InspectCode. --------- Signed-off-by: Octol1ttle --- .github/workflows/resharper.yml | 2 +- src/Commands/BanCommandGroup.cs | 6 ++++-- src/Data/Options/LanguageOption.cs | 7 +++---- src/Responders/MessageDeletedResponder.cs | 14 ++++++-------- src/Services/UtilityService.cs | 10 +++++----- 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/.github/workflows/resharper.yml b/.github/workflows/resharper.yml index b0ec1a3..29302aa 100644 --- a/.github/workflows/resharper.yml +++ b/.github/workflows/resharper.yml @@ -31,5 +31,5 @@ jobs: uses: muno92/resharper_inspectcode@1.7.1 with: solutionPath: ./Boyfriend.sln - ignoreIssueType: InvertIf, ConvertIfStatementToReturnStatement, ConvertIfStatementToSwitchStatement + ignoreIssueType: InvertIf, ConvertIfStatementToSwitchStatement solutionWideAnalysis: true diff --git a/src/Commands/BanCommandGroup.cs b/src/Commands/BanCommandGroup.cs index 902c753..14f6d4f 100644 --- a/src/Commands/BanCommandGroup.cs +++ b/src/Commands/BanCommandGroup.cs @@ -152,7 +152,9 @@ public class BanCommandGroup : CommandGroup { title, target) .WithColour(ColorsList.Green).Build(); - _utility.LogActionAsync(cfg, channelId, title, target, description, user, CancellationToken); + var logResult = _utility.LogActionAsync(cfg, channelId, user, title, description, target, CancellationToken); + if (!logResult.IsSuccess) + return Result.FromError(logResult.Error); return await _feedbackService.SendContextualEmbedResultAsync(embed, CancellationToken); } @@ -222,7 +224,7 @@ public class BanCommandGroup : CommandGroup { var title = string.Format(Messages.UserUnbanned, target.GetTag()); var description = string.Format(Messages.DescriptionActionReason, reason); - var logResult = _utility.LogActionAsync(cfg, channelId, title, target, description, user, CancellationToken); + var logResult = _utility.LogActionAsync(cfg, channelId, user, title, description, target, CancellationToken); if (!logResult.IsSuccess) return Result.FromError(logResult.Error); diff --git a/src/Data/Options/LanguageOption.cs b/src/Data/Options/LanguageOption.cs index 6c4a49f..dbe1b4f 100644 --- a/src/Data/Options/LanguageOption.cs +++ b/src/Data/Options/LanguageOption.cs @@ -27,9 +27,8 @@ public class LanguageOption : Option { /// public override Result Set(JsonNode settings, string from) { - if (!CultureInfoCache.ContainsKey(from.ToLowerInvariant())) - return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.LanguageNotSupported)); - - return base.Set(settings, from.ToLowerInvariant()); + return CultureInfoCache.ContainsKey(from.ToLowerInvariant()) + ? base.Set(settings, from.ToLowerInvariant()) + : Result.FromError(new ArgumentInvalidError(nameof(from), Messages.LanguageNotSupported)); } } diff --git a/src/Responders/MessageDeletedResponder.cs b/src/Responders/MessageDeletedResponder.cs index 903db84..652631e 100644 --- a/src/Responders/MessageDeletedResponder.cs +++ b/src/Responders/MessageDeletedResponder.cs @@ -46,15 +46,13 @@ public class MessageDeletedResponder : IResponder { if (!auditLogResult.IsDefined(out var auditLogPage)) return Result.FromError(auditLogResult); var auditLog = auditLogPage.AuditLogEntries.Single(); - if (!auditLog.Options.IsDefined(out var options)) - return Result.FromError(new ArgumentNullError(nameof(auditLog.Options))); - var user = message.Author; - if (options.ChannelID == gatewayEvent.ChannelID - && DateTimeOffset.UtcNow.Subtract(auditLog.ID.Timestamp).TotalSeconds <= 2) { - var userResult = await _userApi.GetUserAsync(auditLog.UserID!.Value, ct); - if (!userResult.IsDefined(out user)) return Result.FromError(userResult); - } + var userResult = Result.FromSuccess(message.Author); + if (auditLog.Options.Value.ChannelID == gatewayEvent.ChannelID + && DateTimeOffset.UtcNow.Subtract(auditLog.ID.Timestamp).TotalSeconds <= 2) + userResult = await _userApi.GetUserAsync(auditLog.UserID!.Value, ct); + + if (!userResult.IsDefined(out var user)) return Result.FromError(userResult); Messages.Culture = GuildSettings.Language.Get(cfg); diff --git a/src/Services/UtilityService.cs b/src/Services/UtilityService.cs index 44168fa..18808eb 100644 --- a/src/Services/UtilityService.cs +++ b/src/Services/UtilityService.cs @@ -154,15 +154,15 @@ public class UtilityService : IHostedService { /// /// The guild configuration. /// The ID of the channel where the action was executed. - /// The title for the embed. - /// The user whose avatar will be displayed next to the of the embed. - /// The description of the embed. /// The user who performed the action. + /// The title for the embed. + /// The description of the embed. + /// The user whose avatar will be displayed next to the of the embed. /// The cancellation token for this operation. /// public Result LogActionAsync( - JsonNode cfg, Snowflake channelId, string title, IUser avatar, string description, - IUser user, CancellationToken ct = default) { + JsonNode cfg, Snowflake channelId, IUser user, string title, string description, IUser avatar, + CancellationToken ct = default) { var publicChannel = GuildSettings.PublicFeedbackChannel.Get(cfg); var privateChannel = GuildSettings.PrivateFeedbackChannel.Get(cfg); if (GuildSettings.PublicFeedbackChannel.Get(cfg).EmptyOrEqualTo(channelId)