Cleaned up a bit of the modules/services add/get format
This commit is contained in:
@@ -4,24 +4,22 @@
|
||||
{
|
||||
public static DiscordClient UsingModules(this DiscordClient client)
|
||||
{
|
||||
client.Services.Add(new ModuleService());
|
||||
client.AddService(new ModuleService());
|
||||
return client;
|
||||
}
|
||||
|
||||
public static DiscordClient AddModule<T>(this DiscordClient client, T instance, string name = null, ModuleFilter filter = ModuleFilter.None)
|
||||
public static void AddModule<T>(this DiscordClient client, T instance, string name = null, ModuleFilter filter = ModuleFilter.None)
|
||||
where T : class, IModule
|
||||
{
|
||||
client.Modules().Add(instance, name ?? nameof(T), filter);
|
||||
return client;
|
||||
client.GetService<ModuleService>().Add(instance, name ?? nameof(T), filter);
|
||||
}
|
||||
public static DiscordClient AddModule<T>(this DiscordClient client, string name = null, ModuleFilter filter = ModuleFilter.None)
|
||||
public static void AddModule<T>(this DiscordClient client, string name = null, ModuleFilter filter = ModuleFilter.None)
|
||||
where T : class, IModule, new()
|
||||
{
|
||||
client.Modules().Add(new T(), name ?? nameof(T), filter);
|
||||
return client;
|
||||
client.GetService<ModuleService>().Add(new T(), name ?? nameof(T), filter);
|
||||
}
|
||||
|
||||
public static ModuleService Modules(this DiscordClient client, bool required = true)
|
||||
=> client.Services.Get<ModuleService>(required);
|
||||
public static ModuleManager<T> GetModule<T>(this DiscordClient client)
|
||||
where T : class, IModule
|
||||
=> client.GetService<ModuleService>().Get<T>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,17 @@ using System.Linq;
|
||||
|
||||
namespace Discord.Modules
|
||||
{
|
||||
public class ModuleManager<T> : ModuleManager
|
||||
where T : class, IModule
|
||||
{
|
||||
public new T Instance => base.Instance as T;
|
||||
|
||||
internal ModuleManager(DiscordClient client, T instance, string name, ModuleFilter filterType)
|
||||
: base(client, instance, name, filterType)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class ModuleManager
|
||||
{
|
||||
public event EventHandler<ServerEventArgs> ServerEnabled = delegate { };
|
||||
@@ -115,7 +126,7 @@ namespace Discord.Modules
|
||||
|
||||
public void CreateCommands(string prefix, Action<CommandGroupBuilder> config)
|
||||
{
|
||||
var commandService = Client.Services.Get<CommandService>();
|
||||
var commandService = Client.GetService<CommandService>();
|
||||
commandService.CreateGroup(prefix, x =>
|
||||
{
|
||||
x.Category(Name);
|
||||
|
||||
@@ -8,11 +8,11 @@ namespace Discord.Modules
|
||||
public DiscordClient Client { get; private set; }
|
||||
|
||||
public IEnumerable<ModuleManager> Modules => _modules.Values;
|
||||
private readonly Dictionary<IModule, ModuleManager> _modules;
|
||||
private readonly Dictionary<Type, ModuleManager> _modules;
|
||||
|
||||
public ModuleService()
|
||||
{
|
||||
_modules = new Dictionary<IModule, ModuleManager>();
|
||||
_modules = new Dictionary<Type, ModuleManager>();
|
||||
}
|
||||
|
||||
void IService.Install(DiscordClient client)
|
||||
@@ -20,29 +20,30 @@ namespace Discord.Modules
|
||||
Client = client;
|
||||
}
|
||||
|
||||
public T Add<T>(T module, string name, ModuleFilter type)
|
||||
public T Add<T>(T module, string name, ModuleFilter filterType)
|
||||
where T : class, IModule
|
||||
{
|
||||
if (module == null) throw new ArgumentNullException(nameof(module));
|
||||
if (name == null) throw new ArgumentNullException(nameof(name));
|
||||
if (Client == null)
|
||||
throw new InvalidOperationException("Service needs to be added to a DiscordClient before modules can be installed.");
|
||||
if (_modules.ContainsKey(module))
|
||||
|
||||
Type type = typeof(T);
|
||||
if (_modules.ContainsKey(type))
|
||||
throw new InvalidOperationException("This module has already been added.");
|
||||
|
||||
var manager = new ModuleManager(Client, module, name, type);
|
||||
_modules.Add(module, manager);
|
||||
var manager = new ModuleManager<T>(Client, module, name, filterType);
|
||||
_modules.Add(type, manager);
|
||||
module.Install(manager);
|
||||
return module;
|
||||
}
|
||||
|
||||
public ModuleManager GetManager(IModule module)
|
||||
{
|
||||
if (module == null) throw new ArgumentNullException(nameof(module));
|
||||
|
||||
ModuleManager result = null;
|
||||
_modules.TryGetValue(module, out result);
|
||||
return result;
|
||||
}
|
||||
public ModuleManager<T> Get<T>()
|
||||
where T : class, IModule
|
||||
{
|
||||
ModuleManager manager;
|
||||
if (_modules.TryGetValue(typeof(T), out manager))
|
||||
return manager as ModuleManager<T>;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
|
||||
"dependencies": {
|
||||
"Discord.Net": "0.9.0-rc3-3",
|
||||
"Discord.Net.Commands": "0.9.0-rc3"
|
||||
"Discord.Net.Commands": "0.9.0-rc3-1"
|
||||
},
|
||||
"frameworks": {
|
||||
"net45": { },
|
||||
|
||||
Reference in New Issue
Block a user