public Result <CommandArgumentContainer> IsCorrectArgumentCount(CommandArgumentContainer args) { Result <BotCommandDescriptor> commandTask = _commands.GetCommand(args.CommandName); if (commandTask.IsFailed) { return(commandTask.ToResult <CommandArgumentContainer>()); } return(commandTask.Value.Args.Length == args.Arguments.Count ? Result.Ok(args) : Result.Fail <CommandArgumentContainer>("Cannot execute command. Argument count miss matched with command signature")); }
public Result <CommandArgumentContainer> IsCommandCanBeExecuted(CommandArgumentContainer args) { Result <BotCommandDescriptor> commandTask = _commands.GetCommand(args.CommandName); if (commandTask.IsFailed) { return(commandTask.ToResult <CommandArgumentContainer>()); } IBotCommand command = commandTask.Value.ResolveCommand(_serviceProvider); Result canExecute = command.CanExecute(args); return(canExecute.IsSuccess ? Result.Ok(args) : Result.Fail <CommandArgumentContainer>($"Command [{commandTask.Value.CommandName}] cannot be executed: {canExecute}")); }
public Result <IBotMessage> ExecuteCommand(CommandArgumentContainer args) { Result <BotCommandDescriptor> commandDescriptor = _commands.GetCommand(args.CommandName); if (!commandDescriptor.IsSuccess) { return(commandDescriptor.ToResult <IBotMessage>()); } try { IBotCommand command = commandDescriptor.Value.ResolveCommand(_serviceProvider); return(command switch { IBotAsyncCommand asyncCommand => asyncCommand.Execute(args).Result, IBotSyncCommand syncCommand => syncCommand.Execute(args), _ => Result.Fail(new Error("Command execution failed. Wrong command inheritance.")) }); }