Browse Source

Added Module.CreateCommands, exposed a few properties and added module names.

tags/docs-0.9
RogueException 9 years ago
parent
commit
63e70468e1
2 changed files with 29 additions and 8 deletions
  1. +26
    -5
      src/Discord.Net.Modules/ModuleManager.cs
  2. +3
    -3
      src/Discord.Net.Modules/ModuleService.cs

+ 26
- 5
src/Discord.Net.Modules/ModuleManager.cs View File

@@ -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<MessageEventArgs> MessageReadRemotely;

private readonly DiscordClient _client;
private readonly string _name, _id;
private readonly FilterType _filterType;
private readonly bool _allowServerWhitelist, _allowChannelWhitelist, _allowPrivate;
private readonly ConcurrentDictionary<string, Server> _enabledServers;
private readonly ConcurrentDictionary<string, Channel> _enabledChannels;
private readonly ConcurrentDictionary<string, int> _indirectServers;

public DiscordClient Client => _client;
public string Name => _name;
public string Id => _id;
public FilterType FilterType => _filterType;
public IEnumerable<Server> EnabledServers => _enabledServers.Select(x => x.Value);
public IEnumerable<Channel> 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<string, Server>();
_enabledChannels = new ConcurrentDictionary<string, Channel>();
@@ -98,6 +107,18 @@ namespace Discord.Modules
}
}

public void CreateCommands(string prefix, Action<CommandGroupBuilder> 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));


+ 3
- 3
src/Discord.Net.Modules/ModuleService.cs View File

@@ -8,7 +8,7 @@ namespace Discord.Modules
private DiscordClient _client;

//ModuleServiceConfig Config { get; }
public IEnumerable<IModule> Modules => _modules.Keys;
public IEnumerable<ModuleManager> Modules => _modules.Values;
private readonly Dictionary<IModule, ModuleManager> _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);
}


Loading…
Cancel
Save