Browse Source

Added a configurable run mode for commands

tags/1.0-rc
RogueException 8 years ago
parent
commit
c7ac85455f
3 changed files with 25 additions and 1 deletions
  1. +1
    -0
      src/Discord.Net.Commands/Attributes/CommandAttribute.cs
  2. +15
    -1
      src/Discord.Net.Commands/CommandInfo.cs
  3. +9
    -0
      src/Discord.Net.Commands/RunMode.cs

+ 1
- 0
src/Discord.Net.Commands/Attributes/CommandAttribute.cs View File

@@ -6,6 +6,7 @@ namespace Discord.Commands
public class CommandAttribute : Attribute
{
public string Text { get; }
public RunMode RunMode { get; set; } = RunMode.Sync;

public CommandAttribute()
{


+ 15
- 1
src/Discord.Net.Commands/CommandInfo.cs View File

@@ -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<string> Aliases { get; }
public IReadOnlyList<CommandParameter> Parameters { get; }
public IReadOnlyList<PreconditionAttribute> 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)


+ 9
- 0
src/Discord.Net.Commands/RunMode.cs View File

@@ -0,0 +1,9 @@
namespace Discord.Commands
{
public enum RunMode
{
Sync,
Async,
FireAndForget
}
}

Loading…
Cancel
Save