From f9c5e229d0fb5b66939d7703a3c109c70c7b3605 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Fri, 25 Nov 2016 18:49:35 -0500 Subject: [PATCH] Add CommandServiceConfig, DefaultRunMode This adds an (optional) CommandServiceConfig, as well as a DefaultRunMode for commands. This resolves #368 (for commands where a RunMode is not explicitly specified, a custom default value should be used) --- src/Discord.Net.Commands/Attributes/CommandAttribute.cs | 2 +- src/Discord.Net.Commands/Builders/CommandBuilder.cs | 2 +- src/Discord.Net.Commands/CommandService.cs | 6 +++++- src/Discord.Net.Commands/CommandServiceConfig.cs | 8 ++++++++ src/Discord.Net.Commands/Info/CommandInfo.cs | 2 +- 5 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 src/Discord.Net.Commands/CommandServiceConfig.cs diff --git a/src/Discord.Net.Commands/Attributes/CommandAttribute.cs b/src/Discord.Net.Commands/Attributes/CommandAttribute.cs index baac75ff9..86daf0103 100644 --- a/src/Discord.Net.Commands/Attributes/CommandAttribute.cs +++ b/src/Discord.Net.Commands/Attributes/CommandAttribute.cs @@ -6,7 +6,7 @@ namespace Discord.Commands public class CommandAttribute : Attribute { public string Text { get; } - public RunMode RunMode { get; set; } = RunMode.Sync; + public RunMode RunMode { get; set; } public CommandAttribute() { diff --git a/src/Discord.Net.Commands/Builders/CommandBuilder.cs b/src/Discord.Net.Commands/Builders/CommandBuilder.cs index 9b983fd1f..25c0223e6 100644 --- a/src/Discord.Net.Commands/Builders/CommandBuilder.cs +++ b/src/Discord.Net.Commands/Builders/CommandBuilder.cs @@ -17,7 +17,7 @@ namespace Discord.Commands.Builders public string Name { get; set; } public string Summary { get; set; } public string Remarks { get; set; } - public RunMode RunMode { get; set; } + public RunMode? RunMode { get; set; } public int Priority { get; set; } public IReadOnlyList Preconditions => _preconditions; diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index 3c3760908..376eca84e 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -19,10 +19,13 @@ namespace Discord.Commands private readonly ConcurrentBag _moduleDefs; private readonly CommandMap _map; + internal readonly RunMode _defaultRunMode; + public IEnumerable Modules => _typedModuleDefs.Select(x => x.Value); public IEnumerable Commands => _typedModuleDefs.SelectMany(x => x.Value.Commands); - public CommandService() + public CommandService() : this(new CommandServiceConfig()) { } + public CommandService(CommandServiceConfig config) { _moduleLock = new SemaphoreSlim(1, 1); _typedModuleDefs = new ConcurrentDictionary(); @@ -64,6 +67,7 @@ namespace Discord.Commands [typeof(IGroupUser)] = new UserTypeReader(), [typeof(IGuildUser)] = new UserTypeReader(), }; + _defaultRunMode = config.DefaultRunMode; } //Modules diff --git a/src/Discord.Net.Commands/CommandServiceConfig.cs b/src/Discord.Net.Commands/CommandServiceConfig.cs new file mode 100644 index 000000000..97c98a54c --- /dev/null +++ b/src/Discord.Net.Commands/CommandServiceConfig.cs @@ -0,0 +1,8 @@ +namespace Discord.Commands +{ + public class CommandServiceConfig + { + /// The default RunMode commands should have, if one is not specified on the Command attribute or builder. + public RunMode DefaultRunMode { get; set; } = RunMode.Mixed; + } +} diff --git a/src/Discord.Net.Commands/Info/CommandInfo.cs b/src/Discord.Net.Commands/Info/CommandInfo.cs index 4d546b6fa..a9a3c2415 100644 --- a/src/Discord.Net.Commands/Info/CommandInfo.cs +++ b/src/Discord.Net.Commands/Info/CommandInfo.cs @@ -37,7 +37,7 @@ namespace Discord.Commands Summary = builder.Summary; Remarks = builder.Remarks; - RunMode = builder.RunMode; + RunMode = builder.RunMode ?? service._defaultRunMode; Priority = builder.Priority; if (module.Aliases.Count != 0)