Browse Source

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)
tags/2.0.0-beta
Christopher F GitHub 7 years ago
parent
commit
182f00f8ce
6 changed files with 15 additions and 4 deletions
  1. +2
    -0
      src/Discord.Net.Rest/API/Common/Channel.cs
  2. +2
    -2
      src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
  3. +1
    -1
      src/Discord.Net.Rest/Entities/Channels/RestChannel.cs
  4. +4
    -0
      src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
  5. +1
    -1
      src/Discord.Net.WebSocket/Entities/Channels/SocketChannel.cs
  6. +5
    -0
      src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs

+ 2
- 0
src/Discord.Net.Rest/API/Common/Channel.cs View File

@@ -29,6 +29,8 @@ namespace Discord.API
public Optional<string> Topic { get; set; }
[JsonProperty("last_pin_timestamp")]
public Optional<DateTimeOffset?> LastPinTimestamp { get; set; }
[JsonProperty("nsfw")]
public Optional<bool> Nsfw { get; set; }

//VoiceChannel
[JsonProperty("bitrate")]


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

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


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

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

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

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


+ 4
- 0
src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs View File

@@ -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<TextChannelProperties> func, RequestOptions options = null)
@@ -149,5 +152,6 @@ namespace Discord.Rest
else
return AsyncEnumerable.Empty<IReadOnlyCollection<IGuildUser>>();
}
bool IChannel.IsNsfw => Nsfw || ChannelHelper.IsNsfw(this);
}
}

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

@@ -41,7 +41,7 @@ namespace Discord.WebSocket

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

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


+ 5
- 0
src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs View File

@@ -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<SocketMessage> CachedMessages => _messages?.Messages ?? ImmutableArray.Create<SocketMessage>();
@@ -41,6 +42,7 @@ namespace Discord.WebSocket
base.Update(state, model);

Topic = model.Topic.Value;
Nsfw = model.Nsfw.GetValueOrDefault();
}

public Task ModifyAsync(Action<TextChannelProperties> 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);
}
}

Loading…
Cancel
Save