From 158222bb78693c94b2f29b7b1918e86c123ac7e3 Mon Sep 17 00:00:00 2001 From: RogueException Date: Tue, 24 Jan 2017 11:41:07 -0400 Subject: [PATCH] Merged GuildExtensions into IGuild --- .../Entities/Guilds/IGuild.cs | 9 ++ .../Extensions/GuildExtensions.cs | 38 ------ .../Entities/Guilds/RestGuild.cs | 109 +++++++++++++++++- .../Entities/Guilds/SocketGuild.cs | 55 ++++++++- 4 files changed, 164 insertions(+), 47 deletions(-) delete mode 100644 src/Discord.Net.Core/Extensions/GuildExtensions.cs diff --git a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs index f62ea080c..2ce9b48d0 100644 --- a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs +++ b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs @@ -77,6 +77,13 @@ namespace Discord Task> GetChannelsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); /// Gets the channel in this guild with the provided id, or null if not found. Task GetChannelAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); + Task> GetTextChannelsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); + Task GetTextChannelAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); + Task> GetVoiceChannelsAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); + Task GetVoiceChannelAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); + Task GetAFKChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); + Task GetDefaultChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); + Task GetEmbedChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); /// Creates a new text channel. Task CreateTextChannelAsync(string name, RequestOptions options = null); /// Creates a new voice channel. @@ -99,6 +106,8 @@ namespace Discord Task GetUserAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); /// Gets the current user for this guild. Task GetCurrentUserAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); + /// Gets the owner of this guild. + Task GetOwnerAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); /// Downloads all users for this guild if the current list is incomplete. Task DownloadUsersAsync(); /// Removes all users from this guild if they have not logged on in a provided number of days or, if simulate is true, returns the number of users that would be removed. diff --git a/src/Discord.Net.Core/Extensions/GuildExtensions.cs b/src/Discord.Net.Core/Extensions/GuildExtensions.cs deleted file mode 100644 index ea8e58e2b..000000000 --- a/src/Discord.Net.Core/Extensions/GuildExtensions.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Discord -{ - public static class GuildExtensions - { - public static async Task GetTextChannelAsync(this IGuild guild, ulong id) - => await guild.GetChannelAsync(id).ConfigureAwait(false) as ITextChannel; - public static async Task> GetTextChannelsAsync(this IGuild guild) - => (await guild.GetChannelsAsync().ConfigureAwait(false)).Select(x => x as ITextChannel).Where(x => x != null); - - public static async Task GetVoiceChannelAsync(this IGuild guild, ulong id) - => await guild.GetChannelAsync(id).ConfigureAwait(false) as IVoiceChannel; - public static async Task> GetVoiceChannelsAsync(this IGuild guild) - => (await guild.GetChannelsAsync().ConfigureAwait(false)).Select(x => x as IVoiceChannel).Where(x => x != null); - - public static async Task GetAFKChannelAsync(this IGuild guild) - { - var afkId = guild.AFKChannelId; - if (afkId.HasValue) - return await guild.GetChannelAsync(afkId.Value).ConfigureAwait(false) as IVoiceChannel; - return null; - } - public static async Task GetDefaultChannelAsync(this IGuild guild) - => await guild.GetChannelAsync(guild.DefaultChannelId).ConfigureAwait(false) as ITextChannel; - public static async Task GetEmbedChannelAsync(this IGuild guild) - { - var embedId = guild.EmbedChannelId; - if (embedId.HasValue) - return await guild.GetChannelAsync(embedId.Value).ConfigureAwait(false) as IVoiceChannel; - return null; - } - public static async Task GetOwnerAsync(this IGuild guild) - => await guild.GetUserAsync(guild.OwnerId).ConfigureAwait(false); - } -} diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs index 340fe7f3c..0622df6ce 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs @@ -1,5 +1,4 @@ -using Discord.API.Rest; -using Discord.Audio; +using Discord.Audio; using System; using System.Collections.Generic; using System.Collections.Immutable; @@ -152,7 +151,53 @@ namespace Discord.Rest public Task> GetChannelsAsync(RequestOptions options = null) => GuildHelper.GetChannelsAsync(this, Discord, options); public Task GetChannelAsync(ulong id, RequestOptions options = null) - => GuildHelper.GetChannelAsync(this, Discord, id, options); + => GuildHelper.GetChannelAsync(this, Discord, id, options); + public async Task GetTextChannelAsync(ulong id, RequestOptions options = null) + { + var channel = await GuildHelper.GetChannelAsync(this, Discord, id, options).ConfigureAwait(false); + return channel as RestTextChannel; + } + public async Task> GetTextChannelsAsync(RequestOptions options = null) + { + var channels = await GuildHelper.GetChannelsAsync(this, Discord, options).ConfigureAwait(false); + return channels.Select(x => x as RestTextChannel).Where(x => x != null).ToImmutableArray(); + } + public async Task GetVoiceChannelAsync(ulong id, RequestOptions options = null) + { + var channel = await GuildHelper.GetChannelAsync(this, Discord, id, options).ConfigureAwait(false); + return channel as RestVoiceChannel; + } + public async Task> GetVoiceChannelsAsync(RequestOptions options = null) + { + var channels = await GuildHelper.GetChannelsAsync(this, Discord, options).ConfigureAwait(false); + return channels.Select(x => x as RestVoiceChannel).Where(x => x != null).ToImmutableArray(); + } + + public async Task GetAFKChannelAsync(RequestOptions options = null) + { + var afkId = AFKChannelId; + if (afkId.HasValue) + { + var channel = await GuildHelper.GetChannelAsync(this, Discord, afkId.Value, options).ConfigureAwait(false); + return channel as RestVoiceChannel; + } + return null; + } + public async Task GetDefaultChannelAsync(RequestOptions options = null) + { + var channel = await GuildHelper.GetChannelAsync(this, Discord, DefaultChannelId, options).ConfigureAwait(false); + return channel as RestTextChannel; + } + public async Task GetEmbedChannelAsync(RequestOptions options = null) + { + var embedId = EmbedChannelId; + if (embedId.HasValue) + { + var channel = await GuildHelper.GetChannelAsync(this, Discord, embedId.Value, options).ConfigureAwait(false); + return channel as RestVoiceChannel; + } + return null; + } public Task CreateTextChannelAsync(string name, RequestOptions options = null) => GuildHelper.CreateTextChannelAsync(this, Discord, name, options); public Task CreateVoiceChannelAsync(string name, RequestOptions options = null) @@ -192,6 +237,8 @@ namespace Discord.Rest => GuildHelper.GetUserAsync(this, Discord, id, options); public Task GetCurrentUserAsync(RequestOptions options = null) => GuildHelper.GetUserAsync(this, Discord, Discord.CurrentUser.Id, options); + public Task GetOwnerAsync(RequestOptions options = null) + => GuildHelper.GetUserAsync(this, Discord, OwnerId, options); public Task PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null) => GuildHelper.PruneUsersAsync(this, Discord, days, simulate, options); @@ -222,6 +269,55 @@ namespace Discord.Rest else return null; } + async Task> IGuild.GetTextChannelsAsync(CacheMode mode, RequestOptions options) + { + if (mode == CacheMode.AllowDownload) + return await GetTextChannelsAsync(options).ConfigureAwait(false); + else + return ImmutableArray.Create(); + } + async Task IGuild.GetTextChannelAsync(ulong id, CacheMode mode, RequestOptions options) + { + if (mode == CacheMode.AllowDownload) + return await GetTextChannelAsync(id, options).ConfigureAwait(false); + else + return null; + } + async Task> IGuild.GetVoiceChannelsAsync(CacheMode mode, RequestOptions options) + { + if (mode == CacheMode.AllowDownload) + return await GetVoiceChannelsAsync(options).ConfigureAwait(false); + else + return ImmutableArray.Create(); + } + async Task IGuild.GetVoiceChannelAsync(ulong id, CacheMode mode, RequestOptions options) + { + if (mode == CacheMode.AllowDownload) + return await GetVoiceChannelAsync(id, options).ConfigureAwait(false); + else + return null; + } + async Task IGuild.GetAFKChannelAsync(CacheMode mode, RequestOptions options) + { + if (mode == CacheMode.AllowDownload) + return await GetAFKChannelAsync(options).ConfigureAwait(false); + else + return null; + } + async Task IGuild.GetDefaultChannelAsync(CacheMode mode, RequestOptions options) + { + if (mode == CacheMode.AllowDownload) + return await GetDefaultChannelAsync(options).ConfigureAwait(false); + else + return null; + } + async Task IGuild.GetEmbedChannelAsync(CacheMode mode, RequestOptions options) + { + if (mode == CacheMode.AllowDownload) + return await GetEmbedChannelAsync(options).ConfigureAwait(false); + else + return null; + } async Task IGuild.CreateTextChannelAsync(string name, RequestOptions options) => await CreateTextChannelAsync(name, options).ConfigureAwait(false); async Task IGuild.CreateVoiceChannelAsync(string name, RequestOptions options) @@ -254,6 +350,13 @@ namespace Discord.Rest else return null; } + async Task IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options) + { + if (mode == CacheMode.AllowDownload) + return await GetOwnerAsync(options).ConfigureAwait(false); + else + return null; + } async Task> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options) { if (mode == CacheMode.AllowDownload) diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index b46107dc6..c1bec756c 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -1,5 +1,4 @@ -using Discord.API.Rest; -using Discord.Audio; +using Discord.Audio; using Discord.Rest; using System; using System.Collections.Concurrent; @@ -44,15 +43,16 @@ namespace Discord.WebSocket public int MemberCount { get; set; } public int DownloadedMemberCount { get; private set; } - public ulong? AFKChannelId { get; private set; } - public ulong? EmbedChannelId { get; private set; } + internal ulong? AFKChannelId { get; private set; } + internal ulong? EmbedChannelId { get; private set; } public ulong OwnerId { get; private set; } + public SocketGuildUser Owner => GetUser(OwnerId); public string VoiceRegionId { get; private set; } public string IconId { get; private set; } public string SplashId { get; private set; } public DateTimeOffset CreatedAt => DateTimeUtils.FromSnowflake(Id); - public ulong DefaultChannelId => Id; + public SocketTextChannel DefaultChannel => GetTextChannel(Id); public string IconUrl => CDN.GetGuildIconUrl(Id, IconId); public string SplashUrl => CDN.GetGuildSplashUrl(Id, SplashId); public bool HasAllMembers => _downloaderPromise.Task.IsCompleted; @@ -60,6 +60,26 @@ namespace Discord.WebSocket public Task SyncPromise => _syncPromise.Task; public Task DownloaderPromise => _downloaderPromise.Task; public IAudioClient AudioClient => _audioClient; + public SocketVoiceChannel AFKChannel + { + get + { + var id = AFKChannelId; + return id.HasValue ? GetVoiceChannel(id.Value) : null; + } + } + public SocketVoiceChannel EmbedChannel + { + get + { + var id = EmbedChannelId; + return id.HasValue ? GetVoiceChannel(id.Value) : null; + } + } + public IReadOnlyCollection TextChannels + => Channels.Select(x => x as SocketTextChannel).Where(x => x != null).ToImmutableArray(); + public IReadOnlyCollection VoiceChannels + => Channels.Select(x => x as SocketVoiceChannel).Where(x => x != null).ToImmutableArray(); public SocketGuildUser CurrentUser { get @@ -286,6 +306,10 @@ namespace Discord.WebSocket return channel; return null; } + public SocketTextChannel GetTextChannel(ulong id) + => GetChannel(id) as SocketTextChannel; + public SocketVoiceChannel GetVoiceChannel(ulong id) + => GetChannel(id) as SocketVoiceChannel; public Task CreateTextChannelAsync(string name, RequestOptions options = null) => GuildHelper.CreateTextChannelAsync(this, Discord, name, options); public Task CreateVoiceChannelAsync(string name, RequestOptions options = null) @@ -560,8 +584,11 @@ namespace Discord.WebSocket internal SocketGuild Clone() => MemberwiseClone() as SocketGuild; //IGuild - bool IGuild.Available => true; + ulong? IGuild.AFKChannelId => AFKChannelId; IAudioClient IGuild.AudioClient => null; + bool IGuild.Available => true; + ulong IGuild.DefaultChannelId => Id; + ulong? IGuild.EmbedChannelId => EmbedChannelId; IRole IGuild.EveryoneRole => EveryoneRole; IReadOnlyCollection IGuild.Roles => Roles; @@ -572,6 +599,20 @@ namespace Discord.WebSocket => Task.FromResult>(Channels); Task IGuild.GetChannelAsync(ulong id, CacheMode mode, RequestOptions options) => Task.FromResult(GetChannel(id)); + Task> IGuild.GetTextChannelsAsync(CacheMode mode, RequestOptions options) + => Task.FromResult>(TextChannels); + Task IGuild.GetTextChannelAsync(ulong id, CacheMode mode, RequestOptions options) + => Task.FromResult(GetTextChannel(id)); + Task> IGuild.GetVoiceChannelsAsync(CacheMode mode, RequestOptions options) + => Task.FromResult>(VoiceChannels); + Task IGuild.GetVoiceChannelAsync(ulong id, CacheMode mode, RequestOptions options) + => Task.FromResult(GetVoiceChannel(id)); + Task IGuild.GetAFKChannelAsync(CacheMode mode, RequestOptions options) + => Task.FromResult(AFKChannel); + Task IGuild.GetDefaultChannelAsync(CacheMode mode, RequestOptions options) + => Task.FromResult(DefaultChannel); + Task IGuild.GetEmbedChannelAsync(CacheMode mode, RequestOptions options) + => Task.FromResult(EmbedChannel); async Task IGuild.CreateTextChannelAsync(string name, RequestOptions options) => await CreateTextChannelAsync(name, options).ConfigureAwait(false); async Task IGuild.CreateVoiceChannelAsync(string name, RequestOptions options) @@ -596,6 +637,8 @@ namespace Discord.WebSocket => Task.FromResult(GetUser(id)); Task IGuild.GetCurrentUserAsync(CacheMode mode, RequestOptions options) => Task.FromResult(CurrentUser); + Task IGuild.GetOwnerAsync(CacheMode mode, RequestOptions options) + => Task.FromResult(Owner); Task IGuild.DownloadUsersAsync() { throw new NotSupportedException(); } } }