| @@ -2,7 +2,7 @@ | |||||
| { | { | ||||
| public static class CommandExtensions | 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); | |||||
| } | } | ||||
| } | } | ||||
| @@ -1,6 +1,4 @@ | |||||
| using System; | |||||
| namespace Discord.Commands.Permissions.Levels | |||||
| namespace Discord.Commands.Permissions.Levels | |||||
| { | { | ||||
| public class PermissionLevelChecker : IPermissionChecker | public class PermissionLevelChecker : IPermissionChecker | ||||
| { | { | ||||
| @@ -12,10 +10,8 @@ namespace Discord.Commands.Permissions.Levels | |||||
| internal PermissionLevelChecker(DiscordClient client, int minPermissions) | internal PermissionLevelChecker(DiscordClient client, int minPermissions) | ||||
| { | { | ||||
| _service = client.GetService<PermissionLevelService>(); | |||||
| _service = client.GetService<PermissionLevelService>(true); | |||||
| _minPermissions = minPermissions; | _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) | public bool CanRun(Command command, User user, Channel channel) | ||||
| @@ -1,6 +1,4 @@ | |||||
| using System; | |||||
| namespace Discord.Commands.Permissions.Userlist | |||||
| namespace Discord.Commands.Permissions.Userlist | |||||
| { | { | ||||
| public class BlacklistChecker : IPermissionChecker | public class BlacklistChecker : IPermissionChecker | ||||
| { | { | ||||
| @@ -8,9 +6,7 @@ namespace Discord.Commands.Permissions.Userlist | |||||
| internal BlacklistChecker(DiscordClient client) | 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) | public bool CanRun(Command command, User user, Channel channel) | ||||
| @@ -1,6 +1,4 @@ | |||||
| using System; | |||||
| namespace Discord.Commands.Permissions.Userlist | |||||
| namespace Discord.Commands.Permissions.Userlist | |||||
| { | { | ||||
| public class WhitelistChecker : IPermissionChecker | public class WhitelistChecker : IPermissionChecker | ||||
| { | { | ||||
| @@ -8,9 +6,7 @@ namespace Discord.Commands.Permissions.Userlist | |||||
| internal WhitelistChecker(DiscordClient client) | 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) | public bool CanRun(Command command, User user, Channel channel) | ||||
| @@ -2,7 +2,7 @@ | |||||
| { | { | ||||
| public static class ModuleExtensions | 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); | |||||
| } | } | ||||
| } | } | ||||
| @@ -109,9 +109,7 @@ namespace Discord.Modules | |||||
| public void CreateCommands(string prefix, Action<CommandGroupBuilder> config) | 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 => | commandService.CreateGroup(prefix, x => | ||||
| { | { | ||||
| x.Category(_name); | x.Category(_name); | ||||
| @@ -280,15 +280,20 @@ namespace Discord | |||||
| obj.Install(this); | obj.Install(this); | ||||
| return obj; | return obj; | ||||
| } | } | ||||
| public T GetService<T>() | |||||
| public T GetService<T>(bool required = true) | |||||
| where T : class, IService | where T : class, IService | ||||
| { | { | ||||
| IService service; | IService service; | ||||
| T serviceT = null; | |||||
| if (_services.TryGetValue(typeof(T), out service)) | if (_services.TryGetValue(typeof(T), out service)) | ||||
| return service as T; | |||||
| serviceT = service as T; | |||||
| else | else | ||||
| return null; | 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() | protected override IEnumerable<Task> GetTasks() | ||||
| { | { | ||||