From 182f00f8ce51d3e350d1793dc016474751f63f75 Mon Sep 17 00:00:00 2001 From: Christopher F <13098994+foxbot@users.noreply.github.com> Date: Mon, 28 Aug 2017 16:45:53 -0400 Subject: [PATCH] Reworked IChannel.IsNsfw to support the new API flag (#771) IChannel.IsNsfw will now return false when being used on any channel that is not an ITextChannel. When being used on an ITextChannel, this will now account for the API flag, and fall back to the channel name. (this is gross design, thanks discord) --- src/Discord.Net.Rest/API/Common/Channel.cs | 2 ++ src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs | 4 ++-- src/Discord.Net.Rest/Entities/Channels/RestChannel.cs | 2 +- src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs | 4 ++++ src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs | 2 +- .../Entities/Channels/SocketTextChannel.cs | 5 +++++ 6 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Discord.Net.Rest/API/Common/Channel.cs b/src/Discord.Net.Rest/API/Common/Channel.cs index 56a24a1f4..608ddcf66 100644 --- a/src/Discord.Net.Rest/API/Common/Channel.cs +++ b/src/Discord.Net.Rest/API/Common/Channel.cs @@ -29,6 +29,8 @@ namespace Discord.API public Optional Topic { get; set; } [JsonProperty("last_pin_timestamp")] public Optional LastPinTimestamp { get; set; } + [JsonProperty("nsfw")] + public Optional Nsfw { get; set; } //VoiceChannel [JsonProperty("bitrate")] diff --git a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs index 6b7dca3a9..30c28bacb 100644 --- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs +++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs @@ -290,8 +290,8 @@ namespace Discord.Rest return author; } - public static bool IsNsfw(IChannel channel) => - IsNsfw(channel.Name); + 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 7291b591e..9e4c97cfa 100644 --- a/src/Discord.Net.Rest/Entities/Channels/RestChannel.cs +++ b/src/Discord.Net.Rest/Entities/Channels/RestChannel.cs @@ -46,7 +46,7 @@ namespace Discord.Rest //IChannel string IChannel.Name => null; - bool IChannel.IsNsfw => ChannelHelper.IsNsfw(this); + bool IChannel.IsNsfw => false; Task IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) => Task.FromResult(null); //Overriden diff --git a/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs index d7405fb4a..3353dc7b0 100644 --- a/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs +++ b/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs @@ -15,6 +15,8 @@ namespace Discord.Rest public string Mention => MentionUtils.MentionChannel(Id); + internal bool Nsfw { get; private set; } + internal RestTextChannel(BaseDiscordClient discord, IGuild guild, ulong id) : base(discord, guild, id) { @@ -30,6 +32,7 @@ namespace Discord.Rest base.Update(model); Topic = model.Topic.Value; + Nsfw = model.Nsfw.GetValueOrDefault(); } public async Task ModifyAsync(Action func, RequestOptions options = null) @@ -149,5 +152,6 @@ namespace Discord.Rest else return AsyncEnumerable.Empty>(); } + bool IChannel.IsNsfw => Nsfw || ChannelHelper.IsNsfw(this); } } diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs index 42c4156f3..b0d7261fd 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs @@ -41,7 +41,7 @@ namespace Discord.WebSocket //IChannel string IChannel.Name => null; - bool IChannel.IsNsfw => ChannelHelper.IsNsfw(this); + bool IChannel.IsNsfw => false; Task IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) => Task.FromResult(null); //Overridden diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs index c22523e00..1b351aae4 100644 --- a/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs +++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs @@ -16,6 +16,7 @@ namespace Discord.WebSocket private readonly MessageCache _messages; public string Topic { get; private set; } + internal bool Nsfw { get; private set; } public string Mention => MentionUtils.MentionChannel(Id); public IReadOnlyCollection CachedMessages => _messages?.Messages ?? ImmutableArray.Create(); @@ -41,6 +42,7 @@ namespace Discord.WebSocket base.Update(state, model); Topic = model.Topic.Value; + Nsfw = model.Nsfw.GetValueOrDefault(); } public Task ModifyAsync(Action func, RequestOptions options = null) @@ -144,5 +146,8 @@ namespace Discord.WebSocket => await SendMessageAsync(text, isTTS, embed, options).ConfigureAwait(false); IDisposable IMessageChannel.EnterTypingState(RequestOptions options) => EnterTypingState(options); + + // IChannel + bool IChannel.IsNsfw => Nsfw || ChannelHelper.IsNsfw(this); } } \ No newline at end of file