From 1ffcd4bfa713b6bdedf238a4845880d79ec4fc5b Mon Sep 17 00:00:00 2001 From: Christopher F <13098994+foxbot@users.noreply.github.com> Date: Mon, 28 Aug 2017 16:49:16 -0400 Subject: [PATCH] Changed Guild#DefaultChannel to resolve the first accessible channel (#777) * Changed Guild#DefaultChannel to resolve the first accessible channel Resolves #776 This change is inline with hammerandchisel/discord-api-docs#329 RestGuild#DefaultChannelId is now obsolete and will throw a NotSupportedException. * RestGuild#DefaultChannelId will fall back to the guild ID Adding an exception here would be a breaking change, so this was agreed to fall back to the previous behavior, which would just return the guild ID. --- src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs | 10 ++++++++-- .../Entities/Guilds/SocketGuild.cs | 7 +++++-- test/Discord.Net.Tests/Tests.Channels.cs | 2 +- test/Discord.Net.Tests/Tests.Migrations.cs | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs index 11971a5c1..0ad76c61a 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs @@ -33,6 +33,8 @@ namespace Discord.Rest internal bool Available { get; private set; } public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id); + + [Obsolete("DefaultChannelId is deprecated, use GetDefaultChannelAsync")] public ulong DefaultChannelId => Id; public string IconUrl => CDN.GetGuildIconUrl(Id, IconId); public string SplashUrl => CDN.GetGuildSplashUrl(Id, SplashId); @@ -185,8 +187,12 @@ namespace Discord.Rest } public async Task GetDefaultChannelAsync(RequestOptions options = null) { - var channel = await GuildHelper.GetChannelAsync(this, Discord, DefaultChannelId, options).ConfigureAwait(false); - return channel as RestTextChannel; + var channels = await GetTextChannelsAsync(options).ConfigureAwait(false); + var user = await GetCurrentUserAsync(options).ConfigureAwait(false); + return channels + .Where(c => user.GetPermissions(c).ReadMessages) + .OrderBy(c => c.Position) + .FirstOrDefault(); } public async Task GetEmbedChannelAsync(RequestOptions options = null) { diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index aae18be36..f973df468 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -54,7 +54,6 @@ namespace Discord.WebSocket public string SplashId { get; private set; } public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id); - public SocketTextChannel DefaultChannel => GetTextChannel(Id); public string IconUrl => CDN.GetGuildIconUrl(Id, IconId); public string SplashUrl => CDN.GetGuildSplashUrl(Id, SplashId); public bool HasAllMembers => MemberCount == DownloadedMemberCount;// _downloaderPromise.Task.IsCompleted; @@ -62,6 +61,10 @@ namespace Discord.WebSocket public Task SyncPromise => _syncPromise.Task; public Task DownloaderPromise => _downloaderPromise.Task; public IAudioClient AudioClient => _audioClient; + public SocketTextChannel DefaultChannel => TextChannels + .Where(c => CurrentUser.GetPermissions(c).ReadMessages) + .OrderBy(c => c.Position) + .FirstOrDefault(); public SocketVoiceChannel AFKChannel { get @@ -606,7 +609,7 @@ namespace Discord.WebSocket ulong? IGuild.AFKChannelId => AFKChannelId; IAudioClient IGuild.AudioClient => null; bool IGuild.Available => true; - ulong IGuild.DefaultChannelId => Id; + ulong IGuild.DefaultChannelId => DefaultChannel?.Id ?? 0; ulong? IGuild.EmbedChannelId => EmbedChannelId; IRole IGuild.EveryoneRole => EveryoneRole; IReadOnlyCollection IGuild.Roles => Roles; diff --git a/test/Discord.Net.Tests/Tests.Channels.cs b/test/Discord.Net.Tests/Tests.Channels.cs index d81d28f3e..b528ca5fb 100644 --- a/test/Discord.Net.Tests/Tests.Channels.cs +++ b/test/Discord.Net.Tests/Tests.Channels.cs @@ -64,7 +64,7 @@ namespace Discord var text5 = textChannels.Where(x => x.Name == "text5").FirstOrDefault(); Assert.NotNull(text1); - Assert.True(text1.Id == guild.DefaultChannelId); + //Assert.True(text1.Id == guild.DefaultChannelId); Assert.Equal(text1.Position, 1); Assert.Equal(text1.Topic, "Topic1"); diff --git a/test/Discord.Net.Tests/Tests.Migrations.cs b/test/Discord.Net.Tests/Tests.Migrations.cs index e786329cd..23e55a737 100644 --- a/test/Discord.Net.Tests/Tests.Migrations.cs +++ b/test/Discord.Net.Tests/Tests.Migrations.cs @@ -57,7 +57,7 @@ namespace Discord foreach (var channel in textChannels) { - if (channel.Id != guild.DefaultChannelId) + //if (channel.Id != guild.DefaultChannelId) await channel.DeleteAsync(); } foreach (var channel in voiceChannels)