Пример #1
0
        private async Task LoginAsync()
        {
            Console.Title         = "Volte";
            Console.CursorVisible = false;

            if (!Directory.Exists("data"))
            {
                Console.WriteLine("The \"data\" directory didn't exist, so I created it for you.");
                Directory.CreateDirectory("data");
                //99.9999999999% of the time the config also won't exist if this block is reached
            }

            if (!Config.CreateIfNotExists())
            {
                Console.WriteLine($"Please fill in the configuration located at \"{Config.ConfigFile}\"; restart me when you've done so.");
                return;
            }

            Config.Load();

            if (Config.Token.IsNullOrEmpty() || Config.Token.EqualsIgnoreCase("token here"))
            {
                return;
            }

            using var rest = new DiscordRestClient();

            await rest.LoginAsync(TokenType.Bot, Config.Token);

            var shardCount = await rest.GetRecommendedShardCountAsync();

            await rest.LogoutAsync();

            BuildServiceProvider(shardCount, out var provider);

            provider.Get <DiscordShardedClient>(out var client);
            provider.Get <CancellationTokenSource>(out var cts);
            provider.Get <VolteHandler>(out var handler);
            provider.Get <LoggingService>(out var logger);

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

            await client.StartAsync().ContinueWith(_ => client.SetStatusAsync(UserStatus.Online));

            await handler.InitializeAsync(provider);

            try
            {
                await Task.Delay(-1, cts.Token);
            }
            catch (TaskCanceledException)
            {
                //this exception always occurs when CancellationTokenSource#Cancel() is called; so we put the shutdown logic inside the catch block
                logger.Critical(LogSource.Volte,
                                "Bot shutdown requested by the bot owner; shutting down.");
                await ShutdownAsync(client, cts);
            }
        }
Пример #2
0
        /// <summary>
        ///     Gets the recommended shard count from Discord by logging into a <see cref="DiscordRestClient"/> via the value in <see cref="Config"/>.<see cref="Config.Token"/>.
        ///     This method assumes that the value in the config has already been validated and is usable with Discord.
        /// </summary>
        /// <returns></returns>
        public static async ValueTask <int> GetRecommendedShardCountAsync()
        {
            using var client = new DiscordRestClient();
            await client.LoginAsync(TokenType.Bot, Config.Token);

            var res = await client.GetRecommendedShardCountAsync();

            await client.LogoutAsync();

            return(res);
        }