From baf1efb4469b2a89b8f4b61117bda2f3e8e0b9c9 Mon Sep 17 00:00:00 2001 From: RogueException Date: Tue, 14 Jun 2016 21:34:41 -0300 Subject: [PATCH] Added assembly searching to commands --- src/Discord.Net.Commands/CommandParser.cs | 55 +++++++++++++++++------ src/Discord.Net/Net/Queue/RequestQueue.cs | 2 +- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/Discord.Net.Commands/CommandParser.cs b/src/Discord.Net.Commands/CommandParser.cs index adee2715b..8157a3afd 100644 --- a/src/Discord.Net.Commands/CommandParser.cs +++ b/src/Discord.Net.Commands/CommandParser.cs @@ -12,21 +12,38 @@ namespace Discord.Commands public string Name { get; } public IEnumerable Commands { get; } - internal Module(object module, TypeInfo typeInfo) + internal Module(object parent, TypeInfo typeInfo) { List commands = new List(); - SearchClass(commands); + SearchClass(parent, commands, typeInfo); Commands = commands; } - private void SearchClass(List commands) + private void SearchClass(object parent, List commands, TypeInfo typeInfo) { - //TODO: Implement + foreach (var method in typeInfo.DeclaredMethods) + { + if (typeInfo.GetCustomAttribute() != null) + { + + } + } + foreach (var type in typeInfo.DeclaredNestedTypes) + { + if (typeInfo.GetCustomAttribute() != null) + { + SearchClass(CommandParser.CreateObject(typeInfo), commands, type); + } + } } } public class Command { public string SourceName { get; } + + internal Command(TypeInfo typeInfo) + { + } } public class CommandParser @@ -46,7 +63,10 @@ namespace Discord.Commands { if (_modules.ContainsKey(module)) throw new ArgumentException($"This module has already been loaded."); - return LoadInternal(module, module.GetType().GetTypeInfo()); + var typeInfo = module.GetType().GetTypeInfo(); + if (typeInfo.GetCustomAttribute() == null) + throw new ArgumentException($"Modules must be marked with ModuleAttribute."); + return LoadInternal(module, typeInfo); } finally { @@ -70,15 +90,7 @@ namespace Discord.Commands var typeInfo = type.GetTypeInfo(); if (typeInfo.GetCustomAttribute() != null) { - var constructor = typeInfo.DeclaredConstructors.Where(x => x.GetParameters().Length == 0).FirstOrDefault(); - if (constructor == null) - throw new InvalidOperationException($"Failed to find a valid constructor for \"{typeInfo.FullName}\""); - object module; - try { module = constructor.Invoke(null); } - catch (Exception ex) - { - throw new InvalidOperationException($"Failed to create \"{typeInfo.FullName}\"", ex); - } + var module = CreateObject(typeInfo); modules.Add(LoadInternal(module, typeInfo)); } } @@ -102,5 +114,20 @@ namespace Discord.Commands _moduleLock.Release(); } } + + internal static object CreateObject(TypeInfo typeInfo) + { + var constructor = typeInfo.DeclaredConstructors.Where(x => x.GetParameters().Length == 0).FirstOrDefault(); + if (constructor == null) + throw new InvalidOperationException($"Failed to find a valid constructor for \"{typeInfo.FullName}\""); + try + { + return constructor.Invoke(null); + } + catch (Exception ex) + { + throw new InvalidOperationException($"Failed to create \"{typeInfo.FullName}\"", ex); + } + } } } diff --git a/src/Discord.Net/Net/Queue/RequestQueue.cs b/src/Discord.Net/Net/Queue/RequestQueue.cs index 631f5b457..27b11a38d 100644 --- a/src/Discord.Net/Net/Queue/RequestQueue.cs +++ b/src/Discord.Net/Net/Queue/RequestQueue.cs @@ -24,7 +24,7 @@ namespace Discord.Net.Queue _globalLimits = new Dictionary { //REST - [GlobalBucket.GeneralRest] = new BucketDefinition(0, 0), + [GlobalBucket.GeneralRest] = new BucketDefinition(0, 0), //No Limit //[GlobalBucket.Login] = new BucketDefinition(1, 1), [GlobalBucket.DirectMessage] = new BucketDefinition(5, 5), [GlobalBucket.SendEditMessage] = new BucketDefinition(50, 10),