Cleaned up extension projects, added fluent extension methods

This commit is contained in:
RogueException
2016-01-05 00:11:35 -04:00
parent 55ed6cd783
commit 55c5f0dade
29 changed files with 303 additions and 343 deletions

View File

@@ -6,7 +6,7 @@ namespace Discord.Modules
public class ModuleChecker : IPermissionChecker
{
private readonly ModuleManager _manager;
private readonly FilterType _filterType;
private readonly ModuleFilter _filterType;
internal ModuleChecker(ModuleManager manager)
{
@@ -16,7 +16,7 @@ namespace Discord.Modules
public bool CanRun(Command command, User user, Channel channel, out string error)
{
if (_filterType == FilterType.Unrestricted || _filterType == FilterType.AllowPrivate || _manager.HasChannel(channel))
if (_filterType == ModuleFilter.None || _filterType == ModuleFilter.AlwaysAllowPrivate || _manager.HasChannel(channel))
{
error = null;
return true;

View File

@@ -2,7 +2,25 @@
{
public static class ModuleExtensions
{
public static ModuleService Modules(this DiscordClient client, bool required = true)
public static DiscordClient UsingModules(this DiscordClient client)
{
client.Services.Add(new ModuleService());
return client;
}
public static DiscordClient 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;
}
public static DiscordClient 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;
}
public static ModuleService Modules(this DiscordClient client, bool required = true)
=> client.Services.Get<ModuleService>(required);
}
}

View File

@@ -3,15 +3,15 @@
namespace Discord.Modules
{
[Flags]
public enum FilterType
public enum ModuleFilter
{
/// <summary> Disables the event and command filtesr. </summary>
Unrestricted = 0x0,
/// <summary> Disables the event and command filters. </summary>
None = 0x0,
/// <summary> Uses the server whitelist to filter events and commands. </summary>
ServerWhitelist = 0x1,
/// <summary> Uses the channel whitelist to filter events and commands. </summary>
ChannelWhitelist = 0x2,
/// <summary> Enables this module in all private messages. </summary>
AllowPrivate = 0x4
AlwaysAllowPrivate = 0x4
}
}

View File

@@ -41,10 +41,7 @@ namespace Discord.Modules
public event EventHandler<MessageEventArgs> MessageDeleted = delegate { };
public event EventHandler<MessageEventArgs> MessageUpdated = delegate { };
public event EventHandler<MessageEventArgs> MessageReadRemotely = delegate { };
private readonly DiscordClient _client;
private readonly string _name, _id;
private readonly FilterType _filterType;
private readonly bool _useServerWhitelist, _useChannelWhitelist, _allowAll, _allowPrivate;
private readonly ConcurrentDictionary<ulong, Server> _enabledServers;
private readonly ConcurrentDictionary<ulong, Channel> _enabledChannels;
@@ -55,12 +52,12 @@ namespace Discord.Modules
public IModule Instance { get; }
public string Name { get; }
public string Id { get; }
public FilterType FilterType { get; }
public ModuleFilter FilterType { get; }
public IEnumerable<Server> EnabledServers => _enabledServers.Select(x => x.Value);
public IEnumerable<Channel> EnabledChannels => _enabledChannels.Select(x => x.Value);
internal ModuleManager(DiscordClient client, IModule instance, string name, FilterType filterType)
internal ModuleManager(DiscordClient client, IModule instance, string name, ModuleFilter filterType)
{
Client = client;
Instance = instance;
@@ -70,10 +67,10 @@ namespace Discord.Modules
Id = name.ToLowerInvariant();
_lock = new AsyncLock();
_allowAll = filterType == FilterType.Unrestricted;
_useServerWhitelist = filterType.HasFlag(FilterType.ServerWhitelist);
_useChannelWhitelist = filterType.HasFlag(FilterType.ChannelWhitelist);
_allowPrivate = filterType.HasFlag(FilterType.AllowPrivate);
_allowAll = filterType == ModuleFilter.None;
_useServerWhitelist = filterType.HasFlag(ModuleFilter.ServerWhitelist);
_useChannelWhitelist = filterType.HasFlag(ModuleFilter.ChannelWhitelist);
_allowPrivate = filterType.HasFlag(ModuleFilter.AlwaysAllowPrivate);
_enabledServers = new ConcurrentDictionary<ulong, Server>();
_enabledChannels = new ConcurrentDictionary<ulong, Channel>();
@@ -115,10 +112,10 @@ namespace Discord.Modules
public void CreateCommands(string prefix, Action<CommandGroupBuilder> config)
{
var commandService = _client.Commands(true);
var commandService = Client.Commands(true);
commandService.CreateGroup(prefix, x =>
{
x.Category(_name);
x.Category(Name);
x.AddCheck(new ModuleChecker(this));
config(x);
});

View File

@@ -20,7 +20,7 @@ namespace Discord.Modules
Client = client;
}
public void Install<T>(T module, string name, FilterType type)
public T Add<T>(T module, string name, ModuleFilter type)
where T : class, IModule
{
if (module == null) throw new ArgumentNullException(nameof(module));
@@ -33,6 +33,7 @@ namespace Discord.Modules
var manager = new ModuleManager(Client, module, name, type);
_modules.Add(module, manager);
module.Install(manager);
return module;
}
public ModuleManager GetManager(IModule module)