feat: update Discord status only on identify

See <https://github.com/discord/discord-api-docs/issues/4073#issuecomment-1016762755>

We still update status when restarting cluster, because it doesn't really matter if the session dies in that case (we're already restarting / going to reidentify)
This commit is contained in:
spiral 2022-01-20 05:52:40 -05:00
parent b586ef5d0a
commit c6e4c862b8
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
3 changed files with 31 additions and 63 deletions

View File

@ -16,6 +16,8 @@ public class Cluster
private readonly ConcurrentDictionary<int, Shard> _shards = new(); private readonly ConcurrentDictionary<int, Shard> _shards = new();
private IGatewayRatelimiter? _ratelimiter; private IGatewayRatelimiter? _ratelimiter;
public GatewayStatusUpdate DiscordPresence { get; set; }
public Cluster(GatewaySettings gatewaySettings, ILogger logger) public Cluster(GatewaySettings gatewaySettings, ILogger logger)
{ {
_gatewaySettings = gatewaySettings; _gatewaySettings = gatewaySettings;
@ -54,7 +56,7 @@ public class Cluster
private void CreateAndAddShard(string url, ShardInfo shardInfo) private void CreateAndAddShard(string url, ShardInfo shardInfo)
{ {
var shard = new Shard(_gatewaySettings, shardInfo, _ratelimiter!, url, _logger); var shard = new Shard(_gatewaySettings, shardInfo, _ratelimiter!, url, _logger, DiscordPresence);
shard.OnEventReceived += evt => OnShardEventReceived(shard, evt); shard.OnEventReceived += evt => OnShardEventReceived(shard, evt);
_shards[shardInfo.ShardId] = shard; _shards[shardInfo.ShardId] = shard;

View File

@ -41,12 +41,15 @@ public class Shard
private TimeSpan _reconnectDelay = TimeSpan.Zero; private TimeSpan _reconnectDelay = TimeSpan.Zero;
private Task? _worker; private Task? _worker;
public Shard(GatewaySettings settings, ShardInfo info, IGatewayRatelimiter ratelimiter, string url, ILogger logger) private GatewayStatusUpdate? _presence { get; init; }
public Shard(GatewaySettings settings, ShardInfo info, IGatewayRatelimiter ratelimiter, string url, ILogger logger, GatewayStatusUpdate? presence = null)
{ {
_jsonSerializerOptions = new JsonSerializerOptions().ConfigureForMyriad(); _jsonSerializerOptions = new JsonSerializerOptions().ConfigureForMyriad();
_settings = settings; _settings = settings;
_info = info; _info = info;
_presence = presence;
_ratelimiter = ratelimiter; _ratelimiter = ratelimiter;
_url = url; _url = url;
_logger = logger.ForContext<Shard>().ForContext("ShardId", info.ShardId); _logger = logger.ForContext<Shard>().ForContext("ShardId", info.ShardId);
@ -164,7 +167,8 @@ public class Shard
}, },
Shard = _info, Shard = _info,
Token = _settings.Token, Token = _settings.Token,
LargeThreshold = 50 LargeThreshold = 50,
Presence = _presence,
} }
}); });

View File

