Browse Source

feature: Allow setting custom error messages for preconditions (#1124)

* Rebase and use in all in-box preconditions

* Silly git....

* Add configurable 'NotAGuild' message

* Respond to feedback
tags/2.0
Joe4evr Christopher F 6 years ago
parent
commit
5677f23e9a
6 changed files with 27 additions and 10 deletions
  1. +8
    -0
      src/Discord.Net.Commands/Attributes/PreconditionAttribute.cs
  2. +5
    -3
      src/Discord.Net.Commands/Attributes/Preconditions/RequireBotPermissionAttribute.cs
  3. +2
    -1
      src/Discord.Net.Commands/Attributes/Preconditions/RequireContextAttribute.cs
  4. +3
    -1
      src/Discord.Net.Commands/Attributes/Preconditions/RequireNsfwAttribute.cs
  5. +4
    -2
      src/Discord.Net.Commands/Attributes/Preconditions/RequireOwnerAttribute.cs
  6. +5
    -3
      src/Discord.Net.Commands/Attributes/Preconditions/RequireUserPermissionAttribute.cs

+ 8
- 0
src/Discord.Net.Commands/Attributes/PreconditionAttribute.cs View File

@@ -20,6 +20,14 @@ namespace Discord.Commands
/// </remarks>
public string Group { get; set; } = null;

/// <summary>
/// When overridden in a derived class, uses the supplied string
/// as the error message if the precondition doesn't pass.
/// Setting this for a class that doesn't override
/// this property is a no-op.
/// </summary>
public virtual string ErrorMessage { get { return null; } set { } }

/// <summary>
/// Checks if the <paramref name="command"/> has the sufficient permission to be executed.
/// </summary>


+ 5
- 3
src/Discord.Net.Commands/Attributes/Preconditions/RequireBotPermissionAttribute.cs View File

@@ -17,6 +17,8 @@ namespace Discord.Commands
/// Gets the specified <see cref="Discord.ChannelPermission" /> of the precondition.
/// </summary>
public ChannelPermission? ChannelPermission { get; }
public override string ErrorMessage { get; set; }
public string NotAGuildErrorMessage { get; set; }

/// <summary>
/// Requires the bot account to have a specific <see cref="Discord.GuildPermission"/>.
@@ -56,9 +58,9 @@ namespace Discord.Commands
if (GuildPermission.HasValue)
{
if (guildUser == null)
return PreconditionResult.FromError("Command must be used in a guild channel.");
return PreconditionResult.FromError(NotAGuildErrorMessage ?? "Command must be used in a guild channel.");
if (!guildUser.GuildPermissions.Has(GuildPermission.Value))
return PreconditionResult.FromError($"Bot requires guild permission {GuildPermission.Value}.");
return PreconditionResult.FromError(ErrorMessage ?? $"Bot requires guild permission {GuildPermission.Value}.");
}

if (ChannelPermission.HasValue)
@@ -70,7 +72,7 @@ namespace Discord.Commands
perms = ChannelPermissions.All(context.Channel);

if (!perms.Has(ChannelPermission.Value))
return PreconditionResult.FromError($"Bot requires channel permission {ChannelPermission.Value}.");
return PreconditionResult.FromError(ErrorMessage ?? $"Bot requires channel permission {ChannelPermission.Value}.");
}

return PreconditionResult.FromSuccess();


+ 2
- 1
src/Discord.Net.Commands/Attributes/Preconditions/RequireContextAttribute.cs View File

@@ -33,6 +33,7 @@ namespace Discord.Commands
/// Gets the context required to execute the command.
/// </summary>
public ContextType Contexts { get; }
public override string ErrorMessage { get; set; }

