private async Task Login(string token) { await client.LoginAsync(TokenType.Bot, token); await client.StartAsync(); await Task.Delay(-1); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await InitializeHandlers(); RegisterHandlers(); await _client.LoginAsync(TokenType.Bot, _discordConfiguration.Token); await _client.StartAsync(); while (!stoppingToken.IsCancellationRequested) { // _logger.LogInformation("DiscordClientWorker running at: {time}", DateTimeOffset.Now); await Task.Delay(1000, stoppingToken); } _logger.LogInformation($"DiscordClientWorker task is stopping"); await _client.StopAsync(); }
private async Task Init(string token, TokenType type) { await _clientLogger.InfoAsync("Discord Manager Initialize....").ConfigureAwait(false); await LogManager.PrintVersion().ConfigureAwait(false); await _clientLogger.DebugAsync("Check Internet is Available").ConfigureAwait(false); if (!NetworkInterface.GetIsNetworkAvailable()) { throw new ManagerException( "UnAvailable Internet Check Your Pc/Server Internet State"); } await _clientLogger.DebugAsync("Check Token is Validated").ConfigureAwait(false); try { TokenUtils.ValidateToken(type, token); } catch (Exception e) { throw new ManagerException( "Token is Invalid. The token must be Validated", e); } await _clientLogger.DebugAsync("Successfully Check Token").ConfigureAwait(false); await _clientLogger.DebugAsync("Register Events...").ConfigureAwait(false); RegisterEvents(); await _clientLogger.DebugAsync("Successfully Register Events").ConfigureAwait(false); await Client.LoginAsync(type, token).ConfigureAwait(false); await Client.StartAsync().ConfigureAwait(false); await Task.Delay(-1); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("Discord Bot Hosted Service started"); await _client.LoginAsync(TokenType.Bot, _discordSettings.Token); await _client.StartAsync(); _client.MessageReceived += (eventArgs) => { // fire and forget _ = Task.Run(async() => { try { await PollyExceptionHandlerHelper.HandleExceptionAndRetry <SqlException>(OnMessageReceived(eventArgs), _logger, stoppingToken); } catch (HttpException ex) when(ex.Message.ContainsAny("50013", "50001", "Forbidden", "160002") || ex.HttpCode == HttpStatusCode.BadRequest) { _logger.LogWarning(ex, ex.Message); } catch (SqlException ex) when(ex.Message.Contains("SHUTDOWN")) { _logger.LogWarning(ex, "Sql Server shutdown in progress"); } catch (Exception ex) { _logger.LogError(ex, ex.Message); await _githubService.CreateBugIssue($"Application Exception: {ex.Message}", ex, GithubIssueLabels.Discord); } }); return(Task.CompletedTask); }; // Keep hosted service alive while receiving messages await Task.Delay(Timeout.Infinite, stoppingToken); }