@@ -0,0 +1,20 @@ | |||||
using System; | |||||
using System.Threading.Tasks; | |||||
namespace Discord.Commands | |||||
{ | |||||
/// <summary> | |||||
/// Require that the command is invoked in a channel marked NSFW | |||||
/// </summary> | |||||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] | |||||
public class RequireNsfwAttribute : PreconditionAttribute | |||||
{ | |||||
public override Task<PreconditionResult> CheckPermissions(ICommandContext context, CommandInfo command, IDependencyMap map) | |||||
{ | |||||
if (context.Channel.Nsfw) | |||||
return Task.FromResult(PreconditionResult.FromSuccess()); | |||||
else | |||||
return Task.FromResult(PreconditionResult.FromError("This command may only be invoked in an NSFW channel.")); | |||||
} | |||||
} | |||||
} |
@@ -8,6 +8,9 @@ namespace Discord | |||||
/// <summary> Gets the name of this channel. </summary> | /// <summary> Gets the name of this channel. </summary> | ||||
string Name { get; } | string Name { get; } | ||||
/// <summary> Checks if the channel is NSFW. </summary> | |||||
bool Nsfw { get; } | |||||
/// <summary> Gets a collection of all users in this channel. </summary> | /// <summary> Gets a collection of all users in this channel. </summary> | ||||
IAsyncEnumerable<IReadOnlyCollection<IUser>> GetUsersAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); | IAsyncEnumerable<IReadOnlyCollection<IUser>> GetUsersAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); | ||||
@@ -0,0 +1,10 @@ | |||||
namespace Discord | |||||
{ | |||||
public static class NsfwUtils | |||||
{ | |||||
public static bool IsNsfw(IChannel channel) => | |||||
IsNsfw(channel.Name); | |||||
public static bool IsNsfw(string channelName) => | |||||
channelName.StartsWith("nsfw"); | |||||
} | |||||
} |
@@ -46,6 +46,7 @@ namespace Discord.Rest | |||||
//IChannel | //IChannel | ||||
string IChannel.Name => null; | string IChannel.Name => null; | ||||
bool IChannel.Nsfw => NsfwUtils.IsNsfw(this); | |||||
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | ||||
=> Task.FromResult<IUser>(null); //Overriden | => Task.FromResult<IUser>(null); //Overriden | ||||
@@ -97,6 +97,7 @@ namespace Discord.Rest | |||||
//IChannel | //IChannel | ||||
string IChannel.Name { get { throw new NotSupportedException(); } } | string IChannel.Name { get { throw new NotSupportedException(); } } | ||||
bool IChannel.Nsfw { get { throw new NotSupportedException(); } } | |||||
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options) | IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options) | ||||
{ | { | ||||
throw new NotSupportedException(); | throw new NotSupportedException(); | ||||
@@ -7,6 +7,7 @@ namespace Discord.Rpc | |||||
public class RpcChannel : RpcEntity<ulong> | public class RpcChannel : RpcEntity<ulong> | ||||
{ | { | ||||
public string Name { get; private set; } | public string Name { get; private set; } | ||||
public bool Nsfw => NsfwUtils.IsNsfw(Name); | |||||
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id); | public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id); | ||||
@@ -40,6 +40,7 @@ namespace Discord.WebSocket | |||||
//IChannel | //IChannel | ||||
string IChannel.Name => null; | string IChannel.Name => null; | ||||
bool IChannel.Nsfw => NsfwUtils.IsNsfw(this); | |||||
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | ||||
=> Task.FromResult<IUser>(null); //Overridden | => Task.FromResult<IUser>(null); //Overridden | ||||