From 9ae030a5c599ee741ea2d9f77db30de4b6e1aa06 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Sun, 26 Dec 2021 17:45:26 +0100 Subject: [PATCH] Added new rules concerning type constraints and :base() calls. Reformatted attributes folder --- src/NadekoBot/.editorconfig | 5 +++++ .../Common/Attributes/CommandNameLoadHelper.cs | 10 +++++----- src/NadekoBot/Common/Attributes/NadekoCommand.cs | 2 +- .../Common/Attributes/NadekoModuleAttribute.cs | 3 ++- .../Common/Attributes/OwnerOnlyAttribute.cs | 10 ++++++++-- src/NadekoBot/Common/Attributes/Ratelimit.cs | 7 +++++-- src/NadekoBot/Common/Attributes/UserPerm.cs | 15 ++++++++++----- src/NadekoBot/_Extensions/ArrayExtensions.cs | 3 --- src/NadekoBot/_Extensions/EnumerableExtensions.cs | 3 ++- src/NadekoBot/_Extensions/LinkedListExtensions.cs | 2 +- 10 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/NadekoBot/.editorconfig b/src/NadekoBot/.editorconfig index 2b69e7dfd..38173d65f 100644 --- a/src/NadekoBot/.editorconfig +++ b/src/NadekoBot/.editorconfig @@ -330,3 +330,8 @@ resharper_csharp_wrap_parameters_style = chop_if_long resharper_force_chop_compound_if_expression = true resharper_keep_existing_linebreaks = false resharper_max_formal_parameters_on_line = 3 + +resharper_csharp_wrap_before_first_type_parameter_constraint=true +resharper_csharp_place_type_constraints_on_same_line=false +resharper_csharp_wrap_before_extends_colon=true +resharper_csharp_place_constructor_initializer_on_same_line=false \ No newline at end of file diff --git a/src/NadekoBot/Common/Attributes/CommandNameLoadHelper.cs b/src/NadekoBot/Common/Attributes/CommandNameLoadHelper.cs index 9bebf0c36..f4bb3ab9c 100644 --- a/src/NadekoBot/Common/Attributes/CommandNameLoadHelper.cs +++ b/src/NadekoBot/Common/Attributes/CommandNameLoadHelper.cs @@ -2,11 +2,11 @@ public static class CommandNameLoadHelper { - - private static readonly YamlDotNet.Serialization.IDeserializer _deserializer - = new YamlDotNet.Serialization.Deserializer(); - - public static Lazy> LazyCommandAliases = new(() => LoadCommandNames()); + private static readonly YamlDotNet.Serialization.IDeserializer _deserializer = + new YamlDotNet.Serialization.Deserializer(); + + public static Lazy> LazyCommandAliases = new(() => LoadCommandNames()); + public static Dictionary LoadCommandNames(string aliasesFilePath = "data/aliases.yml") { var text = File.ReadAllText(aliasesFilePath); diff --git a/src/NadekoBot/Common/Attributes/NadekoCommand.cs b/src/NadekoBot/Common/Attributes/NadekoCommand.cs index 1c83a6a9f..76ef11ac0 100644 --- a/src/NadekoBot/Common/Attributes/NadekoCommand.cs +++ b/src/NadekoBot/Common/Attributes/NadekoCommand.cs @@ -5,7 +5,7 @@ namespace NadekoBot.Common.Attributes; [AttributeUsage(AttributeTargets.Method)] public sealed class NadekoCommandAttribute : CommandAttribute { - public NadekoCommandAttribute([CallerMemberName] string memberName="") + public NadekoCommandAttribute([CallerMemberName] string memberName = "") : base(CommandNameLoadHelper.GetCommandNameFor(memberName)) => this.MethodName = memberName.ToLowerInvariant(); diff --git a/src/NadekoBot/Common/Attributes/NadekoModuleAttribute.cs b/src/NadekoBot/Common/Attributes/NadekoModuleAttribute.cs index 819b8d930..0e3207a76 100644 --- a/src/NadekoBot/Common/Attributes/NadekoModuleAttribute.cs +++ b/src/NadekoBot/Common/Attributes/NadekoModuleAttribute.cs @@ -3,7 +3,8 @@ [AttributeUsage(AttributeTargets.Class)] internal sealed class NadekoModuleAttribute : GroupAttribute { - public NadekoModuleAttribute(string moduleName) : base(moduleName) + public NadekoModuleAttribute(string moduleName) + : base(moduleName) { } } \ No newline at end of file diff --git a/src/NadekoBot/Common/Attributes/OwnerOnlyAttribute.cs b/src/NadekoBot/Common/Attributes/OwnerOnlyAttribute.cs index acc57d17b..35c900cfb 100644 --- a/src/NadekoBot/Common/Attributes/OwnerOnlyAttribute.cs +++ b/src/NadekoBot/Common/Attributes/OwnerOnlyAttribute.cs @@ -5,10 +5,16 @@ namespace NadekoBot.Common.Attributes; [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public sealed class OwnerOnlyAttribute : PreconditionAttribute { - public override Task CheckPermissionsAsync(ICommandContext context, CommandInfo executingCommand, IServiceProvider services) + public override Task CheckPermissionsAsync( + ICommandContext context, + CommandInfo executingCommand, + IServiceProvider services) { var creds = services.GetRequiredService().GetCreds(); - return Task.FromResult(creds.IsOwner(context.User) || context.Client.CurrentUser.Id == context.User.Id ? PreconditionResult.FromSuccess() : PreconditionResult.FromError("Not owner")); + return Task.FromResult(creds.IsOwner(context.User) || context.Client.CurrentUser.Id == context.User.Id + ? PreconditionResult.FromSuccess() + : PreconditionResult.FromError("Not owner") + ); } } \ No newline at end of file diff --git a/src/NadekoBot/Common/Attributes/Ratelimit.cs b/src/NadekoBot/Common/Attributes/Ratelimit.cs index b5b9cdbe7..6eff6abb4 100644 --- a/src/NadekoBot/Common/Attributes/Ratelimit.cs +++ b/src/NadekoBot/Common/Attributes/Ratelimit.cs @@ -15,7 +15,10 @@ public sealed class RatelimitAttribute : PreconditionAttribute Seconds = seconds; } - public override Task CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) + public override Task CheckPermissionsAsync( + ICommandContext context, + CommandInfo command, + IServiceProvider services) { if (Seconds == 0) return Task.FromResult(PreconditionResult.FromSuccess()); @@ -23,7 +26,7 @@ public sealed class RatelimitAttribute : PreconditionAttribute var cache = services.GetRequiredService(); var rem = cache.TryAddRatelimit(context.User.Id, command.Name, Seconds); - if(rem is null) + if (rem is null) return Task.FromResult(PreconditionResult.FromSuccess()); var msgContent = $"You can use this command again in {rem.Value.TotalSeconds:F1}s."; diff --git a/src/NadekoBot/Common/Attributes/UserPerm.cs b/src/NadekoBot/Common/Attributes/UserPerm.cs index 616143dd9..3d0743f9d 100644 --- a/src/NadekoBot/Common/Attributes/UserPerm.cs +++ b/src/NadekoBot/Common/Attributes/UserPerm.cs @@ -6,20 +6,25 @@ namespace Discord; [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class UserPermAttribute : RequireUserPermissionAttribute { - public override Task CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) + public override Task CheckPermissionsAsync( + ICommandContext context, + CommandInfo command, + IServiceProvider services) { var permService = services.GetRequiredService(); - if (permService.TryGetOverrides(context.Guild?.Id ?? 0, command.Name.ToUpperInvariant(), out var _)) + if (permService.TryGetOverrides(context.Guild?.Id ?? 0, command.Name.ToUpperInvariant(), out _)) return Task.FromResult(PreconditionResult.FromSuccess()); - + return base.CheckPermissionsAsync(context, command, services); } - public UserPermAttribute(GuildPerm permission) : base(permission) + public UserPermAttribute(GuildPerm permission) + : base(permission) { } - public UserPermAttribute(ChannelPerm permission) : base(permission) + public UserPermAttribute(ChannelPerm permission) + : base(permission) { } } \ No newline at end of file diff --git a/src/NadekoBot/_Extensions/ArrayExtensions.cs b/src/NadekoBot/_Extensions/ArrayExtensions.cs index b6cc1f7f0..ac1078f4c 100644 --- a/src/NadekoBot/_Extensions/ArrayExtensions.cs +++ b/src/NadekoBot/_Extensions/ArrayExtensions.cs @@ -4,9 +4,6 @@ // and they get looped through constantly public static class ArrayExtensions { - private static int x = 0; - - /// /// Create a new array from the old array + new element at the end /// diff --git a/src/NadekoBot/_Extensions/EnumerableExtensions.cs b/src/NadekoBot/_Extensions/EnumerableExtensions.cs index a6db53ef2..6ccd6171c 100644 --- a/src/NadekoBot/_Extensions/EnumerableExtensions.cs +++ b/src/NadekoBot/_Extensions/EnumerableExtensions.cs @@ -68,7 +68,8 @@ public static class EnumerableExtensions this IEnumerable> dict) => new(dict); - public static IndexedCollection ToIndexed(this IEnumerable enumerable) where T : class, IIndexed + public static IndexedCollection ToIndexed(this IEnumerable enumerable) + where T : class, IIndexed => new(enumerable); // todo use this extension instead of Task.WhenAll diff --git a/src/NadekoBot/_Extensions/LinkedListExtensions.cs b/src/NadekoBot/_Extensions/LinkedListExtensions.cs index 9e5b717f6..10bf790b5 100644 --- a/src/NadekoBot/_Extensions/LinkedListExtensions.cs +++ b/src/NadekoBot/_Extensions/LinkedListExtensions.cs @@ -10,7 +10,7 @@ public static class LinkedListExtensions { if (predicate(node.Value)) return node; - + node = node.Next; }