Esempio n. 1
0
        /// <summary>
        /// Runs the <see cref="IDiscordBot"/>.
        /// </summary>
        private async Task <int> RunAsync()
        {
            discordBot.LoadConfig();

            if (allowMultipleInstances || !SingleInstanceCheck())
            {
                return(0);
            }

            services = await discordBot.InitializeAsync(ConfigureServices()).ConfigureAwait(false);

            discordBot.ShuttingDown += OnShuttingDownAsync;

            // Start the startup service
            services.GetRequiredService <DiscordSocketClient>().Ready += OnReadyAsync;
            await services.GetRequiredService <StartupService>().StartAsync().ConfigureAwait(false);

            await discordBot.StartAsync().ConfigureAwait(false);

            // Keep the program alive until shutdown/restart
            shutdownEvent.WaitOne();

            // Wait a moment to prevent wonkey Discord message output
            //await Task.Delay(TimeSpan.FromSeconds(2)).ConfigureAwait(false);

            // Allow time to go invisible
            await Task.Delay(TimeSpan.FromSeconds(1.5)).ConfigureAwait(false);

            await services.GetRequiredService <DiscordSocketClient>().StopAsync().ConfigureAwait(false);

            mutex?.Dispose();
            services.Dispose();

            if (shutdownMode == ShutdownMode.Restart)
            {
                if (isRunningFromDaemon)
                {
                    return(RestartExitCode);
                }

                // Restart right here, right now
                string arguments = string.Join(" ", args.Select(a => $"\"{a}\""));
#if NETSTANDARD2_0
                Process.Start("dotnet", $"{(Assembly.GetEntryAssembly().Location)} {arguments}");
#else
                Process.Start(Assembly.GetEntryAssembly().Location, arguments);
#endif

                // Double clear is needed to ensure *actually cleared* console. It's weird.
                Console.Clear();
                Console.Clear();
                Environment.Exit(0);
            }
            return(0);
        }