using System;
namespace Discord.Interactions
{
///
/// Contains information of the command's overall execution result.
///
public struct ExecuteResult : IResult
{
///
/// Gets the exception that may have occurred during the command execution.
///
public Exception Exception { get; }
///
public InteractionCommandError? Error { get; }
///
public string ErrorReason { get; }
///
public bool IsSuccess => !Error.HasValue;
private ExecuteResult (Exception exception, InteractionCommandError? commandError, string errorReason)
{
Exception = exception;
Error = commandError;
ErrorReason = errorReason;
}
///
/// Initializes a new with no error, indicating a successful execution.
///
///
/// A that does not contain any errors.
///
public static ExecuteResult FromSuccess ( ) =>
new ExecuteResult(null, null, null);
///
/// Initializes a new with a specified and its
/// reason, indicating an unsuccessful execution.
///
/// The type of error.
/// The reason behind the error.
///
/// A that contains a and reason.
///
public static ExecuteResult FromError (InteractionCommandError commandError, string reason) =>
new ExecuteResult(null, commandError, reason);
///
/// Initializes a new with a specified exception, indicating an unsuccessful
/// execution.
///
/// The exception that caused the command execution to fail.
///
/// A that contains the exception that caused the unsuccessful execution, along
/// with a of type Exception as well as the exception message as the
/// reason.
///
public static ExecuteResult FromError (Exception exception) =>
new ExecuteResult(exception, InteractionCommandError.Exception, exception.Message);
///
/// Initializes a new with a specified result; this may or may not be an
/// successful execution depending on the and
/// specified.
///
/// The result to inherit from.
///
/// A that inherits the error type and reason.
///
public static ExecuteResult FromError (IResult result) =>
new ExecuteResult(null, result.Error, result.ErrorReason);
///
/// Gets a string that indicates the execution result.
///
///
/// Success if is ; otherwise ":
/// ".
///
public override string ToString ( ) => IsSuccess ? "Success" : $"{Error}: {ErrorReason}";
}
}