PluralKit/PluralKit.Bot/Services/ShardInfoService.cs

59 lines
1.7 KiB
C#
Raw Normal View History

2019-12-22 11:50:47 +00:00
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DSharpPlus;
2019-12-22 11:50:47 +00:00
using NodaTime;
namespace PluralKit.Bot
{
public class ShardInfoService
{
public class ShardInfo
{
public Instant LastConnectionTime;
public int DisconnectionCount;
public int ShardLatency;
}
private Dictionary<int, ShardInfo> _shardInfo = new Dictionary<int, ShardInfo>();
public void Init(DiscordShardedClient client)
{
foreach (var i in client.ShardClients.Keys)
2019-12-22 11:50:47 +00:00
_shardInfo[i] = new ShardInfo();
// TODO
// client.ShardConnected += ShardConnected;
// client.ShardDisconnected += ShardDisconnected;
// client.ShardReady += ShardReady;
// client.ShardLatencyUpdated += ShardLatencyUpdated;
2019-12-22 11:50:47 +00:00
}
public ShardInfo GetShardInfo(DiscordClient shard) => _shardInfo[shard.ShardId];
2019-12-22 11:50:47 +00:00
private Task ShardLatencyUpdated(int oldLatency, int newLatency, DiscordClient shard)
2019-12-22 11:50:47 +00:00
{
_shardInfo[shard.ShardId].ShardLatency = newLatency;
return Task.CompletedTask;
}
private Task ShardReady(DiscordClient shard)
2019-12-22 11:50:47 +00:00
{
return Task.CompletedTask;
}
private Task ShardDisconnected(Exception e, DiscordClient shard)
2019-12-22 11:50:47 +00:00
{
_shardInfo[shard.ShardId].DisconnectionCount++;
return Task.CompletedTask;
}
private Task ShardConnected(DiscordClient shard)
2019-12-22 11:50:47 +00:00
{
_shardInfo[shard.ShardId].LastConnectionTime = SystemClock.Instance.GetCurrentInstant();
return Task.CompletedTask;
}
}
}