Skip to content

Commit

Permalink
Attempts to resolve #961 (#962)
Browse files Browse the repository at this point in the history
* Move REST requests to appropiate class

* Add call to ClientHelper and expose to public API

* Expose shard count request in public api

* Expose method from interface

* Update sharded client to utilize the new method

* Method is already implemented in a base class

* Refactor name to fit pattern for methods returning a `Task`

* Adds missing ConfigureAwait

* Corrects unnecessary whitespace

* Removes unneeded whitespace
  • Loading branch information
Seeker1437 authored and foxbot committed Mar 15, 2018
1 parent f175dde commit fc5e70c
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/Discord.Net.Core/IDiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ public interface IDiscordClient : IDisposable
Task<IVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null);

Task<IWebhook> GetWebhookAsync(ulong id, RequestOptions options = null);

Task<int> GetRecommendedShardCountAsync(RequestOptions options = null);
}
}
6 changes: 5 additions & 1 deletion src/Discord.Net.Rest/BaseDiscordClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Discord.Logging;
using Discord.Logging;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -125,6 +125,10 @@ internal virtual void Dispose(bool disposing)
/// <inheritdoc />
public void Dispose() => Dispose(true);

/// <inheritdoc />
public Task<int> GetRecommendedShardCountAsync(RequestOptions options = null)
=> ClientHelper.GetRecommendShardCountAsync(this, options);

//IDiscordClient
ConnectionState IDiscordClient.ConnectionState => ConnectionState.Disconnected;
ISelfUser IDiscordClient.CurrentUser => CurrentUser;
Expand Down
8 changes: 7 additions & 1 deletion src/Discord.Net.Rest/ClientHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Discord.API.Rest;
using Discord.API.Rest;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
Expand Down Expand Up @@ -163,5 +163,11 @@ public static async Task<RestVoiceRegion> GetVoiceRegionAsync(BaseDiscordClient
var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false);
return models.Select(x => RestVoiceRegion.Create(client, x)).FirstOrDefault(x => x.Id == id);
}

public static async Task<int> GetRecommendShardCountAsync(BaseDiscordClient client, RequestOptions options)
{
var response = await client.ApiClient.GetBotGatewayAsync(options).ConfigureAwait(false);
return response.Shards;
}
}
}
12 changes: 12 additions & 0 deletions src/Discord.Net.Rest/DiscordRestApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,18 @@ public async Task ValidateTokenAsync(RequestOptions options = null)
await SendAsync("GET", () => "auth/login", new BucketIds(), options: options).ConfigureAwait(false);
}

//Gateway
public async Task<GetGatewayResponse> GetGatewayAsync(RequestOptions options = null)
{
options = RequestOptions.CreateOrClone(options);
return await SendAsync<GetGatewayResponse>("GET", () => "gateway", new BucketIds(), options: options).ConfigureAwait(false);
}
public async Task<GetBotGatewayResponse> GetBotGatewayAsync(RequestOptions options = null)
{
options = RequestOptions.CreateOrClone(options);
return await SendAsync<GetBotGatewayResponse>("GET", () => "gateway/bot", new BucketIds(), options: options).ConfigureAwait(false);
}

//Channels
public async Task<Channel> GetChannelAsync(ulong channelId, RequestOptions options = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Discord.Net.Rest/DiscordRestClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Threading.Tasks;
Expand Down
2 changes: 1 addition & 1 deletion src/Discord.Net.WebSocket/BaseSocketClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Discord.API;
Expand Down
6 changes: 3 additions & 3 deletions src/Discord.Net.WebSocket/DiscordShardedClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Discord.API;
using Discord.API;
using Discord.Rest;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -75,8 +75,8 @@ internal override async Task OnLoginAsync(TokenType tokenType, string token)
{
if (_automaticShards)
{
var response = await ApiClient.GetBotGatewayAsync().ConfigureAwait(false);
_shardIds = Enumerable.Range(0, response.Shards).ToArray();
var shardCount = await GetRecommendedShardCountAsync().ConfigureAwait(false);
_shardIds = Enumerable.Range(0, shardCount).ToArray();
_totalShards = _shardIds.Length;
_shards = new DiscordSocketClient[_shardIds.Length];
for (int i = 0; i < _shardIds.Length; i++)
Expand Down
15 changes: 2 additions & 13 deletions src/Discord.Net.WebSocket/DiscordSocketApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma warning disable CS1591
#pragma warning disable CS1591
using Discord.API.Gateway;
using Discord.API.Rest;
using Discord.Net.Queue;
Expand Down Expand Up @@ -207,18 +207,7 @@ private async Task SendGatewayInternalAsync(GatewayOpCode opCode, object payload
await RequestQueue.SendAsync(new WebSocketRequest(WebSocketClient, null, bytes, true, options)).ConfigureAwait(false);
await _sentGatewayMessageEvent.InvokeAsync(opCode).ConfigureAwait(false);
}

//Gateway
public async Task<GetGatewayResponse> GetGatewayAsync(RequestOptions options = null)
{
options = RequestOptions.CreateOrClone(options);
return await SendAsync<GetGatewayResponse>("GET", () => "gateway", new BucketIds(), options: options).ConfigureAwait(false);
}
public async Task<GetBotGatewayResponse> GetBotGatewayAsync(RequestOptions options = null)
{
options = RequestOptions.CreateOrClone(options);
return await SendAsync<GetBotGatewayResponse>("GET", () => "gateway/bot", new BucketIds(), options: options).ConfigureAwait(false);
}

public async Task SendIdentifyAsync(int largeThreshold = 100, int shardID = 0, int totalShards = 1, RequestOptions options = null)
{
options = RequestOptions.CreateOrClone(options);
Expand Down

0 comments on commit fc5e70c

Please sign in to comment.