Пример #1
0
        static void Main(string[] args)
        {
            var botType     = BotType.Discord;
            var shard       = 0;
            var totalShards = 1;

            if (args != null)
            {
                foreach (string arg in args)
                {
                    var argParts = arg.Split(new[] { ':' }, 2);

                    switch (argParts[0])
                    {
                    case "/?":
                        Console.WriteLine("dotnet UB3RB0T.dll [/t:type] [/s:shard]");
                        Console.WriteLine("/t:type \t The type of bot to create. [Irc, Discord]");
                        Console.WriteLine("/s:shard \t The shard for this instance (Discord only)");
                        Console.WriteLine("/c:path \t The path to botconfig.json");
                        return;

                    case "/t":
                        if (!Enum.TryParse(argParts[1], /* ignoreCase */ true, out botType))
                        {
                            throw new ArgumentException("Invalid bot type specified.");
                        }

                        break;

                    case "/c":
                        Environment.SetEnvironmentVariable(JsonConfig.PathEnvironmentVariableName, argParts[1]);
                        break;

                    case "/s":
                        shard = int.Parse(argParts[1]);
                        break;

                    case "/st":
                        totalShards = int.Parse(argParts[1]);
                        break;
                    }
                }
            }

            int exitCode = (int)ExitCode.Success;
            // Convert to async main method
            var instanceCount = 0;

            do
            {
                try
                {
                    using (var bot = Bot.Create(botType, shard, totalShards, instanceCount))
                    {
                        BotInstance = bot;
                        // Handle clean shutdown when possible
                        SetConsoleCtrlHandler((ctrlType) =>
                        {
                            bot.StopAsync().GetAwaiter().GetResult();
                            return(true);
                        }, true);

                        exitCode = bot.StartAsync().GetAwaiter().GetResult();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                instanceCount++;
            } while (exitCode == (int)ExitCode.UnexpectedError); // re-create the bot on failures.  Only exit if a clean shutdown occurs.

            Console.WriteLine("Game over man, game over!");
        }
Пример #2
0
        static int Main(string[] args)
        {
            var botType     = BotType.Discord;
            var shard       = 0;
            var totalShards = 1;

            if (args != null)
            {
                foreach (string arg in args)
                {
                    var argParts = arg.Split(new[] { ':' }, 2);

                    switch (argParts[0])
                    {
                    case "/?":
                        Console.WriteLine("dotnet UB3RB0T.dll [/t:type] [/s:shard]");
                        Console.WriteLine("/t:type \t The type of bot to create. [Irc, Discord]");
                        Console.WriteLine("/s:shard \t The shard for this instance (Discord only)");
                        Console.WriteLine("/c:path \t The path to botconfig.json");
                        return(0);

                    case "/t":
                        if (!Enum.TryParse(argParts[1], /* ignoreCase */ true, out botType))
                        {
                            throw new ArgumentException("Invalid bot type specified.");
                        }

                        break;

                    case "/c":
                        Environment.SetEnvironmentVariable(JsonConfig.PathEnvironmentVariableName, argParts[1]);
                        break;

                    case "/s":
                        shard = int.Parse(argParts[1]);
                        break;

                    case "/st":
                        totalShards = int.Parse(argParts[1]);
                        break;
                    }
                }
            }

            // Logging
            levelSwitch = new LoggingLevelSwitch
            {
                MinimumLevel = BotConfig.Instance.LogEventLevel
            };

            var logConfiguration = new LoggerConfiguration()
                                   .MinimumLevel.ControlledBy(levelSwitch)
                                   .Enrich.WithProperty("Shard", shard.ToString().PadLeft(2))
                                   .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Error,
                                                    outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} {Shard}] {Message:lj}{NewLine}{Exception}");

            if (!string.IsNullOrWhiteSpace(BotConfig.Instance.LogsPath))
            {
                logConfiguration.WriteTo.File($"{BotConfig.Instance.LogsPath}\\{botType}_shard{shard}_.txt",
                                              buffered: true,
                                              rollingInterval: RollingInterval.Day,
                                              flushToDiskInterval: TimeSpan.FromSeconds(5));
            }

            Log.Logger = logConfiguration.CreateLogger();

            // setup a watcher for configs
            var watcher = new FileSystemWatcher
            {
                Path         = JsonConfig.ConfigRootPath,
                NotifyFilter = NotifyFilters.LastWrite,
                Filter       = "*.json",
            };

            watcher.Changed += (source, e) =>
            {
                _ = ReloadConfigs();
            };

            watcher.EnableRaisingEvents = true;

            int exitCode = (int)ExitCode.Success;
            // Convert to async main method
            var instanceCount = 0;

            do
            {
                try
                {
                    using (var bot = Bot.Create(botType, shard, totalShards, instanceCount))
                    {
                        BotInstance = bot;
                        // Handle clean shutdown when possible
                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            SetConsoleCtrlHandler((ctrlType) =>
                            {
                                bot.StopAsync().GetAwaiter().GetResult();
                                return(true);
                            }, true);
                        }

                        Log.Information($"Starting shard {shard}");

                        exitCode = bot.StartAsync().GetAwaiter().GetResult();
                    }
                }
                catch (Exception ex)
                {
                    Log.Fatal(ex, "Failure in bot loop");
                }

                instanceCount++;
            } while (exitCode == (int)ExitCode.UnexpectedError); // re-create the bot on failures.  Only exit if a clean shutdown occurs.

            Log.CloseAndFlush();

            Log.Fatal("Game over man, {{GameOver}}", "game over!");

            return(exitCode);
        }