Complete builders, start work on using them
This commit is contained in:
74
src/Discord.Net.Commands/Info/ModuleInfo.cs
Normal file
74
src/Discord.Net.Commands/Info/ModuleInfo.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
using Discord.Commands.Builders;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
public class ModuleInfo
|
||||
{
|
||||
public CommandService Service { get; }
|
||||
public string Name { get; }
|
||||
public string Summary { get; }
|
||||
public string Remarks { get; }
|
||||
|
||||
public IReadOnlyList<string> Aliases { get; }
|
||||
public IEnumerable<CommandInfo> Commands { get; }
|
||||
public IReadOnlyList<PreconditionAttribute> Preconditions { get; }
|
||||
|
||||
internal ModuleInfo(ModuleBuilder builder, CommandService service)
|
||||
{
|
||||
Service = service;
|
||||
|
||||
Name = builder.Name;
|
||||
Summary = builder.Summary;
|
||||
Remarks = builder.Remarks;
|
||||
|
||||
Aliases = BuildAliases(builder).ToImmutableArray();
|
||||
Commands = builder.Commands.Select(x => x.Build(this, service));
|
||||
Preconditions = BuildPreconditions(builder).ToImmutableArray();
|
||||
}
|
||||
|
||||
private static List<string> BuildAliases(ModuleBuilder builder)
|
||||
{
|
||||
IEnumerable<string> result = null;
|
||||
|
||||
Stack<ModuleBuilder> builderStack = new Stack<ModuleBuilder>();
|
||||
|
||||
ModuleBuilder parent = builder;
|
||||
while (parent.ParentModule != null)
|
||||
{
|
||||
builderStack.Push(parent);
|
||||
parent = parent.ParentModule;
|
||||
}
|
||||
|
||||
while (builderStack.Count() > 0)
|
||||
{
|
||||
ModuleBuilder level = builderStack.Pop(); // get the topmost builder
|
||||
if (result == null)
|
||||
result = level.Aliases.ToList(); // create a shallow copy so we don't overwrite the builder unexpectedly
|
||||
else
|
||||
result = result.Permutate(level.Aliases, (first, second) => first + " " + second);
|
||||
}
|
||||
|
||||
return result.ToList();
|
||||
}
|
||||
|
||||
private static List<PreconditionAttribute> BuildPreconditions(ModuleBuilder builder)
|
||||
{
|
||||
var result = new List<PreconditionAttribute>();
|
||||
|
||||
|
||||
ModuleBuilder parent = builder;
|
||||
while (parent.ParentModule != null)
|
||||
{
|
||||
result.AddRange(parent.Preconditions);
|
||||
parent = parent.ParentModule;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user