Complete command builders implementation

In theory this should just work, more testing is needed though
This commit is contained in:
FiniteReality
2016-11-18 09:14:19 +00:00
parent 6d46347ebc
commit de645548a9
7 changed files with 177 additions and 45 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
@@ -22,6 +23,8 @@ namespace Discord.Commands.Builders
public string Name { get; set; }
public string Summary { get; set; }
public string Remarks { get; set; }
public RunMode RunMode { get; set; }
public int Priority { get; set; }
public Func<CommandContext, object[], IDependencyMap, Task> Callback { get; set; }
public ModuleBuilder Module { get; }
@@ -47,6 +50,18 @@ namespace Discord.Commands.Builders
return this;
}
public CommandBuilder SetRunMode(RunMode runMode)
{
RunMode = runMode;
return this;
}
public CommandBuilder SetPriority(int priority)
{
Priority = priority;
return this;
}
public CommandBuilder SetCallback(Func<CommandContext, object[], IDependencyMap, Task> callback)
{
Callback = callback;
@@ -75,6 +90,28 @@ namespace Discord.Commands.Builders
internal CommandInfo Build(ModuleInfo info, CommandService service)
{
if (aliases.Count == 0)
throw new InvalidOperationException("Commands require at least one alias to be registered");
if (Callback == null)
throw new InvalidOperationException("Commands require a callback to be built");
if (Name == null)
Name = aliases[0];
if (parameters.Count > 0)
{
var lastParam = parameters[parameters.Count - 1];
var firstMultipleParam = parameters.FirstOrDefault(x => x.Multiple);
if ((firstMultipleParam != null) && (firstMultipleParam != lastParam))
throw new InvalidOperationException("Only the last parameter in a command may have the Multiple flag.");
var firstRemainderParam = parameters.FirstOrDefault(x => x.Remainder);
if ((firstRemainderParam != null) && (firstRemainderParam != lastParam))
throw new InvalidOperationException("Only the last parameter in a command may have the Remainder flag.");
}
return new CommandInfo(this, info, service);
}
}

View File

@@ -83,6 +83,15 @@ namespace Discord.Commands.Builders
public ModuleInfo Build(CommandService service)
{
if (aliases.Count == 0)
throw new InvalidOperationException("Modules require at least one alias to be registered");
if (commands.Count == 0 && submodules.Count == 0)
throw new InvalidOperationException("Tried to build empty module");
if (Name == null)
Name = aliases[0];
return new ModuleInfo(this, service);
}
}

View File

@@ -81,9 +81,19 @@ namespace Discord.Commands.Builders
internal ParameterInfo Build(CommandInfo info, CommandService service)
{
// TODO: should we throw when we don't have a name?
if (Name == null)
Name = "[unknown parameter]";
if (ParameterType == null)
throw new InvalidOperationException($"Could not build parameter {Name} from command {info.Name} - An invalid parameter type was given");
if (TypeReader == null)
TypeReader = service.GetTypeReader(ParameterType);
if (TypeReader == null)
throw new InvalidOperationException($"Could not build parameter {Name} from command {info.Name} - A valid TypeReader could not be found");
return new ParameterInfo(this, info, service);
}
}