diff --git a/src/Discord.Net.Commands/Attributes/Preconditions/RequireNsfwAttribute.cs b/src/Discord.Net.Commands/Attributes/Preconditions/RequireNsfwAttribute.cs new file mode 100644 index 000000000..a52ec6ddd --- /dev/null +++ b/src/Discord.Net.Commands/Attributes/Preconditions/RequireNsfwAttribute.cs @@ -0,0 +1,20 @@ +using System; +using System.Threading.Tasks; + +namespace Discord.Commands +{ + /// + /// Require that the command is invoked in a channel marked NSFW + /// + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] + public class RequireNsfwAttribute : PreconditionAttribute + { + public override Task CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map) + { + if (context.Channel.IsNsfw) + return Task.FromResult(PreconditionResult.FromSuccess()); + else + return Task.FromResult(PreconditionResult.FromError("This command may only be invoked in an NSFW channel.")); + } + } +} diff --git a/src/Discord.Net.Core/Entities/Channels/IChannel.cs b/src/Discord.Net.Core/Entities/Channels/IChannel.cs index 72608ec6a..fbb979951 100644 --- a/src/Discord.Net.Core/Entities/Channels/IChannel.cs +++ b/src/Discord.Net.Core/Entities/Channels/IChannel.cs @@ -8,6 +8,9 @@ namespace Discord /// Gets the name of this channel. string Name { get; } + /// Checks if the channel is NSFW. + bool IsNsfw { get; } + /// Gets a collection of all users in this channel. IAsyncEnumerable> GetUsersAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); diff --git a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs index e6d017a89..284decd8c 100644 --- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs +++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs @@ -289,5 +289,10 @@ namespace Discord.Rest author = RestUser.Create(client, guild, model, webhookId); return author; } + + public static bool IsNsfw(IChannel channel) => + IsNsfw(channel.Name); + public static bool IsNsfw(string channelName) => + channelName == "nsfw" || channelName.StartsWith("nsfw-"); } } diff --git a/src/Discord.Net.Rest/Entities/Channels/RestChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestChannel.cs index bc521784d..7291b591e 100644 --- a/src/Discord.Net.Rest/Entities/Channels/RestChannel.cs +++ b/src/Discord.Net.Rest/Entities/Channels/RestChannel.cs @@ -46,6 +46,7 @@ namespace Discord.Rest //IChannel string IChannel.Name => null; + bool IChannel.IsNsfw => ChannelHelper.IsNsfw(this); Task IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) => Task.FromResult(null); //Overriden diff --git a/src/Discord.Net.Rest/Entities/Channels/RpcVirtualMessageChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RpcVirtualMessageChannel.cs index 7e515978d..dfd996ee1 100644 --- a/src/Discord.Net.Rest/Entities/Channels/RpcVirtualMessageChannel.cs +++ b/src/Discord.Net.Rest/Entities/Channels/RpcVirtualMessageChannel.cs @@ -97,6 +97,7 @@ namespace Discord.Rest //IChannel string IChannel.Name { get { throw new NotSupportedException(); } } + bool IChannel.IsNsfw { get { throw new NotSupportedException(); } } IAsyncEnumerable> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options) { throw new NotSupportedException(); diff --git a/src/Discord.Net.Rpc/Entities/Channels/RpcChannel.cs b/src/Discord.Net.Rpc/Entities/Channels/RpcChannel.cs index cca559a31..d26c593ba 100644 --- a/src/Discord.Net.Rpc/Entities/Channels/RpcChannel.cs +++ b/src/Discord.Net.Rpc/Entities/Channels/RpcChannel.cs @@ -1,4 +1,5 @@ -using System; +using Discord.Rest; +using System; using Model = Discord.API.Rpc.Channel; @@ -7,6 +8,7 @@ namespace Discord.Rpc public class RpcChannel : RpcEntity { public string Name { get; private set; } + public bool IsNsfw => ChannelHelper.IsNsfw(Name); public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id); diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs index 319e17c50..42c4156f3 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs @@ -1,4 +1,5 @@ -using System; +using Discord.Rest; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -40,6 +41,7 @@ namespace Discord.WebSocket //IChannel string IChannel.Name => null; + bool IChannel.IsNsfw => ChannelHelper.IsNsfw(this); Task IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) => Task.FromResult(null); //Overridden