feat: dispatch new bot status to clusters via Redis
This commit is contained in:
		@@ -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());
 | 
			
		||||
 
 | 
			
		||||
@@ -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>();
 | 
			
		||||
            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");
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user