You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ModuleService.cs 1.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Discord.Modules
  4. {
  5. public class ModuleService : IService
  6. {
  7. private DiscordClient _client;
  8. //ModuleServiceConfig Config { get; }
  9. public IEnumerable<ModuleManager> Modules => _modules.Values;
  10. private readonly Dictionary<IModule, ModuleManager> _modules;
  11. public ModuleService(/*ModuleServiceConfig config*/)
  12. {
  13. //Config = config;
  14. _modules = new Dictionary<IModule, ModuleManager>();
  15. }
  16. void IService.Install(DiscordClient client)
  17. {
  18. _client = client;
  19. }
  20. public void Install<T>(T module, string name, FilterType type)
  21. where T : class, IModule
  22. {
  23. if (module == null) throw new ArgumentNullException(nameof(module));
  24. if (name == null) throw new ArgumentNullException(nameof(name));
  25. if (_client == null) throw new InvalidOperationException("Service needs to be added to a DiscordClient before modules can be installed.");
  26. if (_modules.ContainsKey(module)) throw new InvalidOperationException("This module has already been added.");
  27. var manager = new ModuleManager(_client, name, type);
  28. _modules.Add(module, manager);
  29. module.Install(manager);
  30. }
  31. public ModuleManager GetManager(IModule module)
  32. {
  33. if (module == null) throw new ArgumentNullException(nameof(module));
  34. ModuleManager result = null;
  35. _modules.TryGetValue(module, out result);
  36. return result;
  37. }
  38. }
  39. }