Exemple #1
0
        public static async Task Main(string[] args = null)
        {
            string defaultEnv = Path.Combine(SkuldAppContext.BaseDirectory, ".env");

            if (!File.Exists(defaultEnv))
            {
                Console.WriteLine("Copy .env.default into .env and enter details");
                return;
            }

            DotEnv.Load(new DotEnvOptions(envFilePaths: new[] { defaultEnv }));

            if (args.Contains("--pq"))
            {
                GeneratePixelQuery();
                return;
            }

            SkuldConfig Configuration = null;

            Log.Configure();

            if (!Directory.Exists(SkuldAppContext.StorageDirectory))
            {
                Directory.CreateDirectory(SkuldAppContext.StorageDirectory);
                Log.Verbose(Key, "Created Storage Directory", null);
            }
            if (!Directory.Exists(SkuldAppContext.FontDirectory))
            {
                Directory.CreateDirectory(SkuldAppContext.FontDirectory);
                Log.Verbose(Key, "Created Font Directory", null);
            }

            try
            {
                var database = new SkuldDbContextFactory().CreateDbContext();

                if (!database.Configurations.Any() ||
                    args.Contains("--newconf") ||
                    args.Contains("-nc"))
                {
                    var conf = new SkuldConfig();
                    database.Configurations.Add(conf);
                    await database.SaveChangesAsync().ConfigureAwait(false);

                    Log.Verbose(Key, $"Created new configuration with Id: {conf.Id}", null);

                    Log.Info(Key, $"Please fill out the configuration information in the database matching the Id \"{database.Configurations.LastOrDefault().Id}\"");
                    Console.ReadKey();
                    Environment.Exit(0);
                }

                var configId = SkuldAppContext.GetEnvVar(SkuldAppContext.ConfigEnvVar);

                var c = database.Configurations.Find(configId);

                Configuration = c ?? database.Configurations.FirstOrDefault();

                SkuldAppContext.SetConfigurationId(Configuration.Id);
            }
            catch (Exception ex)
            {
                Log.Critical(Key, ex.Message, null, ex);
            }

            if (Configuration.DiscordToken.IsNullOrWhiteSpace())
            {
                Log.Critical(Key, "You haven't provided a discord token, exiting", null);
                return;
            }

            await ConfigureBotAsync(
                Configuration,
                new DiscordSocketConfig
            {
                MessageCacheSize = 100,
                DefaultRetryMode = RetryMode.AlwaysRetry,
                LogLevel         = LogSeverity.Verbose,
                GatewayIntents   = GatewayIntents.Guilds |
                                   GatewayIntents.GuildMembers |
                                   GatewayIntents.GuildBans |
                                   GatewayIntents.GuildEmojis |
                                   GatewayIntents.GuildIntegrations |
                                   GatewayIntents.GuildWebhooks |
                                   GatewayIntents.GuildInvites |
                                   GatewayIntents.GuildVoiceStates |
                                   GatewayIntents.GuildMessages |
                                   GatewayIntents.GuildMessageReactions |
                                   GatewayIntents.DirectMessages |
                                   GatewayIntents.DirectMessageReactions
            },
                new CommandServiceConfig
            {
                CaseSensitiveCommands = false,
                DefaultRunMode        = RunMode.Async,
                LogLevel        = LogSeverity.Verbose,
                IgnoreExtraArgs = true
            },
                new MessageServiceConfig
            {
                Prefix    = Configuration.Prefix,
                AltPrefix = Configuration.AltPrefix
            }
                ).ConfigureAwait(false);

            Log.Info(Key, "Loaded Skuld v" + SkuldAppContext.Skuld.Key.Version);

            await StartBotAsync().ConfigureAwait(false);

            await Task.Delay(-1).ConfigureAwait(false);

            await StopBotAsync(Key);

            WebSocket.ShutdownServer();

            Environment.Exit(0);
        }