Browse Source

Add IChannel.Nsfw, RequireNsfw precondition (#633)

* Add IChannel.Nsfw, RequireNsfw precondition

* Refactored IChannel.Nsfw to IsNsfw, removed NsfwUtils

Per pull-request feedback

* proper nsfw channel check
tags/1.0.0-rc2
Christopher F RogueException 8 years ago
parent
commit
7f1fc286cf
7 changed files with 36 additions and 2 deletions
  1. +20
    -0
      src/Discord.Net.Commands/Attributes/Preconditions/RequireNsfwAttribute.cs
  2. +3
    -0
      src/Discord.Net.Core/Entities/Channels/IChannel.cs
  3. +5
    -0
      src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
  4. +1
    -0
      src/Discord.Net.Rest/Entities/Channels/RestChannel.cs
  5. +1
    -0
      src/Discord.Net.Rest/Entities/Channels/RpcVirtualMessageChannel.cs
  6. +3
    -1
      src/Discord.Net.Rpc/Entities/Channels/RpcChannel.cs
  7. +3
    -1
      src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs

+ 20
- 0
src/Discord.Net.Commands/Attributes/Preconditions/RequireNsfwAttribute.cs View File

@@ -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.IsNsfw)
return Task.FromResult(PreconditionResult.FromSuccess());
else
return Task.FromResult(PreconditionResult.FromError("This command may only be invoked in an NSFW channel."));
}
}
}

+ 3
- 0
src/Discord.Net.Core/Entities/Channels/IChannel.cs View File

@@ -8,6 +8,9 @@ namespace Discord
/// <summary> Gets the name of this channel. </summary>
string Name { get; }

/// <summary> Checks if the channel is NSFW. </summary>
bool IsNsfw { get; }

/// <summary> Gets a collection of all users in this channel. </summary>
IAsyncEnumerable<IReadOnlyCollection<IUser>> GetUsersAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);


+ 5
- 0
src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs View File

@@ -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-");
}
}

+ 1
- 0
src/Discord.Net.Rest/Entities/Channels/RestChannel.cs View File

@@ -46,6 +46,7 @@ namespace Discord.Rest

//IChannel
string IChannel.Name => null;
bool IChannel.IsNsfw => ChannelHelper.IsNsfw(this);

Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IUser>(null); //Overriden


+ 1
- 0
src/Discord.Net.Rest/Entities/Channels/RpcVirtualMessageChannel.cs View File

@@ -97,6 +97,7 @@ namespace Discord.Rest

//IChannel
string IChannel.Name { get { throw new NotSupportedException(); } }
bool IChannel.IsNsfw { get { throw new NotSupportedException(); } }
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
{
throw new NotSupportedException();


+ 3
- 1
src/Discord.Net.Rpc/Entities/Channels/RpcChannel.cs View File

@@ -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<ulong>
{
public string Name { get; private set; }
public bool IsNsfw => ChannelHelper.IsNsfw(Name);

public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);



+ 3
- 1
src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs View File

@@ -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<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult<IUser>(null); //Overridden


Loading…
Cancel
Save