diff --git a/src/Discord.Net.WebSocket/BaseSocketClient.cs b/src/Discord.Net.WebSocket/BaseSocketClient.cs index 548bb75bf..425889613 100644 --- a/src/Discord.Net.WebSocket/BaseSocketClient.cs +++ b/src/Discord.Net.WebSocket/BaseSocketClient.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; @@ -75,6 +76,7 @@ namespace Discord.WebSocket /// /// A read-only collection of voice regions that the user has access to. /// + [Obsolete("This property is obsolete, use the GetVoiceRegionsAsync method instead.")] public abstract IReadOnlyCollection VoiceRegions { get; } internal BaseSocketClient(DiscordSocketConfig config, DiscordRestApiClient client) @@ -169,7 +171,26 @@ namespace Discord.WebSocket /// A REST-based voice region associated with the identifier; null if the voice region is not /// found. /// + [Obsolete("This method is obsolete, use GetVoiceRegionAsync instead.")] public abstract RestVoiceRegion GetVoiceRegion(string id); + /// + /// Gets all voice regions. + /// + /// The options to be used when sending the request. + /// + /// A task that contains a read-only collection of REST-based voice regions. + /// + public abstract ValueTask> GetVoiceRegionsAsync(RequestOptions options = null); + /// + /// Gets a voice region. + /// + /// The identifier of the voice region (e.g. eu-central ). + /// The options to be used when sending the request. + /// + /// A task that contains a REST-based voice region associated with the identifier; null if the + /// voice region is not found. + /// + public abstract ValueTask GetVoiceRegionAsync(string id, RequestOptions options = null); /// public abstract Task StartAsync(); /// diff --git a/src/Discord.Net.WebSocket/DiscordShardedClient.cs b/src/Discord.Net.WebSocket/DiscordShardedClient.cs index a2c89d4e5..4c94b14e8 100644 --- a/src/Discord.Net.WebSocket/DiscordShardedClient.cs +++ b/src/Discord.Net.WebSocket/DiscordShardedClient.cs @@ -37,6 +37,7 @@ namespace Discord.WebSocket public override IReadOnlyCollection PrivateChannels => GetPrivateChannels().ToReadOnlyCollection(GetPrivateChannelCount); public IReadOnlyCollection Shards => _shards; /// + [Obsolete("This property is obsolete, use the GetVoiceRegionsAsync method instead.")] public override IReadOnlyCollection VoiceRegions => _shards[0].VoiceRegions; /// @@ -264,9 +265,22 @@ namespace Discord.WebSocket } /// + [Obsolete("This method is obsolete, use GetVoiceRegionAsync instead.")] public override RestVoiceRegion GetVoiceRegion(string id) => _shards[0].GetVoiceRegion(id); + /// + public override async ValueTask> GetVoiceRegionsAsync(RequestOptions options = null) + { + return await _shards[0].GetVoiceRegionsAsync().ConfigureAwait(false); + } + + /// + public override async ValueTask GetVoiceRegionAsync(string id, RequestOptions options = null) + { + return await _shards[0].GetVoiceRegionAsync(id, options).ConfigureAwait(false); + } + /// /// is public override async Task DownloadUsersAsync(IEnumerable guilds) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 0a2123ef2..d4c96ab26 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -110,7 +110,8 @@ namespace Discord.WebSocket public IReadOnlyCollection GroupChannels => State.PrivateChannels.OfType().ToImmutableArray(); /// - public override IReadOnlyCollection VoiceRegions => _voiceRegions.ToReadOnlyCollection(); + [Obsolete("This property is obsolete, use the GetVoiceRegionsAsync method instead.")] + public override IReadOnlyCollection VoiceRegions => GetVoiceRegionsAsync().GetAwaiter().GetResult(); /// /// Initializes a new REST/WebSocket-based Discord client. @@ -178,7 +179,6 @@ namespace Discord.WebSocket return Task.Delay(0); }; - _voiceRegions = ImmutableDictionary.Create(); _largeGuilds = new ConcurrentQueue(); } private static API.DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config) @@ -204,13 +204,6 @@ namespace Discord.WebSocket /// internal override async Task OnLoginAsync(TokenType tokenType, string token) { - if (_parentClient == null) - { - var voiceRegions = await ApiClient.GetVoiceRegionsAsync(new RequestOptions { IgnoreState = true, RetryMode = RetryMode.AlwaysRetry }).ConfigureAwait(false); - _voiceRegions = voiceRegions.Select(x => RestVoiceRegion.Create(this, x)).ToImmutableDictionary(x => x.Id); - } - else - _voiceRegions = _parentClient._voiceRegions; await Rest.OnLoginAsync(tokenType, token); } /// @@ -218,7 +211,7 @@ namespace Discord.WebSocket { await StopAsync().ConfigureAwait(false); _applicationInfo = null; - _voiceRegions = ImmutableDictionary.Create(); + _voiceRegions = null; await Rest.OnLogoutAsync(); } @@ -350,11 +343,39 @@ namespace Discord.WebSocket => State.RemoveUser(id); /// + [Obsolete("This method is obsolete, use GetVoiceRegionAsync instead.")] public override RestVoiceRegion GetVoiceRegion(string id) + => GetVoiceRegionAsync(id).GetAwaiter().GetResult(); + + /// + public override async ValueTask> GetVoiceRegionsAsync(RequestOptions options = null) { - if (_voiceRegions.TryGetValue(id, out RestVoiceRegion region)) - return region; - return null; + if (_parentClient == null) + { + if (_voiceRegions == null) + { + options = RequestOptions.CreateOrClone(options); + options.IgnoreState = true; + var voiceRegions = await ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false); + _voiceRegions = voiceRegions.Select(x => RestVoiceRegion.Create(this, x)).ToImmutableDictionary(x => x.Id); + } + return _voiceRegions.ToReadOnlyCollection(); + } + return await _parentClient.GetVoiceRegionsAsync().ConfigureAwait(false); + } + + /// + public override async ValueTask GetVoiceRegionAsync(string id, RequestOptions options = null) + { + if (_parentClient == null) + { + if (_voiceRegions == null) + await GetVoiceRegionsAsync().ConfigureAwait(false); + if (_voiceRegions.TryGetValue(id, out RestVoiceRegion region)) + return region; + return null; + } + return await _parentClient.GetVoiceRegionAsync(id, options).ConfigureAwait(false); } /// @@ -2120,11 +2141,11 @@ namespace Discord.WebSocket => Task.FromResult(GetUser(username, discriminator)); /// - Task> IDiscordClient.GetVoiceRegionsAsync(RequestOptions options) - => Task.FromResult>(VoiceRegions); + async Task> IDiscordClient.GetVoiceRegionsAsync(RequestOptions options) + => await GetVoiceRegionsAsync(options).ConfigureAwait(false); /// - Task IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options) - => Task.FromResult(GetVoiceRegion(id)); + async Task IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options) + => await GetVoiceRegionAsync(id, options).ConfigureAwait(false); /// async Task IDiscordClient.StartAsync()