From c7ac85455f9dfc8a22dcc28c00e7fcc28f1ad5f9 Mon Sep 17 00:00:00 2001 From: RogueException Date: Mon, 10 Oct 2016 21:16:34 -0300 Subject: [PATCH] Added a configurable run mode for commands --- .../Attributes/CommandAttribute.cs | 1 + src/Discord.Net.Commands/CommandInfo.cs | 16 +++++++++++++++- src/Discord.Net.Commands/RunMode.cs | 9 +++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/Discord.Net.Commands/RunMode.cs diff --git a/src/Discord.Net.Commands/Attributes/CommandAttribute.cs b/src/Discord.Net.Commands/Attributes/CommandAttribute.cs index 014668405..baac75ff9 100644 --- a/src/Discord.Net.Commands/Attributes/CommandAttribute.cs +++ b/src/Discord.Net.Commands/Attributes/CommandAttribute.cs @@ -6,6 +6,7 @@ namespace Discord.Commands public class CommandAttribute : Attribute { public string Text { get; } + public RunMode RunMode { get; set; } = RunMode.Sync; public CommandAttribute() { diff --git a/src/Discord.Net.Commands/CommandInfo.cs b/src/Discord.Net.Commands/CommandInfo.cs index e63fb6d3f..4a4980d21 100644 --- a/src/Discord.Net.Commands/CommandInfo.cs +++ b/src/Discord.Net.Commands/CommandInfo.cs @@ -25,6 +25,7 @@ namespace Discord.Commands public string Text { get; } public int Priority { get; } public bool HasVarArgs { get; } + public RunMode RunMode { get; } public IReadOnlyList Aliases { get; } public IReadOnlyList Parameters { get; } public IReadOnlyList Preconditions { get; } @@ -40,6 +41,7 @@ namespace Discord.Commands if (attribute.Text == null) Text = groupPrefix; + RunMode = attribute.RunMode; if (groupPrefix != "") groupPrefix += " "; @@ -150,7 +152,19 @@ namespace Discord.Commands { try { - await _action.Invoke(context, GenerateArgs(argList, paramList)).ConfigureAwait(false);//Note: This code may need context + var args = GenerateArgs(argList, paramList); + switch (RunMode) + { + case RunMode.Sync: + await _action(context, args).ConfigureAwait(false); + break; + case RunMode.Async: + var t1 = _action(context, args); + break; + case RunMode.FireAndForget: + var t2 = Task.Run(() => _action(context, args)); + break; + } return ExecuteResult.FromSuccess(); } catch (Exception ex) diff --git a/src/Discord.Net.Commands/RunMode.cs b/src/Discord.Net.Commands/RunMode.cs new file mode 100644 index 000000000..0acdc1629 --- /dev/null +++ b/src/Discord.Net.Commands/RunMode.cs @@ -0,0 +1,9 @@ +namespace Discord.Commands +{ + public enum RunMode + { + Sync, + Async, + FireAndForget + } +}