Ejemplo n.º 1
0
        /// <summary>
        /// Asynchronous entry point of the bot's binary.
        /// </summary>
        /// <param name="args">Command-line arguments for the binary.</param>
        /// <returns></returns>
        private static async Task MainAsync(string[] args)
        {
            Console.WriteLine("Loading Companion Cube...");
            Console.Write("[1/4] Loading configuration         ");

            // locate the config file
            var cfgFile = new FileInfo("config.json");

            // load the config file and validate it
            var cfgLoader = new CompanionCubeConfigLoader();

            if (!cfgFile.Exists)
            {
                await cfgLoader.SaveConfigurationAsync(new CompanionCubeConfig(), cfgFile);

                return;
            }
            var cfg = await cfgLoader.LoadConfigurationAsync(cfgFile).ConfigureAwait(false);

            cfgLoader.ValidateConfiguration(cfg);

            Console.Write("\r[2/4] Loading unicode data          ");

            // load unicode data
            using (var utfloader = new UnicodeDataLoader("unicode_data.json.gz"))
                await utfloader.LoadCodepointsAsync().ConfigureAwait(false);

            Console.Write("\r[3/4] Validating PostgreSQL database");

            Console.Write("\r[4/4] Creating and booting shards   ");

            // create shards
            Shards = new Dictionary <int, CompanionCubeBot>();
            var async = new AsyncExecutor();

            for (int i = 0; i < cfg.Discord.ShardCount; i++)
            {
                Shards[i] = new CompanionCubeBot(cfg, i, async);
            }

            // --- LOADING COMPLETED ---
            Console.WriteLine("\rLoading completed, booting the shards");
            Console.WriteLine("-------------------------------------");

            // boot shards
            foreach (var(k, shard) in Shards)
            {
                await shard.StartAsync();
            }

            // do a minimal cleanup
            GC.Collect();

            // wait forever
            await Task.Delay(-1);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Asynchronous entry point of the bot's binary.
        /// </summary>
        /// <param name="args">Command-line arguments for the binary.</param>
        /// <returns></returns>
        private static async Task MainAsync(string[] args)
        {
            Console.WriteLine("Loading Companion Cube...");
            Console.Write("[1/4] Loading configuration         ");

            // locate the config file
            var dockerSecret = Environment.GetEnvironmentVariable("DOCKER_SECRET");
            var cfgFile      = new FileInfo(dockerSecret != null ? Path.Combine("/run/secrets", dockerSecret) : "config.json");

            // load the config file and validate it
            var cfgLoader = new CompanionCubeConfigLoader();
            var cfg       = await cfgLoader.LoadConfigurationAsync(cfgFile);

            cfgLoader.ValidateConfiguration(cfg);

            Console.Write("\r[2/4] Loading unicode data          ");

            // load unicode data
            using (var utfloader = new UnicodeDataLoader("unicode_data.json.gz"))
                await utfloader.LoadCodepointsAsync();

            Console.Write("\r[3/4] Validating PostgreSQL database");

            // create database type mapping
            NpgsqlConnection.GlobalTypeMapper.MapEnum <DatabaseEntityKind>("entity_kind");
            NpgsqlConnection.GlobalTypeMapper.MapEnum <DatabaseTagKind>("tag_kind");

            // create database connection and validate schema
            var dbcsp = new ConnectionStringProvider(cfg.PostgreSQL);

            using (var db = new DatabaseContext(dbcsp))
            {
                var dbv = db.Metadata.SingleOrDefault(x => x.MetaKey == "schema_version");
                if (dbv == null || dbv.MetaValue != "8")
                {
                    throw new InvalidDataException("Database schema version mismatch.");
                }
                dbv = db.Metadata.SingleOrDefault(x => x.MetaKey == "project");
                if (dbv == null || dbv.MetaValue != "Companion Cube")
                {
                    throw new InvalidDataException("Database schema type mismatch.");
                }
            }

            Console.Write("\r[4/4] Creating and booting shards   ");

            // create shards
            Shards = new Dictionary <int, CompanionCubeBot>();
            var async = new AsyncExecutor();

            for (int i = 0; i < cfg.Discord.ShardCount; i++)
            {
                Shards[i] = new CompanionCubeBot(cfg, i, async);
            }

            // --- LOADING COMPLETED ---
            Console.WriteLine("\rLoading completed, booting the shards");
            Console.WriteLine("-------------------------------------");

            // boot shards
            foreach (var(k, shard) in Shards)
            {
                await shard.StartAsync();
            }

            // do a minimal cleanup
            GC.Collect();

            // wait forever
            await Task.Delay(-1);
        }