@ -34,7 +34,6 @@ public class Bot
private readonly DiscordApiClient _rest; private readonly DiscordApiClient _rest;
private readonly ILifetimeScope _services; private readonly ILifetimeScope _services;
private bool _hasReceivedReady;
private Timer _periodicTask; // Never read, just kept here for GC reasons private Timer _periodicTask; // Never read, just kept here for GC reasons
public Bot(ILifetimeScope services, ILogger logger, PeriodicStatCollector collector, IMetrics metrics, public Bot(ILifetimeScope services, ILogger logger, PeriodicStatCollector collector, IMetrics metrics,
@ -54,9 +53,23 @@ public class Bot
_cache = cache; _cache = cache;
} }
private string BotStatus => $"{(_config.Prefixes ?? BotConfig.DefaultPrefixes)[0]}help";
public void Init() public void Init()
{ {
_cluster.EventReceived += (shard, evt) => OnEventReceived(shard.ShardId, evt); _cluster.EventReceived += (shard, evt) => OnEventReceived(shard.ShardId, evt);
_cluster.DiscordPresence = new GatewayStatusUpdate
{
Status = GatewayStatusUpdate.UserStatus.Online,
Activities = new[]
{
new Activity
{
Type = ActivityType.Game,
Name = BotStatus
}
}
};
// Init the shard stuff // Init the shard stuff
_services.Resolve<ShardInfoService>().Init(); _services.Resolve<ShardInfoService>().Init();
@ -95,20 +108,6 @@ public class Bot
await HandleEvent(shardId, mra); await HandleEvent(shardId, mra);
if (evt is InteractionCreateEvent ic) if (evt is InteractionCreateEvent ic)
await HandleEvent(shardId, ic); await HandleEvent(shardId, ic);
// Update shard status for shards immediately on connect
if (evt is ReadyEvent re)
await HandleReady(shardId, re);
if (evt is ResumedEvent)
await HandleResumed(shardId);
}
private Task HandleResumed(int shardId) => UpdateBotStatus(shardId);
private Task HandleReady(int shardId, ReadyEvent _)
{
_hasReceivedReady = true;
return UpdateBotStatus(shardId);
} }
public async Task Shutdown() public async Task Shutdown()
@ -119,16 +118,15 @@ public class Bot
// Send users a lil status message // Send users a lil status message
// We're not actually properly disconnecting from the gateway (lol) so it'll linger for a few minutes // We're not actually properly disconnecting from the gateway (lol) so it'll linger for a few minutes
// Should be plenty of time for the bot to connect again next startup and set the real status // Should be plenty of time for the bot to connect again next startup and set the real status
if (_hasReceivedReady) await Task.WhenAll(_cluster.Shards.Values.Select(shard =>
await Task.WhenAll(_cluster.Shards.Values.Select(shard => shard.UpdateStatus(new GatewayStatusUpdate
shard.UpdateStatus(new GatewayStatusUpdate {
Activities = new[]
{ {
Activities = new[] new Activity {Name = "Restarting... (please wait)", Type = ActivityType.Game}
{ },
new Activity {Name = "Restarting... (please wait)", Type = ActivityType.Game} Status = GatewayStatusUpdate.UserStatus.Idle
}, })));
Status = GatewayStatusUpdate.UserStatus.Idle
})));
} }
private Task HandleEvent<T>(int shardId, T evt) where T : IGatewayEvent private Task HandleEvent<T>(int shardId, T evt) where T : IGatewayEvent
@ -238,45 +236,9 @@ public class Bot
{ {
_logger.Debug("Running once-per-minute scheduled tasks"); _logger.Debug("Running once-per-minute scheduled tasks");
await UpdateBotStatus();
// Collect some stats, submit them to the metrics backend // Collect some stats, submit them to the metrics backend
await _collector.CollectStats(); await _collector.CollectStats();
await Task.WhenAll(((IMetricsRoot)_metrics).ReportRunner.RunAllAsync()); await Task.WhenAll(((IMetricsRoot)_metrics).ReportRunner.RunAllAsync());
_logger.Debug("Submitted metrics to backend"); _logger.Debug("Submitted metrics to backend");
} }
private async Task UpdateBotStatus(int? specificShardId = null)
{
// If we're not on any shards, don't bother (this happens if the periodic timer fires before the first Ready)
if (!_hasReceivedReady) return;
var totalGuilds = await _cache.GetAllGuilds().CountAsync();
try // DiscordClient may throw an exception if the socket is closed (e.g just after OP 7 received)
{
Task UpdateStatus(Shard shard) =>
shard.UpdateStatus(new GatewayStatusUpdate
{
Activities = new[]
{
new Activity
{
Name = $"{(_config.Prefixes ?? BotConfig.DefaultPrefixes)[0]}help | in {totalGuilds:N0} servers | shard #{shard.ShardId}",
Type = ActivityType.Game,
Url = "https://pluralkit.me/"
}
}
});
if (specificShardId != null)
await UpdateStatus(_cluster.Shards[specificShardId.Value]);
else // Run shard updates concurrently
await Task.WhenAll(_cluster.Shards.Values.Select(UpdateStatus));
}
catch (WebSocketException)
{
// TODO: this still thrown?
}
}
} }