Ejemplo n.º 1
0
        public async Task Start()
        {
            var settings = new MainSettings("Config/mainSettings.xml", "Config/Local/mainSettings.xml");

            settings.LoadLanguage();

            var connection = new SqliteConnection($"Data Source={settings.DatabasePath};");

            connection.Open();
            Migrate(connection);

            Connection = connection;
            Settings   = settings;

            client      = new DiscordSocketClient();
            client.Log += Log;

            Func <Task> clientReadyHandler = null;

            client.Ready += clientReadyHandler = async() =>
            {
                _game = new Game(SyncContext, client, settings);

                commands = new CommandService();

                var serviceCollection = new ServiceCollection();
                serviceCollection.AddSingleton(client);
                serviceCollection.AddSingleton(connection);
                serviceCollection.AddSingleton(settings);
                serviceCollection.AddSingleton(commands);
                serviceCollection.AddSingleton(_game);

                services = serviceCollection.BuildServiceProvider();

                await InstallCommands();

                await client.SetGameAsync(null);

                client.Ready -= clientReadyHandler;
            };

            await client.LoginAsync(TokenType.Bot, Settings.Token);

            await client.StartAsync();

            try
            {
                SyncContext.RunMessagePump();
            }
            catch (Exception ex)
            {
                var message = $"[{DateTime.Now:s}] {ex}";
                Console.Error.WriteLine(message);
                System.IO.File.AppendAllText("error.log", message);
            }

            await Task.CompletedTask;
        }
Ejemplo n.º 2
0
        public async Task Start()
        {
            var settings = new MainSettings("Config/mainSettings.xml", "Config/Local/mainSettings.xml");

            settings.LoadLanguage();

            var connection = new SqliteConnection($"Data Source={settings.DatabasePath};");

            connection.Open();
            Migrate(connection);

            Connection = connection;
            Settings   = settings;

            async Task clientReadyHandler()
            {
                commands = new CommandService();

                var serviceCollection = new ServiceCollection();

                serviceCollection.AddSingleton(clientWrapper.MainClient);
                serviceCollection.AddSingleton(clientWrapper);
                serviceCollection.AddSingleton(connection);
                serviceCollection.AddSingleton(settings);
                serviceCollection.AddSingleton(commands);
                serviceCollection.AddSingleton <Services.Notifier>();
                serviceCollection.AddSingleton <System.Threading.SynchronizationContext>(SyncContext);
                serviceCollection.AddSingleton <Game>();
                serviceCollection.AddSingleton <Base.Game.IGame>(p => p.GetRequiredService <Game>());
                serviceCollection.AddSingleton <DIContractResolver>();

                services = serviceCollection.BuildServiceProvider();
                _game    = services.GetRequiredService <Game>();
                _game.LoadSettings();

                await InstallCommands();

                await clientWrapper.MainClient.SetGameAsync(null);

                clientWrapper.MainClient.Ready -= clientReadyHandler;
            }

            clientWrapper = new DiscordClientWrapper(settings, clientReadyHandler);

            clientWrapper.MainClient.Log += Log;

            if (clientWrapper.MainClient != clientWrapper.AnnouncerClient)
            {
                clientWrapper.AnnouncerClient.Log += AnnouncerLog;
            }

            try
            {
                SyncContext.RunMessagePump();
            }
            catch (Exception ex)
            {
                var message = $"[{DateTime.Now:s}] {ex}";
                Console.Error.WriteLine(message);
                System.IO.File.AppendAllText("error.log", message);
            }

            await Task.CompletedTask;
        }