/// <summary> Requires the command to be invoked in the specified context. </summary>
/// <param name="contexts">The type of context the command can be invoked in. Multiple contexts can be specified by ORing the contexts together.</param>
@@ -66,7 +67,7 @@ namespace Discord.Commands
if (isValid)
return Task.FromResult(PreconditionResult.FromSuccess());
else
return Task.FromResult(PreconditionResult.FromError($"Invalid context for command; accepted contexts: {Contexts}."));
return Task.FromResult(PreconditionResult.FromError(ErrorMessage ?? $"Invalid context for command; accepted contexts: {Contexts}."));
}
}
}

+ 3
- 1
src/Discord.Net.Commands/Attributes/Preconditions/RequireNsfwAttribute.cs View File

@@ -30,13 +30,15 @@ namespace Discord.Commands
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class RequireNsfwAttribute : PreconditionAttribute
{
public override string ErrorMessage { get; set; }

/// <inheritdoc />
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
{
if (context.Channel is ITextChannel text && text.IsNsfw)
return Task.FromResult(PreconditionResult.FromSuccess());
else
return Task.FromResult(PreconditionResult.FromError("This command may only be invoked in an NSFW channel."));
return Task.FromResult(PreconditionResult.FromError(ErrorMessage ?? "This command may only be invoked in an NSFW channel."));
}
}
}

+ 4
- 2
src/Discord.Net.Commands/Attributes/Preconditions/RequireOwnerAttribute.cs View File

@@ -34,6 +34,8 @@ namespace Discord.Commands
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class RequireOwnerAttribute : PreconditionAttribute
{
public override string ErrorMessage { get; set; }

/// <inheritdoc />
public override async Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
{
@@ -42,10 +44,10 @@ namespace Discord.Commands
case TokenType.Bot:
var application = await context.Client.GetApplicationInfoAsync().ConfigureAwait(false);
if (context.User.Id != application.Owner.Id)
return PreconditionResult.FromError("Command can only be run by the owner of the bot.");
return PreconditionResult.FromError(ErrorMessage ?? "Command can only be run by the owner of the bot.");
return PreconditionResult.FromSuccess();
default:
return PreconditionResult.FromError($"{nameof(RequireOwnerAttribute)} is not supported by this {nameof(TokenType)}.");
return PreconditionResult.FromError($"{nameof(RequireOwnerAttribute)} is not supported by this {nameof(TokenType)}.");
}
}
}


+ 5
- 3
src/Discord.Net.Commands/Attributes/Preconditions/RequireUserPermissionAttribute.cs View File

@@ -17,6 +17,8 @@ namespace Discord.Commands
/// Gets the specified <see cref="Discord.ChannelPermission" /> of the precondition.
/// </summary>
public ChannelPermission? ChannelPermission { get; }
public override string ErrorMessage { get; set; }
public string NotAGuildErrorMessage { get; set; }

/// <summary>
/// Requires that the user invoking the command to have a specific <see cref="Discord.GuildPermission"/>.
@@ -54,9 +56,9 @@ namespace Discord.Commands
if (GuildPermission.HasValue)
{
if (guildUser == null)
return Task.FromResult(PreconditionResult.FromError("Command must be used in a guild channel."));
return Task.FromResult(PreconditionResult.FromError(NotAGuildErrorMessage ?? "Command must be used in a guild channel."));
if (!guildUser.GuildPermissions.Has(GuildPermission.Value))
return Task.FromResult(PreconditionResult.FromError($"User requires guild permission {GuildPermission.Value}."));
return Task.FromResult(PreconditionResult.FromError(ErrorMessage ?? $"User requires guild permission {GuildPermission.Value}."));
}

if (ChannelPermission.HasValue)
@@ -68,7 +70,7 @@ namespace Discord.Commands
perms = ChannelPermissions.All(context.Channel);

if (!perms.Has(ChannelPermission.Value))
return Task.FromResult(PreconditionResult.FromError($"User requires channel permission {ChannelPermission.Value}."));
return Task.FromResult(PreconditionResult.FromError(ErrorMessage ?? $"User requires channel permission {ChannelPermission.Value}."));
}

return Task.FromResult(PreconditionResult.FromSuccess());


Loading…
Cancel
Save