Browse Source

Merge ef4d97dfb4 into b88ce8c51f

pull/949/merge
vim2meta GitHub 7 years ago
parent
commit
493ee481d0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 4 deletions
  1. +12
    -2
      src/Discord.Net.Commands/CommandService.cs
  2. +8
    -2
      src/Discord.Net.Commands/Results/PreconditionResult.cs

+ 12
- 2
src/Discord.Net.Commands/CommandService.cs View File

@@ -1,4 +1,4 @@
using Discord.Commands.Builders;
using Discord.Commands.Builders;
using Discord.Logging;
using System;
using System.Collections.Concurrent;
@@ -287,7 +287,17 @@ namespace Discord.Commands

foreach (var match in commands)
{
preconditionResults[match] = await match.Command.CheckPreconditionsAsync(context, services).ConfigureAwait(false);
try
{
preconditionResults[match] = await match.Command.CheckPreconditionsAsync(context, services).ConfigureAwait(false);
}
catch (Exception ex)
{
if (_throwOnError)
throw;

return PreconditionResult.FromError(ex);
}
}

var successfulPreconditions = preconditionResults


+ 8
- 2
src/Discord.Net.Commands/Results/PreconditionResult.cs View File

@@ -1,17 +1,21 @@
using System.Diagnostics;
using System;
using System.Diagnostics;

namespace Discord.Commands
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class PreconditionResult : IResult
{
public Exception Exception { get; }

public CommandError? Error { get; }
public string ErrorReason { get; }

public bool IsSuccess => !Error.HasValue;

protected PreconditionResult(CommandError? error, string errorReason)
protected PreconditionResult(CommandError? error, string errorReason, Exception exception = null)
{
Exception = exception;
Error = error;
ErrorReason = errorReason;
}
@@ -20,6 +24,8 @@ namespace Discord.Commands
=> new PreconditionResult(null, null);
public static PreconditionResult FromError(string reason)
=> new PreconditionResult(CommandError.UnmetPrecondition, reason);
public static PreconditionResult FromError(Exception ex)
=> new PreconditionResult(CommandError.Exception, ex.Message, ex);
public static PreconditionResult FromError(IResult result)
=> new PreconditionResult(result.Error, result.ErrorReason);



Loading…
Cancel
Save