/// <summary> /// This is being called when the CommandService returned an unsuccessfull result. /// </summary> /// <param name="result">This represents the generic result returned by the command service. We'll check what was wrong.</param> /// <param name="ctx">This represents our context for this result.</param> private Task HandleErrorAsync(IResult result, SaltyCommandContext ctx) { _logger.Debug($"An error occured: {result}"); var errorBuilder = new StringBuilder(); var help = false; switch (result) { case ChecksFailedResult ex: ctx.Command = ex.Command; _logger.Debug("Some checks have failed: " + string.Join("\n", ex.FailedChecks.Select(x => x.Error))); break; case TypeParseFailedResult ex: errorBuilder.Append(ex.Reason); ctx.Command = ex.Parameter.Command; help = true; break; case CommandNotFoundResult ex: errorBuilder.Append($"The command was not found: {ctx.Input}"); break; case ArgumentParseFailedResult ex: ctx.Command = ex.Command; errorBuilder.Append($"The argument for the parameter {ex.Parameter.Name} was invalid."); help = true; break; case SaltyCommandResult ex: errorBuilder.Append($"{ctx.Command.Name}: {ex.Message}"); break; case OverloadsFailedResult ex: ctx.Command = ex.FailedOverloads.Select(x => x.Key).FirstOrDefault(); _logger.Debug($"Every overload failed: {string.Join("\n", ex.FailedOverloads.Select(x => x.Value.Reason))}"); errorBuilder.Append("Your command syntax was wrong."); help = true; break; } if (errorBuilder.Length == 0) { return(Task.CompletedTask); } ctx.Player.SendChatMessageAsync(errorBuilder.ToString(), SayColorType.Green); return(help ? _commands.ExecuteAsync($"help {ctx.Command.FullAliases[0]}", ctx) : Task.CompletedTask); }
/// <inheritdoc /> /// <summary> /// This is where every message from the InGame tchat starting with our prefix will arrive. /// In our case, the parameter message represents the raw message sent by the user. /// The parameter of type object would represent the instance of the entity that invoked the command. /// That method could be called on each messages sent in the in-game tchat. We will just check that it starts with our prefix ($). /// Then we will create a Context that will propagate onto the command. /// The CommandService will try to parse the message and find a command with the parsed arguments and will perform some checks, if necessary. /// </summary> /// <param name="message">It represents the already parsed command with its parameters.</param> /// <param name="entity">It represents the instance of the entity that performed the action of sending a message.</param> public async Task HandleMessageAsync(string message, object entity) { if (!(entity is IPlayerEntity player)) { return; } var ctx = new SaltyCommandContext(message, player, _commands); IResult result = await _commands.ExecuteAsync(ctx.Input, ctx, Services); if (result.IsSuccessful) { return; } await HandleErrorAsync(result, ctx); }