using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Diagnostics; using System.Reflection; namespace Discord.Commands { [DebuggerDisplay(@"{DebuggerDisplay,nq}")] public class ModuleInfo { internal readonly Func _builder; public TypeInfo Source { get; } public CommandService Service { get; } public string Name { get; } public string Prefix { get; } public string Summary { get; } public string Remarks { get; } public IEnumerable Commands { get; } public IReadOnlyList Preconditions { get; } internal ModuleInfo(TypeInfo source, CommandService service) { Source = source; Service = service; Name = source.Name; _builder = ReflectionUtils.CreateBuilder(source, Service); var groupAttr = source.GetCustomAttribute(); if (groupAttr != null) Prefix = groupAttr.Prefix; else Prefix = ""; var nameAttr = source.GetCustomAttribute(); if (nameAttr != null) Name = nameAttr.Text; var summaryAttr = source.GetCustomAttribute(); if (summaryAttr != null) Summary = summaryAttr.Text; var remarksAttr = source.GetCustomAttribute(); if (remarksAttr != null) Remarks = remarksAttr.Text; List commands = new List(); SearchClass(source, commands, Prefix); Commands = commands; Preconditions = Source.GetCustomAttributes().ToImmutableArray(); } private void SearchClass(TypeInfo parentType, List commands, string groupPrefix) { foreach (var method in parentType.DeclaredMethods) { var cmdAttr = method.GetCustomAttribute(); if (cmdAttr != null) commands.Add(new CommandInfo(method, this, cmdAttr, groupPrefix)); } foreach (var type in parentType.DeclaredNestedTypes) { var groupAttrib = type.GetCustomAttribute(); if (groupAttrib != null) { string nextGroupPrefix; if (groupPrefix != "") nextGroupPrefix = groupPrefix + " " + (groupAttrib.Prefix ?? type.Name.ToLowerInvariant()); else nextGroupPrefix = groupAttrib.Prefix ?? type.Name.ToLowerInvariant(); SearchClass(type, commands, nextGroupPrefix); } } } internal ModuleBase CreateInstance(IDependencyMap map) => _builder(map); public override string ToString() => Name; private string DebuggerDisplay => Name; } }