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.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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(IModule module, string name, FilterType type)
  21. {
  22. if (_client == null) throw new InvalidOperationException("Service needs to be added to a DiscordClient before modules can be installed.");
  23. if (_modules.ContainsKey(module)) throw new InvalidOperationException("This module has already been added.");
  24. var manager = new ModuleManager(_client, name, type);
  25. _modules.Add(module, manager);
  26. module.Install(manager);
  27. }
  28. public ModuleManager GetManager(IModule module)
  29. {
  30. if (module == null) throw new ArgumentNullException(nameof(module));
  31. ModuleManager result = null;
  32. _modules.TryGetValue(module, out result);
  33. return result;
  34. }
  35. }
  36. }