コード例 #1
0
        private async Task RestartBot(DiscordSettings previousSettings, DiscordSettings newSettings, HashSet <ulong> currentGuilds)
        {
            if (!string.IsNullOrEmpty(newSettings.BotToken))
            {
                if (!string.Equals(previousSettings.BotToken, newSettings.BotToken, StringComparison.OrdinalIgnoreCase))
                {
                    if (_client != null)
                    {
                        await _client.DisconnectAsync();

                        _client.Ready -= Connected;
                        _client.ComponentInteractionCreated -= DiscordComponentInteractionCreatedHandler;
                        _client.Dispose();
                    }

                    if (_slashCommands != null)
                    {
                        _slashCommands.SlashCommandErrored -= SlashCommandErrorHandler;
                    }

                    var config = new DiscordConfiguration()
                    {
                        Token                 = newSettings.BotToken,
                        TokenType             = TokenType.Bot,
                        AutoReconnect         = true,
                        MinimumLogLevel       = LogLevel.Warning,
                        Intents               = DiscordIntents.All,
                        ReconnectIndefinitely = true
                    };

                    _client = new DiscordClient(config);

                    _slashCommands = _client.UseSlashCommands(new SlashCommandsConfiguration
                    {
                        Services = new ServiceCollection()
                                   .AddSingleton <DiscordClient>(_client)
                                   .AddSingleton <ILogger>(_logger)
                                   .AddSingleton <DiscordSettingsProvider>(_discordSettingsProvider)
                                   .AddSingleton <MovieWorkflowFactory>(_movieWorkflowFactory)
                                   .AddSingleton <TvShowWorkflowFactory>(_tvShowWorkflowFactory)
                                   .BuildServiceProvider()
                    });

                    _slashCommands.SlashCommandErrored += SlashCommandErrorHandler;

                    _client.Ready += Connected;
                    _client.ComponentInteractionCreated += DiscordComponentInteractionCreatedHandler;

                    _currentGuilds = new HashSet <ulong>();
                    await _client.ConnectAsync();
                }

                if (_client != null)
                {
                    if (_client.Guilds.Any())
                    {
                        await ApplyBotConfigurationAsync(newSettings);

                        var prop = _slashCommands.GetType().GetProperty("_updateList", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
                        prop.SetValue(_slashCommands, new List <KeyValuePair <ulong?, Type> >());

                        var slashCommandType = SlashCommandBuilder.Build(_logger, newSettings, _serviceProvider.Get <RadarrSettingsProvider>(), _serviceProvider.Get <SonarrSettingsProvider>(), _serviceProvider.Get <OverseerrSettingsProvider>());

                        if (newSettings.EnableRequestsThroughDirectMessages)
                        {
                            try { _slashCommands.RegisterCommands(slashCommandType); }
                            catch (System.Exception ex) { _logger.LogError(ex, "Error while registering global slash commands: " + ex.Message); }

                            foreach (var guildId in _client.Guilds.Keys)
                            {
                                try { _slashCommands.RegisterCommands <EmptySlashCommands>(guildId); }
                                catch (System.Exception ex) { _logger.LogError(ex, $"Error while emptying guild-specific slash commands for guid {guildId}: " + ex.Message); }
                            }
                        }
                        else
                        {
                            try { _slashCommands.RegisterCommands <EmptySlashCommands>(); }
                            catch (System.Exception ex) { _logger.LogError(ex, "Error while emptying global slash commands: " + ex.Message); }

                            foreach (var guildId in _client.Guilds.Keys)
                            {
                                try { _slashCommands.RegisterCommands(slashCommandType, guildId); }
                                catch (System.Exception ex) { _logger.LogError(ex, $"Error while registering guild-specific slash commands for guid {guildId}: " + ex.Message); }
                            }
                        }

                        await _slashCommands.RefreshCommands();

                        await Task.Delay(TimeSpan.FromMinutes(1));
                    }
                }
                else
                {
                    _logger.LogWarning("No Bot Token for Discord has been configured.");
                }
            }
        }