From 6a213fa694d3377bf1b11608065a782748f5dcb6 Mon Sep 17 00:00:00 2001 From: spiral Date: Wed, 30 Mar 2022 20:42:57 -0400 Subject: [PATCH] feat: dispatch new bot status to clusters via Redis --- PluralKit.Bot/Bot.cs | 33 +++++++++++++++++++++++++++++++-- PluralKit.Bot/Init.cs | 8 ++++++-- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index d61cf8fa..0b595d0b 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -30,12 +30,13 @@ public class Bot private readonly ILogger _logger; private readonly IMetrics _metrics; private readonly DiscordApiClient _rest; + private readonly RedisService _redis; private readonly ILifetimeScope _services; private Timer _periodicTask; // Never read, just kept here for GC reasons public Bot(ILifetimeScope services, ILogger logger, PeriodicStatCollector collector, IMetrics metrics, - BotConfig config, + BotConfig config, RedisService redis, ErrorMessageService errorMessageService, CommandMessageService commandMessageService, Cluster cluster, DiscordApiClient rest, IDiscordCache cache) { @@ -48,10 +49,13 @@ public class Bot _commandMessageService = commandMessageService; _cluster = cluster; _rest = rest; + _redis = redis; _cache = cache; } - private string BotStatus => $"{(_config.Prefixes ?? BotConfig.DefaultPrefixes)[0]}help"; + private string BotStatus => $"{(_config.Prefixes ?? BotConfig.DefaultPrefixes)[0]}help" + + (CustomStatusMessage != null ? $" | {CustomStatusMessage}" : ""); + public string CustomStatusMessage = null; public void Init() { @@ -238,6 +242,31 @@ public class Bot { _logger.Debug("Running once-per-minute scheduled tasks"); + // Check from a new custom status from Redis and update Discord accordingly + if (_redis.Connection != null) + { + var newStatus = await _redis.Connection.GetDatabase().StringGetAsync("pluralkit:botstatus"); + if (newStatus != CustomStatusMessage) + { + CustomStatusMessage = newStatus; + + _logger.Information("Pushing new bot status message to Discord"); + await Task.WhenAll(_cluster.Shards.Values.Select(shard => + shard.UpdateStatus(new GatewayStatusUpdate + { + Activities = new[] + { + new Activity + { + Name = BotStatus, + Type = ActivityType.Game + } + }, + Status = GatewayStatusUpdate.UserStatus.Online + }))); + } + } + // Collect some stats, submit them to the metrics backend await _collector.CollectStats(); await Task.WhenAll(((IMetricsRoot)_metrics).ReportRunner.RunAllAsync()); diff --git a/PluralKit.Bot/Init.cs b/PluralKit.Bot/Init.cs index 7e96f6d5..40baf5db 100644 --- a/PluralKit.Bot/Init.cs +++ b/PluralKit.Bot/Init.cs @@ -61,11 +61,15 @@ public class Init await redis.Connection.GetDatabase().KeyDeleteAsync("pluralkit:shardstatus"); } - // Init the bot instance itself, register handlers and such to the client before beginning to connect logger.Information("Initializing bot"); var bot = services.Resolve(); - bot.Init(); + // Get bot status message from Redis + if (redis.Connection != null) + bot.CustomStatusMessage = await redis.Connection.GetDatabase().StringGetAsync("pluralkit:botstatus"); + + // Init the bot instance itself, register handlers and such to the client before beginning to connect + bot.Init(); // Start the Discord shards themselves (handlers already set up) logger.Information("Connecting to Discord");