using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
using Discord.Commands.Builders;
namespace Discord.Commands
{
///
/// Provides the information of a module.
///
public class ModuleInfo
{
///
/// Gets the command service associated with this module.
///
public CommandService Service { get; }
///
/// Gets the name of this module.
///
public string Name { get; }
///
/// Gets the summary of this module.
///
public string Summary { get; }
///
/// Gets the remarks of this module.
///
public string Remarks { get; }
///
/// Gets the group name (main prefix) of this module.
///
public string Group { get; }
///
/// Gets a read-only list of aliases associated with this module.
///
public IReadOnlyList Aliases { get; }
///
/// Gets a read-only list of commands associated with this module.
///
public IReadOnlyList Commands { get; }
///
/// Gets a read-only list of preconditions that apply to this module.
///
public IReadOnlyList Preconditions { get; }
///
/// Gets a read-only list of attributes that apply to this module.
///
public IReadOnlyList Attributes { get; }
///
/// Gets a read-only list of submodules associated with this module.
///
public IReadOnlyList Submodules { get; }
///
/// Gets the parent module of this submodule if applicable.
///
public ModuleInfo Parent { get; }
///
/// Gets a value that indicates whether this module is a submodule or not.
///
public bool IsSubmodule => Parent != null;
internal ModuleInfo(ModuleBuilder builder, CommandService service, IServiceProvider services, ModuleInfo parent = null)
{
Service = service;
Name = builder.Name;
Summary = builder.Summary;
Remarks = builder.Remarks;
Group = builder.Group;
Parent = parent;
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, services).ToImmutableArray();
}
private static IEnumerable BuildAliases(ModuleBuilder builder, CommandService service)
{
var result = builder.Aliases.ToList();
var builderQueue = new Queue();
var parent = builder;
while ((parent = parent.Parent) != null)
builderQueue.Enqueue(parent);
while (builderQueue.Count > 0)
{
var level = builderQueue.Dequeue();
// permute in reverse because we want to *prefix* our aliases
result = level.Aliases.Permutate(result, (first, second) =>
{
if (first == "")
return second;
else if (second == "")
return first;
else
return first + service._separatorChar + second;
}).ToList();
}
return result;
}
private List BuildSubmodules(ModuleBuilder parent, CommandService service, IServiceProvider services)
{
var result = new List();
foreach (var submodule in parent.Modules)
result.Add(submodule.Build(service, services, this));
return result;
}
private static List BuildPreconditions(ModuleBuilder builder)
{
var result = new List();
ModuleBuilder parent = builder;
while (parent != null)
{
result.AddRange(parent.Preconditions);
parent = parent.Parent;
}
return result;
}
private static List BuildAttributes(ModuleBuilder builder)
{
var result = new List();
ModuleBuilder parent = builder;
while (parent != null)
{
result.AddRange(parent.Attributes);
parent = parent.Parent;
}
return result;
}
}
}