diff --git a/src/Discord.Net.Commands/Info/CommandInfo.cs b/src/Discord.Net.Commands/Info/CommandInfo.cs index d953013ab..26b6163ab 100644 --- a/src/Discord.Net.Commands/Info/CommandInfo.cs +++ b/src/Discord.Net.Commands/Info/CommandInfo.cs @@ -137,47 +137,15 @@ namespace Discord.Commands return ExecuteResult.FromError(result); } - await Module.Service._cmdLogger.DebugAsync($"Executing {GetLogText(context)}").ConfigureAwait(false); switch (RunMode) { case RunMode.Sync: //Always sync - try - { - await _action(context, args, map).ConfigureAwait(false); - } - catch (Exception ex) - { - ex = new CommandException(this, context, ex); - await Module.Service._cmdLogger.ErrorAsync(ex).ConfigureAwait(false); - throw; - } - await Module.Service._cmdLogger.VerboseAsync($"Executed {GetLogText(context)}").ConfigureAwait(false); - break; - case RunMode.Mixed: //Sync until first await statement - var t1 = _action(context, args, map).ContinueWith(async t => - { - if (t.IsFaulted) - { - var ex = new CommandException(this, context, t.Exception); - await Module.Service._cmdLogger.ErrorAsync(ex).ConfigureAwait(false); - } - else - await Module.Service._cmdLogger.VerboseAsync($"Executed {GetLogText(context)}").ConfigureAwait(false); - }); + var t1 = ExecuteAsyncInternal(context, args, map); break; case RunMode.Async: //Always async - var t2 = Task.Run(() => + var t2 = Task.Run(async () => { - var _ = _action(context, args, map).ContinueWith(async t => - { - if (t.IsFaulted) - { - var ex = new CommandException(this, context, t.Exception); - await Module.Service._cmdLogger.ErrorAsync(ex).ConfigureAwait(false); - } - else - await Module.Service._cmdLogger.VerboseAsync($"Executed {GetLogText(context)}").ConfigureAwait(false); - }); + await ExecuteAsyncInternal(context, args, map).ConfigureAwait(false); }); break; } @@ -189,6 +157,22 @@ namespace Discord.Commands } } + private async Task ExecuteAsyncInternal(ICommandContext context, object[] args, IDependencyMap map) + { + await Module.Service._cmdLogger.DebugAsync($"Executing {GetLogText(context)}").ConfigureAwait(false); + try + { + await _action(context, args, map).ConfigureAwait(false); + } + catch (Exception ex) + { + ex = new CommandException(this, context, ex); + await Module.Service._cmdLogger.ErrorAsync(ex).ConfigureAwait(false); + throw; + } + await Module.Service._cmdLogger.VerboseAsync($"Executed {GetLogText(context)}").ConfigureAwait(false); + } + private object[] GenerateArgs(IEnumerable argList, IEnumerable paramsList) { int argCount = Parameters.Count; diff --git a/src/Discord.Net.Commands/RunMode.cs b/src/Discord.Net.Commands/RunMode.cs index 2bb5dbbf6..ecb6a4b58 100644 --- a/src/Discord.Net.Commands/RunMode.cs +++ b/src/Discord.Net.Commands/RunMode.cs @@ -4,7 +4,6 @@ { Default, Sync, - Mixed, Async } }