* Rebase and use in all in-box preconditions * Silly git.... * Add configurable 'NotAGuild' message * Respond to feedbackpull/1214/head
@@ -21,6 +21,14 @@ namespace Discord.Commands | |||||
public string Group { get; set; } = null; | public string Group { get; set; } = null; | ||||
/// <summary> | /// <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. | /// Checks if the <paramref name="command"/> has the sufficient permission to be executed. | ||||
/// </summary> | /// </summary> | ||||
/// <param name="context">The context of the command.</param> | /// <param name="context">The context of the command.</param> | ||||
@@ -17,6 +17,8 @@ namespace Discord.Commands | |||||
/// Gets the specified <see cref="Discord.ChannelPermission" /> of the precondition. | /// Gets the specified <see cref="Discord.ChannelPermission" /> of the precondition. | ||||
/// </summary> | /// </summary> | ||||
public ChannelPermission? ChannelPermission { get; } | public ChannelPermission? ChannelPermission { get; } | ||||
public override string ErrorMessage { get; set; } | |||||
public string NotAGuildErrorMessage { get; set; } | |||||
/// <summary> | /// <summary> | ||||
/// Requires the bot account to have a specific <see cref="Discord.GuildPermission"/>. | /// Requires the bot account to have a specific <see cref="Discord.GuildPermission"/>. | ||||
@@ -56,9 +58,9 @@ namespace Discord.Commands | |||||
if (GuildPermission.HasValue) | if (GuildPermission.HasValue) | ||||
{ | { | ||||
if (guildUser == null) | 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)) | 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) | if (ChannelPermission.HasValue) | ||||
@@ -70,7 +72,7 @@ namespace Discord.Commands | |||||
perms = ChannelPermissions.All(context.Channel); | perms = ChannelPermissions.All(context.Channel); | ||||
if (!perms.Has(ChannelPermission.Value)) | 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(); | return PreconditionResult.FromSuccess(); | ||||
@@ -33,6 +33,7 @@ namespace Discord.Commands | |||||
/// Gets the context required to execute the command. | /// Gets the context required to execute the command. | ||||
/// </summary> | /// </summary> | ||||
public ContextType Contexts { get; } | public ContextType Contexts { get; } | ||||
public override string ErrorMessage { get; set; } | |||||
/// <summary> Requires the command to be invoked in the specified context. </summary> | /// <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> | /// <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) | if (isValid) | ||||
return Task.FromResult(PreconditionResult.FromSuccess()); | return Task.FromResult(PreconditionResult.FromSuccess()); | ||||
else | 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}.")); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -30,13 +30,15 @@ namespace Discord.Commands | |||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] | ||||
public class RequireNsfwAttribute : PreconditionAttribute | public class RequireNsfwAttribute : PreconditionAttribute | ||||
{ | { | ||||
public override string ErrorMessage { get; set; } | |||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) | public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) | ||||
{ | { | ||||
if (context.Channel is ITextChannel text && text.IsNsfw) | if (context.Channel is ITextChannel text && text.IsNsfw) | ||||
return Task.FromResult(PreconditionResult.FromSuccess()); | return Task.FromResult(PreconditionResult.FromSuccess()); | ||||
else | 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.")); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -34,6 +34,8 @@ namespace Discord.Commands | |||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] | ||||
public class RequireOwnerAttribute : PreconditionAttribute | public class RequireOwnerAttribute : PreconditionAttribute | ||||
{ | { | ||||
public override string ErrorMessage { get; set; } | |||||
/// <inheritdoc /> | /// <inheritdoc /> | ||||
public override async Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) | public override async Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) | ||||
{ | { | ||||
@@ -42,10 +44,10 @@ namespace Discord.Commands | |||||
case TokenType.Bot: | case TokenType.Bot: | ||||
var application = await context.Client.GetApplicationInfoAsync().ConfigureAwait(false); | var application = await context.Client.GetApplicationInfoAsync().ConfigureAwait(false); | ||||
if (context.User.Id != application.Owner.Id) | 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(); | return PreconditionResult.FromSuccess(); | ||||
default: | 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)}."); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
@@ -17,6 +17,8 @@ namespace Discord.Commands | |||||
/// Gets the specified <see cref="Discord.ChannelPermission" /> of the precondition. | /// Gets the specified <see cref="Discord.ChannelPermission" /> of the precondition. | ||||
/// </summary> | /// </summary> | ||||
public ChannelPermission? ChannelPermission { get; } | public ChannelPermission? ChannelPermission { get; } | ||||
public override string ErrorMessage { get; set; } | |||||
public string NotAGuildErrorMessage { get; set; } | |||||
/// <summary> | /// <summary> | ||||
/// Requires that the user invoking the command to have a specific <see cref="Discord.GuildPermission"/>. | /// 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 (GuildPermission.HasValue) | ||||
{ | { | ||||
if (guildUser == null) | 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)) | 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) | if (ChannelPermission.HasValue) | ||||
@@ -68,7 +70,7 @@ namespace Discord.Commands | |||||
perms = ChannelPermissions.All(context.Channel); | perms = ChannelPermissions.All(context.Channel); | ||||
if (!perms.Has(ChannelPermission.Value)) | 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()); | return Task.FromResult(PreconditionResult.FromSuccess()); | ||||