Added new rules concerning type constraints and :base() calls. Reformatted attributes folder

This commit is contained in:
Kwoth
2021-12-26 17:45:26 +01:00
parent d5fd6aae8e
commit 9ae030a5c5
10 changed files with 39 additions and 21 deletions

View File

@@ -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

View File

@@ -2,11 +2,11 @@
public static class CommandNameLoadHelper
{
private static readonly YamlDotNet.Serialization.IDeserializer _deserializer
= new YamlDotNet.Serialization.Deserializer();
public static Lazy<Dictionary<string, string[]>> LazyCommandAliases = new(() => LoadCommandNames());
private static readonly YamlDotNet.Serialization.IDeserializer _deserializer =
new YamlDotNet.Serialization.Deserializer();
public static Lazy<Dictionary<string, string[]>> LazyCommandAliases = new(() => LoadCommandNames());
public static Dictionary<string, string[]> LoadCommandNames(string aliasesFilePath = "data/aliases.yml")
{
var text = File.ReadAllText(aliasesFilePath);

View File

@@ -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();

View File

@@ -3,7 +3,8 @@
[AttributeUsage(AttributeTargets.Class)]
internal sealed class NadekoModuleAttribute : GroupAttribute
{
public NadekoModuleAttribute(string moduleName) : base(moduleName)
public NadekoModuleAttribute(string moduleName)
: base(moduleName)
{
}
}

View File

@@ -5,10 +5,16 @@ namespace NadekoBot.Common.Attributes;
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class OwnerOnlyAttribute : PreconditionAttribute
{
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo executingCommand, IServiceProvider services)
public override Task<PreconditionResult> CheckPermissionsAsync(
ICommandContext context,
CommandInfo executingCommand,
IServiceProvider services)
{
var creds = services.GetRequiredService<IBotCredsProvider>().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")
);
}
}

View File

@@ -15,7 +15,10 @@ public sealed class RatelimitAttribute : PreconditionAttribute
Seconds = seconds;
}
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
public override Task<PreconditionResult> 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<IDataCache>();
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.";

View File

@@ -6,20 +6,25 @@ namespace Discord;
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class UserPermAttribute : RequireUserPermissionAttribute
{
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
public override Task<PreconditionResult> CheckPermissionsAsync(
ICommandContext context,
CommandInfo command,
IServiceProvider services)
{
var permService = services.GetRequiredService<DiscordPermOverrideService>();
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)
{
}
}

View File

@@ -4,9 +4,6 @@
// and they get looped through constantly
public static class ArrayExtensions
{
private static int x = 0;
/// <summary>
/// Create a new array from the old array + new element at the end
/// </summary>

View File

@@ -68,7 +68,8 @@ public static class EnumerableExtensions
this IEnumerable<KeyValuePair<TKey, TValue>> dict)
=> new(dict);
public static IndexedCollection<T> ToIndexed<T>(this IEnumerable<T> enumerable) where T : class, IIndexed
public static IndexedCollection<T> ToIndexed<T>(this IEnumerable<T> enumerable)
where T : class, IIndexed
=> new(enumerable);
// todo use this extension instead of Task.WhenAll

View File

@@ -10,7 +10,7 @@ public static class LinkedListExtensions
{
if (predicate(node.Value))
return node;
node = node.Next;
}