From 63e70468e1f30af026901d179b8661d4add8a34c Mon Sep 17 00:00:00 2001 From: RogueException Date: Sun, 8 Nov 2015 17:02:22 -0400 Subject: [PATCH] Added Module.CreateCommands, exposed a few properties and added module names. --- src/Discord.Net.Modules/ModuleManager.cs | 31 ++++++++++++++++++++---- src/Discord.Net.Modules/ModuleService.cs | 6 ++--- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Discord.Net.Modules/ModuleManager.cs b/src/Discord.Net.Modules/ModuleManager.cs index 74fb76f1e..2697b5bcb 100644 --- a/src/Discord.Net.Modules/ModuleManager.cs +++ b/src/Discord.Net.Modules/ModuleManager.cs @@ -1,4 +1,5 @@ -using System; +using Discord.Commands; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; @@ -43,21 +44,29 @@ namespace Discord.Modules public event EventHandler MessageReadRemotely; private readonly DiscordClient _client; + private readonly string _name, _id; + private readonly FilterType _filterType; private readonly bool _allowServerWhitelist, _allowChannelWhitelist, _allowPrivate; private readonly ConcurrentDictionary _enabledServers; private readonly ConcurrentDictionary _enabledChannels; private readonly ConcurrentDictionary _indirectServers; public DiscordClient Client => _client; + public string Name => _name; + public string Id => _id; + public FilterType FilterType => _filterType; public IEnumerable EnabledServers => _enabledServers.Select(x => x.Value); public IEnumerable EnabledChannels => _enabledChannels.Select(x => x.Value); - internal ModuleManager(DiscordClient client, FilterType type) + internal ModuleManager(DiscordClient client, string name, FilterType filterType) { _client = client; - _allowServerWhitelist = type.HasFlag(FilterType.Server); - _allowChannelWhitelist = type.HasFlag(FilterType.Channel); - _allowPrivate = type.HasFlag(FilterType.AllowPrivate); + _name = name; + _id = name.ToLowerInvariant(); + _filterType = filterType; + _allowServerWhitelist = filterType.HasFlag(FilterType.Server); + _allowChannelWhitelist = filterType.HasFlag(FilterType.Channel); + _allowPrivate = filterType.HasFlag(FilterType.AllowPrivate); _enabledServers = new ConcurrentDictionary(); _enabledChannels = new ConcurrentDictionary(); @@ -98,6 +107,18 @@ namespace Discord.Modules } } + public void CreateCommands(string prefix, Action config) + { + var commandService = _client.Commands(); + if (commandService == null) + throw new InvalidOperationException($"{nameof(CommandService)} must be added to DiscordClient before this property is accessed."); + commandService.CreateGroup(prefix, x => + { + x.Category(_name); + config(x); + }); + + } public bool EnableServer(Server server) { if (server == null) throw new ArgumentNullException(nameof(server)); diff --git a/src/Discord.Net.Modules/ModuleService.cs b/src/Discord.Net.Modules/ModuleService.cs index 6483d7b01..bf90bb3dc 100644 --- a/src/Discord.Net.Modules/ModuleService.cs +++ b/src/Discord.Net.Modules/ModuleService.cs @@ -8,7 +8,7 @@ namespace Discord.Modules private DiscordClient _client; //ModuleServiceConfig Config { get; } - public IEnumerable Modules => _modules.Keys; + public IEnumerable Modules => _modules.Values; private readonly Dictionary _modules; public ModuleService(/*ModuleServiceConfig config*/) @@ -22,12 +22,12 @@ namespace Discord.Modules _client = client; } - public void Install(IModule module, FilterType type) + public void Install(IModule module, string name, FilterType type) { if (_client == null) throw new InvalidOperationException("Service needs to be added to a DiscordClient before modules can be installed."); if (_modules.ContainsKey(module)) throw new InvalidOperationException("This module has already been added."); - var manager = new ModuleManager(_client, type); + var manager = new ModuleManager(_client, name, type); _modules.Add(module, manager); module.Install(manager); }