Esempio n. 1
0
        public async void Start()
        {
            _commandService = new CommandService(new CommandServiceConfig
            {
                CaseSensitiveCommands = false,
            });

            _client            = new DiscordSocketClient();
            _client.Log       += LogAsync;
            _client.Connected += Connected;
            _commandService.CommandExecuted += CommandExecutedAsync;
            _client.MessageReceived         += MessageReceivedAsync;

            Task.Run(async() =>
            {
                while (true)
                {
                    try
                    {
                        var newSettings = _discordSettingsProvider.Provide();
                        var mustRestart = false;

                        try
                        {
                            if (_client.ConnectionState == ConnectionState.Connected)
                            {
                                mustRestart = string.IsNullOrEmpty(_client.CurrentUser.Username);
                            }
                        }
                        catch
                        {
                            mustRestart = true;
                        }

                        if (mustRestart)
                        {
                            _logger.LogWarning("Restarting bot due to incorrect automatic reconnection.");
                        }

                        if (!_currentSettings.Equals(newSettings) || mustRestart || _client.ConnectionState == ConnectionState.Disconnected)
                        {
                            _logger.LogWarning("Bot configuration changed/not connected to Discord: restarting bot");
                            _currentSettings = newSettings;
                            await RestartBot(newSettings);
                            _logger.LogWarning("Bot has been restarted.");
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Error while restarting the bot: " + ex.Message);
                    }

                    await Task.Delay(5000);
                }
            });
        }
Esempio n. 2
0
        public async void Start()
        {
            Task.Run(async() =>
            {
                while (true)
                {
                    try
                    {
                        var previousGuildCount = _currentGuilds.Count;
                        var newSettings        = _discordSettingsProvider.Provide();

                        try
                        {
                            var newGuilds = new HashSet <ulong>(_client?.Guilds.Keys.ToArray() ?? Array.Empty <ulong>());

                            if (newGuilds.Any())
                            {
                                _currentGuilds.UnionWith(newGuilds);
                            }
                        }
                        catch (System.Exception) { }

                        if (!_currentSettings.Equals(newSettings) || Language.Current != _previousLanguage || _currentGuilds.Count != previousGuildCount)
                        {
                            var previousSettings = _currentSettings;
                            _logger.LogWarning("Bot configuration changed: restarting bot");
                            _currentSettings  = newSettings;
                            _previousLanguage = Language.Current;
                            await RestartBot(previousSettings, newSettings, _currentGuilds);
                            _logger.LogWarning("Bot has been restarted.");

                            SlashCommandBuilder.CleanUp();
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "Error while restarting the bot: " + ex.Message);
                    }

                    await Task.Delay(5000);
                }
            });
        }
Esempio n. 3
0
        public async void Start()
        {
            _commandService = new CommandService(new CommandServiceConfig
            {
                CaseSensitiveCommands = false,
            });

            _client            = new DiscordSocketClient();
            _client.Log       += LogAsync;
            _client.Connected += Connected;
            _commandService.CommandExecuted += CommandExecutedAsync;
            _client.MessageReceived         += MessageReceivedAsync;

            Task.Run(async() =>
            {
                while (true)
                {
                    try
                    {
                        var newSettings = _discordSettingsProvider.Provide();

                        if (!_currentSettings.Equals(newSettings) || _client.ConnectionState == ConnectionState.Disconnected)
                        {
                            _logger.LogWarning("Bot changes detected: restarting bot");
                            _currentSettings = newSettings;
                            await RestartBot(newSettings);
                            _logger.LogWarning("Bot changes detected: bot restarted");
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogWarning(ex.Message);
                    }

                    await Task.Delay(5000);
                }
            });
        }