Browse Source

Implemented passing the gateway URL down from DiscordShardedClient.

pull/2423/head
Quahu 2 years ago
parent
commit
2329f1d362
2 changed files with 33 additions and 8 deletions
  1. +11
    -1
      src/Discord.Net.WebSocket/DiscordShardedClient.cs
  2. +22
    -7
      src/Discord.Net.WebSocket/DiscordSocketApiClient.cs

+ 11
- 1
src/Discord.Net.WebSocket/DiscordShardedClient.cs View File

@@ -139,9 +139,9 @@ namespace Discord.WebSocket

internal override async Task OnLoginAsync(TokenType tokenType, string token)
{
var botGateway = await GetBotGatewayAsync().ConfigureAwait(false);
if (_automaticShards)
{
var botGateway = await GetBotGatewayAsync().ConfigureAwait(false);
_shardIds = Enumerable.Range(0, botGateway.Shards).ToArray();
_totalShards = _shardIds.Length;
_shards = new DiscordSocketClient[_shardIds.Length];
@@ -163,7 +163,12 @@ namespace Discord.WebSocket

//Assume thread safe: already in a connection lock
for (int i = 0; i < _shards.Length; i++)
{
// Set the gateway URL to the one returned by Discord, if a custom one isn't set.
_shards[i].ApiClient.GatewayUrl = botGateway.Url;

await _shards[i].LoginAsync(tokenType, token);
}

if(_defaultStickers.Length == 0 && _baseConfig.AlwaysDownloadDefaultStickers)
await DownloadDefaultStickersAsync().ConfigureAwait(false);
@@ -175,7 +180,12 @@ namespace Discord.WebSocket
if (_shards != null)
{
for (int i = 0; i < _shards.Length; i++)
{
// Reset the gateway URL set for the shard.
_shards[i].ApiClient.GatewayUrl = null;

await _shards[i].LogoutAsync();
}
}

if (_automaticShards)


+ 22
- 7
src/Discord.Net.WebSocket/DiscordSocketApiClient.cs View File

@@ -38,6 +38,24 @@ namespace Discord.API

public ConnectionState ConnectionState { get; private set; }

/// <summary>
/// Sets the gateway URL used for identifies.
/// </summary>
/// <remarks>
/// If a custom URL is set, setting this property does nothing.
/// </remarks>
public string GatewayUrl
{
set
{
// Makes the sharded client not override the custom value.
if (_isExplicitUrl)
return;

_gatewayUrl = FormatGatewayUrl(value);
}
}

/// <summary>
/// Sets the gateway URL used for resumes.
/// </summary>
@@ -214,16 +232,13 @@ namespace Discord.API
string gatewayUrl;
if (_resumeGatewayUrl == null)
{
if (!_isExplicitUrl)
if (!_isExplicitUrl && _gatewayUrl == null)
{
// TODO: pass this down from DiscordShardedClient, so that it's not requested separately for every single shard
var gatewayResponse = await GetBotGatewayAsync().ConfigureAwait(false);
gatewayUrl = _gatewayUrl = FormatGatewayUrl(gatewayResponse.Url);
}
else
{
gatewayUrl = _gatewayUrl;
_gatewayUrl = FormatGatewayUrl(gatewayResponse.Url);
}

gatewayUrl = _gatewayUrl;
}
else
{


Loading…
Cancel
Save