Complete builders, start work on using them
This commit is contained in:
@@ -2,14 +2,18 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Discord.Commands
|
||||
namespace Discord.Commands.Builders
|
||||
{
|
||||
public class CommandBuilder
|
||||
{
|
||||
private List<PreconditionAttribute> preconditions;
|
||||
private List<ParameterBuilder> parameters;
|
||||
private List<string> aliases;
|
||||
|
||||
internal CommandBuilder(ModuleBuilder module, string prefix)
|
||||
{
|
||||
preconditions = new List<PreconditionAttribute>();
|
||||
parameters = new List<ParameterBuilder>();
|
||||
aliases = new List<string>();
|
||||
|
||||
if (prefix != null)
|
||||
@@ -21,16 +25,16 @@ namespace Discord.Commands
|
||||
Module = module;
|
||||
}
|
||||
|
||||
|
||||
public string Name { get; set; }
|
||||
public string Summary { get; set; }
|
||||
public string Remarks { get; set; }
|
||||
public Func<object[], Task> Callback { get; set; }
|
||||
public Func<CommandContext, object[], Task> Callback { get; set; }
|
||||
public ModuleBuilder Module { get; }
|
||||
|
||||
public List<PreconditionAttribute> Preconditions => preconditions;
|
||||
public List<ParameterBuilder> Parameters => parameters;
|
||||
public List<string> Aliases => aliases;
|
||||
|
||||
|
||||
public CommandBuilder SetName(string name)
|
||||
{
|
||||
Name = name;
|
||||
@@ -49,21 +53,33 @@ namespace Discord.Commands
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder SetCallback(Func<object[], Task> callback)
|
||||
public CommandBuilder SetCallback(Func<CommandContext, object[], Task> callback)
|
||||
{
|
||||
Callback = callback;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder AddPrecondition(PreconditionAttribute precondition)
|
||||
{
|
||||
preconditions.Add(precondition);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder AddParameter(ParameterBuilder parameter)
|
||||
{
|
||||
parameters.Add(parameter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder AddAlias(string alias)
|
||||
{
|
||||
aliases.Add(alias);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ModuleBuilder Done()
|
||||
internal CommandInfo Build(ModuleInfo info, CommandService service)
|
||||
{
|
||||
return Module;
|
||||
return new CommandInfo(this, info, service);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,15 +2,15 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace Discord.Commands
|
||||
namespace Discord.Commands.Builders
|
||||
{
|
||||
public class ModuleBuilder
|
||||
{
|
||||
private List<CommandBuilder> commands;
|
||||
private List<ModuleBuilder> submodules;
|
||||
private List<PreconditionAttribute> preconditions;
|
||||
private List<string> aliases;
|
||||
|
||||
|
||||
public ModuleBuilder()
|
||||
: this(null, null)
|
||||
{ }
|
||||
@@ -27,6 +27,7 @@ namespace Discord.Commands
|
||||
{
|
||||
commands = new List<CommandBuilder>();
|
||||
submodules = new List<ModuleBuilder>();
|
||||
preconditions = new List<PreconditionAttribute>();
|
||||
aliases = new List<string>();
|
||||
|
||||
if (prefix != null)
|
||||
@@ -38,7 +39,6 @@ namespace Discord.Commands
|
||||
ParentModule = parent;
|
||||
}
|
||||
|
||||
|
||||
public string Name { get; set; }
|
||||
public string Summary { get; set; }
|
||||
public string Remarks { get; set; }
|
||||
@@ -46,9 +46,9 @@ namespace Discord.Commands
|
||||
|
||||
public List<CommandBuilder> Commands => commands;
|
||||
public List<ModuleBuilder> Modules => submodules;
|
||||
public List<PreconditionAttribute> Preconditions => preconditions;
|
||||
public List<string> Aliases => aliases;
|
||||
|
||||
|
||||
public ModuleBuilder SetName(string name)
|
||||
{
|
||||
Name = name;
|
||||
@@ -73,6 +73,12 @@ namespace Discord.Commands
|
||||
return this;
|
||||
}
|
||||
|
||||
public ModuleBuilder AddPrecondition(PreconditionAttribute precondition)
|
||||
{
|
||||
preconditions.Add(precondition);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder AddCommand() => AddCommand(null);
|
||||
public CommandBuilder AddCommand(string name)
|
||||
{
|
||||
@@ -91,12 +97,9 @@ namespace Discord.Commands
|
||||
return builder;
|
||||
}
|
||||
|
||||
public ModuleBuilder Done()
|
||||
public ModuleInfo Build(CommandService service)
|
||||
{
|
||||
if (ParentModule == null)
|
||||
throw new InvalidOperationException("Cannot finish a top-level module!");
|
||||
|
||||
return ParentModule;
|
||||
return new ModuleInfo(this, service);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
86
src/Discord.Net.Commands/Builders/ParameterBuilder.cs
Normal file
86
src/Discord.Net.Commands/Builders/ParameterBuilder.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Discord.Commands.Builders
|
||||
{
|
||||
public class ParameterBuilder
|
||||
{
|
||||
public ParameterBuilder()
|
||||
{ }
|
||||
|
||||
public ParameterBuilder(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
public string Summary { get; set; }
|
||||
public object DefaultValue { get; set; }
|
||||
public Type ParameterType { get; set; }
|
||||
|
||||
public TypeReader TypeReader { get; set; }
|
||||
|
||||
public bool Optional { get; set; }
|
||||
public bool Remainder { get; set; }
|
||||
public bool Multiple { get; set; }
|
||||
|
||||
public ParameterBuilder SetName(string name)
|
||||
{
|
||||
Name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetSummary(string summary)
|
||||
{
|
||||
Summary = summary;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetDefault<T>(T defaultValue)
|
||||
{
|
||||
DefaultValue = defaultValue;
|
||||
ParameterType = typeof(T);
|
||||
|
||||
if (ParameterType.IsArray)
|
||||
ParameterType = ParameterType.GetElementType();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetType(Type parameterType)
|
||||
{
|
||||
ParameterType = parameterType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetTypeReader(TypeReader reader)
|
||||
{
|
||||
TypeReader = reader;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetOptional(bool isOptional)
|
||||
{
|
||||
Optional = isOptional;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetRemainder(bool isRemainder)
|
||||
{
|
||||
Remainder = isRemainder;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetMultiple(bool isMultiple)
|
||||
{
|
||||
Multiple = isMultiple;
|
||||
return this;
|
||||
}
|
||||
|
||||
internal ParameterInfo Build(CommandInfo info, CommandService service)
|
||||
{
|
||||
return new ParameterInfo(this, info, service);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user