Added assembly searching to commands
This commit is contained in:
@@ -12,21 +12,38 @@ namespace Discord.Commands
|
|||||||
public string Name { get; }
|
public string Name { get; }
|
||||||
public IEnumerable<Command> Commands { get; }
|
public IEnumerable<Command> Commands { get; }
|
||||||
|
|
||||||
internal Module(object module, TypeInfo typeInfo)
|
internal Module(object parent, TypeInfo typeInfo)
|
||||||
{
|
{
|
||||||
List<Command> commands = new List<Command>();
|
List<Command> commands = new List<Command>();
|
||||||
SearchClass(commands);
|
SearchClass(parent, commands, typeInfo);
|
||||||
Commands = commands;
|
Commands = commands;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SearchClass(List<Command> commands)
|
private void SearchClass(object parent, List<Command> commands, TypeInfo typeInfo)
|
||||||
{
|
{
|
||||||
//TODO: Implement
|
foreach (var method in typeInfo.DeclaredMethods)
|
||||||
|
{
|
||||||
|
if (typeInfo.GetCustomAttribute<CommandAttribute>() != null)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach (var type in typeInfo.DeclaredNestedTypes)
|
||||||
|
{
|
||||||
|
if (typeInfo.GetCustomAttribute<GroupAttribute>() != null)
|
||||||
|
{
|
||||||
|
SearchClass(CommandParser.CreateObject(typeInfo), commands, type);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public class Command
|
public class Command
|
||||||
{
|
{
|
||||||
public string SourceName { get; }
|
public string SourceName { get; }
|
||||||
|
|
||||||
|
internal Command(TypeInfo typeInfo)
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class CommandParser
|
public class CommandParser
|
||||||
@@ -46,7 +63,10 @@ namespace Discord.Commands
|
|||||||
{
|
{
|
||||||
if (_modules.ContainsKey(module))
|
if (_modules.ContainsKey(module))
|
||||||
throw new ArgumentException($"This module has already been loaded.");
|
throw new ArgumentException($"This module has already been loaded.");
|
||||||
return LoadInternal(module, module.GetType().GetTypeInfo());
|
var typeInfo = module.GetType().GetTypeInfo();
|
||||||
|
if (typeInfo.GetCustomAttribute<ModuleAttribute>() == null)
|
||||||
|
throw new ArgumentException($"Modules must be marked with ModuleAttribute.");
|
||||||
|
return LoadInternal(module, typeInfo);
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -70,15 +90,7 @@ namespace Discord.Commands
|
|||||||
var typeInfo = type.GetTypeInfo();
|
var typeInfo = type.GetTypeInfo();
|
||||||
if (typeInfo.GetCustomAttribute<ModuleAttribute>() != null)
|
if (typeInfo.GetCustomAttribute<ModuleAttribute>() != null)
|
||||||
{
|
{
|
||||||
var constructor = typeInfo.DeclaredConstructors.Where(x => x.GetParameters().Length == 0).FirstOrDefault();
|
var module = CreateObject(typeInfo);
|
||||||
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);
|
|
||||||
}
|
|
||||||
modules.Add(LoadInternal(module, typeInfo));
|
modules.Add(LoadInternal(module, typeInfo));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -102,5 +114,20 @@ namespace Discord.Commands
|
|||||||
_moduleLock.Release();
|
_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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ namespace Discord.Net.Queue
|
|||||||
_globalLimits = new Dictionary<GlobalBucket, BucketDefinition>
|
_globalLimits = new Dictionary<GlobalBucket, BucketDefinition>
|
||||||
{
|
{
|
||||||
//REST
|
//REST
|
||||||
[GlobalBucket.GeneralRest] = new BucketDefinition(0, 0),
|
[GlobalBucket.GeneralRest] = new BucketDefinition(0, 0), //No Limit
|
||||||
//[GlobalBucket.Login] = new BucketDefinition(1, 1),
|
//[GlobalBucket.Login] = new BucketDefinition(1, 1),
|
||||||
[GlobalBucket.DirectMessage] = new BucketDefinition(5, 5),
|
[GlobalBucket.DirectMessage] = new BucketDefinition(5, 5),
|
||||||
[GlobalBucket.SendEditMessage] = new BucketDefinition(50, 10),
|
[GlobalBucket.SendEditMessage] = new BucketDefinition(50, 10),
|
||||||
|
|||||||
Reference in New Issue
Block a user