Ejemplo n.º 1
0
        /// <summary>
        ///     Asynchronous start of the connection.
        /// </summary>
        public async Task Start()
        {
            while (RestartHandler.KeepAlive)
            {
                try
                {
                    DownloadPrerequisites();
                    var config = _config.Read();
                    if (string.IsNullOrWhiteSpace(config.Token))
                    {
                        return;
                    }
                    _client = new DiscordSocketClient(new DiscordSocketConfig
                    {
                        LogLevel = LogSeverity.Verbose
                    });
                    _client.Log += Log;

                    IServiceCollection serviceCollection = new ServiceCollection();
                    ConfigureServices(serviceCollection);
                    _services = serviceCollection.BuildServiceProvider();

                    await _client.LoginAsync(TokenType.Bot, config.Token);

                    await _client.StartAsync();

                    await _client.SetActivityAsync(new Game("her Yukiteru Diary", ActivityType.Watching));

                    await _commandHandler.Initialize(_client, _services);

                    await _channelHandler.Initialize(_client, _services);

                    await _roleHandler.Initialize(_client, _services);

                    await _dblHandler.Initialize(_client, config);

                    await RestartHandler.AwaitRestart();
                }
                catch (Exception e)
                {
                    LogsHandler.Instance.Log("Main", $"Fatal exception occured. Restarting bot. Traceback: {e}");
                }
            }
        }