Allow arbitrary attributes to be added to commands (#458)

* Allow arbitrary attributes to be added to commands

I still don't approve adding type info back into commands, so
I decided to use this solution for allowing arbitrary attributes to be
added to commands.

Add attributes property to ParameterBuilder

Add Attributes properties to info types

* Why on earth git

* Add using for system so that Attribute can be used
This commit is contained in:
Finite Reality
2017-06-29 22:30:26 +01:00
committed by RogueException
parent 394e0aa4d1
commit b96748f9c3
7 changed files with 56 additions and 2 deletions

View File

@@ -31,6 +31,7 @@ namespace Discord.Commands
public IReadOnlyList<string> Aliases { get; }
public IReadOnlyList<ParameterInfo> Parameters { get; }
public IReadOnlyList<PreconditionAttribute> Preconditions { get; }
public IReadOnlyList<Attribute> Attributes { get; }
internal CommandInfo(CommandBuilder builder, ModuleInfo module, CommandService service)
{
@@ -57,6 +58,7 @@ namespace Discord.Commands
.ToImmutableArray();
Preconditions = builder.Preconditions.ToImmutableArray();
Attributes = builder.Attributes.ToImmutableArray();
Parameters = builder.Parameters.Select(x => x.Build(this)).ToImmutableArray();
HasVarArgs = builder.Parameters.Count > 0 ? builder.Parameters[builder.Parameters.Count - 1].IsMultiple : false;

View File

@@ -1,3 +1,4 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
@@ -16,6 +17,7 @@ namespace Discord.Commands
public IReadOnlyList<string> Aliases { get; }
public IReadOnlyList<CommandInfo> Commands { get; }
public IReadOnlyList<PreconditionAttribute> Preconditions { get; }
public IReadOnlyList<Attribute> Attributes { get; }
public IReadOnlyList<ModuleInfo> Submodules { get; }
public ModuleInfo Parent { get; }
public bool IsSubmodule => Parent != null;
@@ -32,6 +34,7 @@ namespace Discord.Commands
Aliases = BuildAliases(builder, service).ToImmutableArray();
Commands = builder.Commands.Select(x => x.Build(this, service)).ToImmutableArray();
Preconditions = BuildPreconditions(builder).ToImmutableArray();
Attributes = BuildAttributes(builder).ToImmutableArray();
Submodules = BuildSubmodules(builder, service).ToImmutableArray();
}
@@ -86,5 +89,19 @@ namespace Discord.Commands
return result;
}
private static List<Attribute> BuildAttributes(ModuleBuilder builder)
{
var result = new List<Attribute>();
ModuleBuilder parent = builder;
while (parent != null)
{
result.AddRange(parent.Attributes);
parent = parent.Parent;
}
return result;
}
}
}

View File

@@ -21,6 +21,7 @@ namespace Discord.Commands
public object DefaultValue { get; }
public IReadOnlyList<ParameterPreconditionAttribute> Preconditions { get; }
public IReadOnlyList<Attribute> Attributes { get; }
internal ParameterInfo(ParameterBuilder builder, CommandInfo command, CommandService service)
{
@@ -36,6 +37,7 @@ namespace Discord.Commands
DefaultValue = builder.DefaultValue;
Preconditions = builder.Preconditions.ToImmutableArray();
Attributes = builder.Attributes.ToImmutableArray();
_reader = builder.TypeReader;
}