Added required parameter to GetService

This commit is contained in:
RogueException
2015-11-09 01:35:02 -04:00
parent 093095e410
commit 2e4f880563
7 changed files with 19 additions and 28 deletions

View File

@@ -2,7 +2,7 @@
{
public static class CommandExtensions
{
public static CommandService Commands(this DiscordClient client)
=> client.GetService<CommandService>();
public static CommandService Commands(this DiscordClient client, bool required = true)
=> client.GetService<CommandService>(required);
}
}

View File

@@ -1,6 +1,4 @@
using System;
namespace Discord.Commands.Permissions.Levels
namespace Discord.Commands.Permissions.Levels
{
public class PermissionLevelChecker : IPermissionChecker
{
@@ -12,10 +10,8 @@ namespace Discord.Commands.Permissions.Levels
internal PermissionLevelChecker(DiscordClient client, int minPermissions)
{
_service = client.GetService<PermissionLevelService>();
_service = client.GetService<PermissionLevelService>(true);
_minPermissions = minPermissions;
if (_service == null)
throw new InvalidOperationException($"{nameof(PermissionLevelService)} must be added to {nameof(DiscordClient)} before this function is called.");
}
public bool CanRun(Command command, User user, Channel channel)

View File

@@ -1,6 +1,4 @@
using System;
namespace Discord.Commands.Permissions.Userlist
namespace Discord.Commands.Permissions.Userlist
{
public class BlacklistChecker : IPermissionChecker
{
@@ -8,9 +6,7 @@ namespace Discord.Commands.Permissions.Userlist
internal BlacklistChecker(DiscordClient client)
{
_service = client.GetService<BlacklistService>();
if (_service == null)
throw new InvalidOperationException($"{nameof(BlacklistService)} must be added to {nameof(DiscordClient)} before this function is called.");
_service = client.GetService<BlacklistService>(true);
}
public bool CanRun(Command command, User user, Channel channel)

View File

@@ -1,6 +1,4 @@
using System;
namespace Discord.Commands.Permissions.Userlist
namespace Discord.Commands.Permissions.Userlist
{
public class WhitelistChecker : IPermissionChecker
{
@@ -8,9 +6,7 @@ namespace Discord.Commands.Permissions.Userlist
internal WhitelistChecker(DiscordClient client)
{
_service = client.GetService<WhitelistService>();
if (_service == null)
throw new InvalidOperationException($"{nameof(WhitelistService)} must be added to {nameof(DiscordClient)} before this function is called.");
_service = client.GetService<WhitelistService>(true);
}
public bool CanRun(Command command, User user, Channel channel)

View File

@@ -2,7 +2,7 @@
{
public static class ModuleExtensions
{
public static ModuleService Modules(this DiscordClient client)
=> client.GetService<ModuleService>();
public static ModuleService Modules(this DiscordClient client, bool required = true)
=> client.GetService<ModuleService>(required);
}
}

View File

@@ -109,9 +109,7 @@ 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.");
var commandService = _client.Commands(true);
commandService.CreateGroup(prefix, x =>
{
x.Category(_name);

View File

@@ -280,15 +280,20 @@ namespace Discord
obj.Install(this);
return obj;
}
public T GetService<T>()
public T GetService<T>(bool required = true)
where T : class, IService
{
IService service;
T serviceT = null;
if (_services.TryGetValue(typeof(T), out service))
return service as T;
serviceT = service as T;
else
return null;
}
if (serviceT == null && required)
throw new InvalidOperationException($"This operation requires {nameof(T)} to be added to {nameof(DiscordClient)}.");
return serviceT;
}
protected override IEnumerable<Task> GetTasks()
{