/// <summary> /// Executes this command with specified context. /// </summary> /// <param name="ctx">Context to execute the command in.</param> /// <returns>Command's execution results.</returns> public virtual async Task <CommandResult> ExecuteAsync(CommandContext ctx) { var args = CommandsNextUtilities.BindArguments(ctx); try { var ret = (Task)this.Callable.DynamicInvoke(args); await ret; } catch (Exception ex) { return(new CommandResult { IsSuccessful = false, Exception = ex, Context = ctx }); } return(new CommandResult { IsSuccessful = true, Context = ctx }); }
/// <summary> /// Executes this command with specified context. /// </summary> /// <param name="ctx">Context to execute the command in.</param> /// <returns>Command's execution results.</returns> public virtual async Task <CommandResult> ExecuteAsync(CommandContext ctx) { try { var args = await CommandsNextUtilities.BindArguments(ctx, ctx.Config.IgnoreExtraArguments); ctx.RawArguments = args.Raw; var ret = (Task)this.Callable.DynamicInvoke(args.Converted); await ret.ConfigureAwait(false); } catch (Exception ex) { return(new CommandResult { IsSuccessful = false, Exception = ex, Context = ctx }); } return(new CommandResult { IsSuccessful = true, Context = ctx }); }
/// <summary> /// Executes this command with specified context. /// </summary> /// <param name="ctx">Context to execute the command in.</param> /// <returns>Command's execution results.</returns> public virtual async Task <CommandResult> ExecuteAsync(CommandContext ctx) { CommandResult res = default; try { var executed = false; foreach (var ovl in this.Overloads.OrderByDescending(x => x.Priority)) { ctx.Overload = ovl; var args = await CommandsNextUtilities.BindArguments(ctx, ctx.Config.IgnoreExtraArguments).ConfigureAwait(false); if (!args.IsSuccessful) { continue; } ctx.RawArguments = args.Raw; var mdl = ovl.InvocationTarget ?? this.Module?.GetInstance(ctx.Services); if (mdl is BaseCommandModule bcmBefore) { await bcmBefore.BeforeExecutionAsync(ctx).ConfigureAwait(false); } args.Converted[0] = mdl; var ret = (Task)ovl.Callable.DynamicInvoke(args.Converted); await ret.ConfigureAwait(false); executed = true; res = new CommandResult { IsSuccessful = true, Context = ctx }; if (mdl is BaseCommandModule bcmAfter) { await bcmAfter.AfterExecutionAsync(ctx).ConfigureAwait(false); } break; } if (!executed) { throw new ArgumentException("Could not find a suitable overload for the command."); } } catch (Exception ex) { res = new CommandResult { IsSuccessful = false, Exception = ex, Context = ctx }; } return(res); }
/// <summary> /// Executes this command with specified context. /// </summary> /// <param name="ctx">Context to execute the command in.</param> /// <returns>Command's execution results.</returns> public virtual async Task <CommandResult> ExecuteAsync(CommandContext ctx) { CommandResult res = default; try { var executed = false; foreach (var ovl in this.Overloads.OrderByDescending(x => x.Priority)) { ctx.Overload = ovl; var args = await CommandsNextUtilities.BindArguments(ctx, ctx.Config.IgnoreExtraArguments).ConfigureAwait(false); if (!args.IsSuccessful) { continue; } ctx.RawArguments = args.Raw; IServiceScope scope = null; if (this.Module is TransientCommandModule) { scope = ctx.Services.CreateScope(); ctx.ServiceScopeContext = new CommandContext.ServiceContext(ctx.Services, scope); ctx.Services = scope.ServiceProvider; } var mdl = this.Module.GetInstance(ctx.Services); await mdl.BeforeExecutionAsync(ctx).ConfigureAwait(false); args.Converted[0] = mdl; var ret = (Task)ovl.Callable.DynamicInvoke(args.Converted); await ret.ConfigureAwait(false); executed = true; res = new CommandResult { IsSuccessful = true, Context = ctx }; await mdl.AfterExecutionAsync(ctx).ConfigureAwait(false); break; } if (!executed) { throw new ArgumentException("Could not find a suitable overload for the command."); } } catch (Exception ex) { res = new CommandResult { IsSuccessful = false, Exception = ex, Context = ctx }; } return(res); }