Esempio n. 1
0
        private async Task StartDiscordBot(IApplicationBuilder app, ILogger <Startup> logger)
        {
            _logger = logger;
            var options = app.ApplicationServices.GetService <AppOptions>();

            DiscordClient      = app.ApplicationServices.GetRequiredService <DiscordSocketClient>();
            DiscordClient.Log += LogAsync;
            app.ApplicationServices.GetRequiredService <CommandService>().Log += LogAsync;
            await DiscordClient.LoginAsync(TokenType.Bot, options.Discord.Token).ConfigureAwait(false);

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

            CommandHandlingService = app.ApplicationServices.GetService <CommandHandlingService>();
            await CommandHandlingService.InstallCommandsAsync();
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                _discordClient.Log  += LogAsync;
                _commandService.Log += LogAsync;

                _logger.LogInformation("Logging in to Discord");
                await _discordClient.LoginAsync(TokenType.Bot, _options.Discord.Token);

                _logger.LogInformation("Starting Discord");
                await _discordClient.StartAsync();

                _logger.LogInformation("Installing discord commands");
                await _commandHandlingService.InstallCommandsAsync();
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Discord failed");
            }
        }