Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempts to resolve #961 #962

Merged
merged 10 commits into from
Mar 15, 2018
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 @@ -277,6 +277,18 @@ public async Task ValidateTokenAsync(RequestOptions options = null)
await SendAsync("GET", () => "auth/login", new BucketIds(), options: options).ConfigureAwait(false);
}

//Gateway
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why were these moved to REST instead of being left with gateway?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was told it was a better place for them because they themselves are REST requests and should be accessible from that client as well.

I this it should be this way too. As before mentioned, since I have my bot set up in a more advanced configuration, it would be nice to be able to just access the data from the rest client so I can pass it to the individual clients who will then be establishing docket client connections from there.

I with it being there it is more open and therefor more flexible, even though this appears to be an edge case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fair to me

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