chore: format with csharpier
This commit is contained in:
parent
2f516dcb73
commit
4f54077c68
59 changed files with 2000 additions and 942 deletions
|
|
@ -16,18 +16,22 @@ public class ShardedGatewayClient(
|
|||
IDiscordRestGatewayAPI gatewayApi,
|
||||
IServiceProvider services,
|
||||
IOptions<DiscordGatewayClientOptions> gatewayClientOptions,
|
||||
Config config)
|
||||
: IDisposable
|
||||
Config config
|
||||
) : IDisposable
|
||||
{
|
||||
private int _shardCount = config.Discord.ShardCount ?? 0;
|
||||
private readonly ILogger _logger = logger.ForContext<ShardedGatewayClient>();
|
||||
private readonly ConcurrentDictionary<int, DiscordGatewayClient> _gatewayClients = new();
|
||||
|
||||
private static readonly FieldInfo Field =
|
||||
typeof(DiscordGatewayClient).GetField("_connectionStatus", BindingFlags.Instance | BindingFlags.NonPublic)!;
|
||||
private static readonly FieldInfo Field = typeof(DiscordGatewayClient).GetField(
|
||||
"_connectionStatus",
|
||||
BindingFlags.Instance | BindingFlags.NonPublic
|
||||
)!;
|
||||
|
||||
private static readonly Func<DiscordGatewayClient, GatewayConnectionStatus> GetConnectionStatus =
|
||||
client => (GatewayConnectionStatus)Field.GetValue(client)!;
|
||||
private static readonly Func<
|
||||
DiscordGatewayClient,
|
||||
GatewayConnectionStatus
|
||||
> GetConnectionStatus = client => (GatewayConnectionStatus)Field.GetValue(client)!;
|
||||
|
||||
public IReadOnlyDictionary<int, DiscordGatewayClient> Shards => _gatewayClients;
|
||||
|
||||
|
|
@ -45,19 +49,26 @@ public class ShardedGatewayClient(
|
|||
if (_shardCount < discordShardCount && _shardCount != 0)
|
||||
_logger.Warning(
|
||||
"Discord recommends {DiscordShardCount} for this bot, but only {ConfigShardCount} shards are requested. This may cause issues later",
|
||||
discordShardCount, _shardCount);
|
||||
discordShardCount,
|
||||
_shardCount
|
||||
);
|
||||
|
||||
if (_shardCount == 0) _shardCount = discordShardCount;
|
||||
if (_shardCount == 0)
|
||||
_shardCount = discordShardCount;
|
||||
}
|
||||
|
||||
var clients = Enumerable.Range(0, _shardCount).Select(s =>
|
||||
{
|
||||
var client =
|
||||
ActivatorUtilities.CreateInstance<DiscordGatewayClient>(services,
|
||||
CloneOptions(gatewayClientOptions.Value, s));
|
||||
_gatewayClients[s] = client;
|
||||
return client;
|
||||
}).ToArray();
|
||||
var clients = Enumerable
|
||||
.Range(0, _shardCount)
|
||||
.Select(s =>
|
||||
{
|
||||
var client = ActivatorUtilities.CreateInstance<DiscordGatewayClient>(
|
||||
services,
|
||||
CloneOptions(gatewayClientOptions.Value, s)
|
||||
);
|
||||
_gatewayClients[s] = client;
|
||||
return client;
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
var tasks = new List<Task<Result>>();
|
||||
|
||||
|
|
@ -69,7 +80,10 @@ public class ShardedGatewayClient(
|
|||
var res = client.RunAsync(ct);
|
||||
tasks.Add(res);
|
||||
|
||||
while (GetConnectionStatus(client) is not GatewayConnectionStatus.Connected && !res.IsCompleted)
|
||||
while (
|
||||
GetConnectionStatus(client) is not GatewayConnectionStatus.Connected
|
||||
&& !res.IsCompleted
|
||||
)
|
||||
{
|
||||
await Task.Delay(100, ct);
|
||||
}
|
||||
|
|
@ -92,7 +106,9 @@ public class ShardedGatewayClient(
|
|||
public DiscordGatewayClient ClientFor(ulong guildId) =>
|
||||
_gatewayClients.TryGetValue(ShardIdFor(guildId), out var client)
|
||||
? client
|
||||
: throw new CataloggerError("Shard was null, has ShardedGatewayClient.RunAsync been called?");
|
||||
: throw new CataloggerError(
|
||||
"Shard was null, has ShardedGatewayClient.RunAsync been called?"
|
||||
);
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
|
|
@ -100,7 +116,10 @@ public class ShardedGatewayClient(
|
|||
client.Dispose();
|
||||
}
|
||||
|
||||
private IOptions<DiscordGatewayClientOptions> CloneOptions(DiscordGatewayClientOptions options, int shardId)
|
||||
private IOptions<DiscordGatewayClientOptions> CloneOptions(
|
||||
DiscordGatewayClientOptions options,
|
||||
int shardId
|
||||
)
|
||||
{
|
||||
var ret = new DiscordGatewayClientOptions
|
||||
{
|
||||
|
|
@ -112,9 +131,9 @@ public class ShardedGatewayClient(
|
|||
LargeThreshold = options.LargeThreshold,
|
||||
CommandBurstRate = options.CommandBurstRate,
|
||||
HeartbeatSafetyMargin = options.HeartbeatSafetyMargin,
|
||||
MinimumSafetyMargin = options.MinimumSafetyMargin
|
||||
MinimumSafetyMargin = options.MinimumSafetyMargin,
|
||||
};
|
||||
|
||||
return Options.Create(ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue