protected override async Task ExecuteAsync(CancellationToken stoppingToken) { async void OnCancellation(object?_, CancellationToken ct) { _discordSocketClient.MessageReceived -= Bot_MessageReceived; _discordSocketClient.Log -= Bot_Log; await _discordSocketClient.StopAsync(); _logger.LogWarning("Stopped Discord Bot"); _eventSubscriber.Dispose(); } using var scope = _scopeFactory.CreateScope(); await _commands.AddModulesAsync(typeof(DiscordCommands).Assembly, scope.ServiceProvider); if (_discordSocketClient.ConnectionState != ConnectionState.Disconnecting && _discordSocketClient.ConnectionState != ConnectionState.Disconnected) { return; } var botToken = _options.BotToken; if (string.IsNullOrEmpty(botToken)) { _logger.LogError("Error while getting Discord.BotToken! Check your configuration file"); return; } _discordSocketClient.MessageReceived += Bot_MessageReceived; _discordSocketClient.Log += Bot_Log; await _discordSocketClient.LoginAsync(TokenType.Bot, botToken); await _discordSocketClient.StartAsync(); await _retryPolicy.ExecuteAsync(async token => await _eventSubscriber.Subscribe(token), stoppingToken); _logger.LogWarning("Started Discord Bot"); #if NET5_0 stoppingToken.Register(_ => OnCancellation(null, stoppingToken), null); #else stoppingToken.Register(OnCancellation, null); #endif }
public void Dispose() { if (_disposed) { return; } _disposed = true; _subscriber.OnEventReceived -= Event_Received; try { _publisher.Dispose(); _subscriber.Dispose(); } catch (IOException ex) { _logger.LogCritical(ex.ToString()); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { void OnCancellation(object?_, CancellationToken ct) { _bot.OnMessage -= Bot_OnMessageReceived; _logger.LogWarning("Stopped Slack Bot"); _eventSubscriber.Dispose(); } _bot.OnMessage += Bot_OnMessageReceived; await _bot.Connect(stoppingToken); await _retryPolicy.ExecuteAsync(async token => await _eventSubscriber.Subscribe(token), stoppingToken); _logger.LogWarning("Started Slack Bot"); #if NET5_0 stoppingToken.Register(_ => OnCancellation(null, stoppingToken), null); #else stoppingToken.Register(OnCancellation, null); #endif }
public void Dispose() { _cancellationTokenSource.Cancel(); _subscriber.Dispose(); }