Overloaded AddModuleAsync with Type (#581)
* Overloaded AddModuleAsync with Type * Overloaded RemoveModuleAsync with Type * Use expression-bodied method for consistency
This commit is contained in:
@@ -83,20 +83,21 @@ namespace Discord.Commands
|
|||||||
_moduleLock.Release();
|
_moduleLock.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public async Task<ModuleInfo> AddModuleAsync<T>()
|
public Task<ModuleInfo> AddModuleAsync<T>() => AddModuleAsync(typeof(T));
|
||||||
|
public async Task<ModuleInfo> AddModuleAsync(Type type)
|
||||||
{
|
{
|
||||||
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var typeInfo = typeof(T).GetTypeInfo();
|
var typeInfo = type.GetTypeInfo();
|
||||||
|
|
||||||
if (_typedModuleDefs.ContainsKey(typeof(T)))
|
if (_typedModuleDefs.ContainsKey(type))
|
||||||
throw new ArgumentException($"This module has already been added.");
|
throw new ArgumentException($"This module has already been added.");
|
||||||
|
|
||||||
var module = ModuleClassBuilder.Build(this, typeInfo).FirstOrDefault();
|
var module = ModuleClassBuilder.Build(this, typeInfo).FirstOrDefault();
|
||||||
|
|
||||||
if (module.Value == default(ModuleInfo))
|
if (module.Value == default(ModuleInfo))
|
||||||
throw new InvalidOperationException($"Could not build the module {typeof(T).FullName}, did you pass an invalid type?");
|
throw new InvalidOperationException($"Could not build the module {type.FullName}, did you pass an invalid type?");
|
||||||
|
|
||||||
_typedModuleDefs[module.Key] = module.Value;
|
_typedModuleDefs[module.Key] = module.Value;
|
||||||
|
|
||||||
@@ -153,13 +154,14 @@ namespace Discord.Commands
|
|||||||
_moduleLock.Release();
|
_moduleLock.Release();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public async Task<bool> RemoveModuleAsync<T>()
|
public Task<bool> RemoveModuleAsync<T>() => RemoveModuleAsync(typeof(T));
|
||||||
|
public async Task<bool> RemoveModuleAsync(Type type)
|
||||||
{
|
{
|
||||||
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
await _moduleLock.WaitAsync().ConfigureAwait(false);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ModuleInfo module;
|
ModuleInfo module;
|
||||||
if (!_typedModuleDefs.TryRemove(typeof(T), out module))
|
if (!_typedModuleDefs.TryRemove(type, out module))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return RemoveModuleInternal(module);
|
return RemoveModuleInternal(module);
|
||||||
|
|||||||
Reference in New Issue
Block a user