using System; using System.Threading.Tasks; namespace Discord.Commands { /// /// Requires the command to be invoked in a channel marked NSFW. /// /// /// The precondition will restrict the access of the command or module to be accessed within a guild channel /// that has been marked as mature or NSFW. If the channel is not of type or the /// channel is not marked as NSFW, the precondition will fail with an erroneous . /// /// /// The following example restricts the command too-cool to an NSFW-enabled channel only. /// /// public class DankModule : ModuleBase /// { /// [Command("cool")] /// public Task CoolAsync() /// => ReplyAsync("I'm cool for everyone."); /// /// [RequireNsfw] /// [Command("too-cool")] /// public Task TooCoolAsync() /// => ReplyAsync("You can only see this if you're cool enough."); /// } /// /// [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] public class RequireNsfwAttribute : PreconditionAttribute { /// public override string ErrorMessage { get; set; } /// public override Task 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(ErrorMessage ?? "This command may only be invoked in an NSFW channel.")); } } }