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:
committed by
RogueException
parent
394e0aa4d1
commit
b96748f9c3
@@ -10,6 +10,7 @@ namespace Discord.Commands.Builders
|
||||
{
|
||||
private readonly List<PreconditionAttribute> _preconditions;
|
||||
private readonly List<ParameterBuilder> _parameters;
|
||||
private readonly List<Attribute> _attributes;
|
||||
private readonly List<string> _aliases;
|
||||
|
||||
public ModuleBuilder Module { get; }
|
||||
@@ -24,6 +25,7 @@ namespace Discord.Commands.Builders
|
||||
|
||||
public IReadOnlyList<PreconditionAttribute> Preconditions => _preconditions;
|
||||
public IReadOnlyList<ParameterBuilder> Parameters => _parameters;
|
||||
public IReadOnlyList<Attribute> Attributes => _attributes;
|
||||
public IReadOnlyList<string> Aliases => _aliases;
|
||||
|
||||
//Automatic
|
||||
@@ -33,6 +35,7 @@ namespace Discord.Commands.Builders
|
||||
|
||||
_preconditions = new List<PreconditionAttribute>();
|
||||
_parameters = new List<ParameterBuilder>();
|
||||
_attributes = new List<Attribute>();
|
||||
_aliases = new List<string>();
|
||||
}
|
||||
//User-defined
|
||||
@@ -83,6 +86,11 @@ namespace Discord.Commands.Builders
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public CommandBuilder AddAttributes(params Attribute[] attributes)
|
||||
{
|
||||
_attributes.AddRange(attributes);
|
||||
return this;
|
||||
}
|
||||
public CommandBuilder AddPrecondition(PreconditionAttribute precondition)
|
||||
{
|
||||
_preconditions.Add(precondition);
|
||||
|
||||
@@ -10,6 +10,7 @@ namespace Discord.Commands.Builders
|
||||
private readonly List<CommandBuilder> _commands;
|
||||
private readonly List<ModuleBuilder> _submodules;
|
||||
private readonly List<PreconditionAttribute> _preconditions;
|
||||
private readonly List<Attribute> _attributes;
|
||||
private readonly List<string> _aliases;
|
||||
|
||||
public CommandService Service { get; }
|
||||
@@ -21,6 +22,7 @@ namespace Discord.Commands.Builders
|
||||
public IReadOnlyList<CommandBuilder> Commands => _commands;
|
||||
public IReadOnlyList<ModuleBuilder> Modules => _submodules;
|
||||
public IReadOnlyList<PreconditionAttribute> Preconditions => _preconditions;
|
||||
public IReadOnlyList<Attribute> Attributes => _attributes;
|
||||
public IReadOnlyList<string> Aliases => _aliases;
|
||||
|
||||
//Automatic
|
||||
@@ -32,6 +34,7 @@ namespace Discord.Commands.Builders
|
||||
_commands = new List<CommandBuilder>();
|
||||
_submodules = new List<ModuleBuilder>();
|
||||
_preconditions = new List<PreconditionAttribute>();
|
||||
_attributes = new List<Attribute>();
|
||||
_aliases = new List<string>();
|
||||
}
|
||||
//User-defined
|
||||
@@ -69,6 +72,11 @@ namespace Discord.Commands.Builders
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public ModuleBuilder AddAttributes(params Attribute[] attributes)
|
||||
{
|
||||
_attributes.AddRange(attributes);
|
||||
return this;
|
||||
}
|
||||
public ModuleBuilder AddPrecondition(PreconditionAttribute precondition)
|
||||
{
|
||||
_preconditions.Add(precondition);
|
||||
|
||||
@@ -122,6 +122,9 @@ namespace Discord.Commands
|
||||
case PreconditionAttribute precondition:
|
||||
builder.AddPrecondition(precondition);
|
||||
break;
|
||||
default:
|
||||
builder.AddAttributes(attribute);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,6 +176,9 @@ namespace Discord.Commands
|
||||
case PreconditionAttribute precondition:
|
||||
builder.AddPrecondition(precondition);
|
||||
break;
|
||||
default:
|
||||
builder.AddAttributes(attribute);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,6 +245,9 @@ namespace Discord.Commands
|
||||
|
||||
builder.IsRemainder = true;
|
||||
break;
|
||||
default:
|
||||
builder.AddAttributes(attribute);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -289,4 +298,4 @@ namespace Discord.Commands
|
||||
!methodInfo.IsGenericMethod;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,8 @@ namespace Discord.Commands.Builders
|
||||
{
|
||||
public class ParameterBuilder
|
||||
{
|
||||
private readonly List<ParameterPreconditionAttribute> _preconditions;
|
||||
private readonly List<ParameterPreconditionAttribute> _preconditions;
|
||||
private readonly List<Attribute> _attributes;
|
||||
|
||||
public CommandBuilder Command { get; }
|
||||
public string Name { get; internal set; }
|
||||
@@ -22,11 +23,13 @@ namespace Discord.Commands.Builders
|
||||
public string Summary { get; set; }
|
||||
|
||||
public IReadOnlyList<ParameterPreconditionAttribute> Preconditions => _preconditions;
|
||||
public IReadOnlyList<Attribute> Attributes => _attributes;
|
||||
|
||||
//Automatic
|
||||
internal ParameterBuilder(CommandBuilder command)
|
||||
{
|
||||
_preconditions = new List<ParameterPreconditionAttribute>();
|
||||
_attributes = new List<Attribute>();
|
||||
|
||||
Command = command;
|
||||
}
|
||||
@@ -84,6 +87,11 @@ namespace Discord.Commands.Builders
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder AddAttributes(params Attribute[] attributes)
|
||||
{
|
||||
_attributes.AddRange(attributes);
|
||||
return this;
|
||||
}
|
||||
public ParameterBuilder AddPrecondition(ParameterPreconditionAttribute precondition)
|
||||
{
|
||||
_preconditions.Add(precondition);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user