| @@ -1,4 +1,4 @@ | |||
| using System; | |||
| using System; | |||
| using System.Threading.Tasks; | |||
| namespace Discord.Commands | |||
| @@ -13,6 +13,14 @@ namespace Discord.Commands | |||
| /// </summary> | |||
| 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 { } } | |||
| public abstract Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services); | |||
| } | |||
| } | |||
| @@ -1,4 +1,4 @@ | |||
| using System; | |||
| using System; | |||
| using System.Threading.Tasks; | |||
| namespace Discord.Commands | |||
| @@ -11,6 +11,7 @@ namespace Discord.Commands | |||
| { | |||
| public GuildPermission? GuildPermission { get; } | |||
| public ChannelPermission? ChannelPermission { get; } | |||
| public override string ErrorMessage { get; set; } | |||
| /// <summary> | |||
| /// Require that the bot account has a specified GuildPermission | |||
| @@ -52,7 +53,7 @@ namespace Discord.Commands | |||
| if (guildUser == null) | |||
| return PreconditionResult.FromError("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) | |||
| @@ -64,7 +65,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(); | |||
| @@ -19,6 +19,7 @@ namespace Discord.Commands | |||
| public class RequireContextAttribute : PreconditionAttribute | |||
| { | |||
| public ContextType Contexts { get; } | |||
| public override string ErrorMessage { get; set; } | |||
| /// <summary> | |||
| /// Require that the command be invoked in a specified context. | |||
| @@ -52,7 +53,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}")); | |||
| } | |||
| } | |||
| } | |||
| @@ -9,12 +9,14 @@ namespace Discord.Commands | |||
| [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] | |||
| public class RequireNsfwAttribute : PreconditionAttribute | |||
| { | |||
| public override string ErrorMessage { get; set; } | |||
| 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.")); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,5 +1,7 @@ | |||
| #pragma warning disable CS0618 | |||
| using System; | |||
| using System.Threading.Tasks; | |||
| using Microsoft.Extensions.DependencyInjection; | |||
| namespace Discord.Commands | |||
| { | |||
| @@ -10,6 +12,8 @@ namespace Discord.Commands | |||
| [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] | |||
| public class RequireOwnerAttribute : PreconditionAttribute | |||
| { | |||
| public override string ErrorMessage { get; set; } | |||
| public override async Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) | |||
| { | |||
| switch (context.Client.TokenType) | |||
| @@ -17,10 +21,10 @@ namespace Discord.Commands | |||
| case TokenType.Bot: | |||
| var application = await context.Client.GetApplicationInfoAsync(); | |||
| 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)}."); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,4 +1,4 @@ | |||
| using System; | |||
| using System; | |||
| using System.Threading.Tasks; | |||
| namespace Discord.Commands | |||
| @@ -11,6 +11,7 @@ namespace Discord.Commands | |||
| { | |||
| public GuildPermission? GuildPermission { get; } | |||
| public ChannelPermission? ChannelPermission { get; } | |||
| public override string ErrorMessage { get; set; } | |||
| /// <summary> | |||
| /// Require that the user invoking the command has a specified GuildPermission | |||
| @@ -51,7 +52,7 @@ namespace Discord.Commands | |||
| if (guildUser == null) | |||
| return Task.FromResult(PreconditionResult.FromError("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) | |||
| @@ -63,7 +64,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()); | |||