From d83b357ec372b0357019036f6171fe6016dd66f8 Mon Sep 17 00:00:00 2001 From: barelyprofessional <150058423+barelyprofessional@users.noreply.github.com> Date: Wed, 10 Dec 2025 20:06:15 -0600 Subject: [PATCH] Using a proper cancellation token for commands rather than the shared one, so timeouts should be handled more gracefully and also hopefully maybe actually consistently work. Also added error reporting --- KfChatDotNetBot/Services/BotCommands.cs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/KfChatDotNetBot/Services/BotCommands.cs b/KfChatDotNetBot/Services/BotCommands.cs index 9213eb2..da299c4 100644 --- a/KfChatDotNetBot/Services/BotCommands.cs +++ b/KfChatDotNetBot/Services/BotCommands.cs @@ -122,21 +122,37 @@ internal class BotCommands private async Task ProcessMessageAsync(ICommand command, MessageModel message, UserDbModel user, GroupCollection arguments) { - var task = Task.Run(() => command.RunCommand(_bot, message, user, arguments, _cancellationToken), _cancellationToken); + var cts = new CancellationTokenSource(command.Timeout); + var task = Task.Run(() => command.RunCommand(_bot, message, user, arguments, cts.Token), cts.Token); try { - await task.WaitAsync(command.Timeout, _cancellationToken); + await task.WaitAsync(command.Timeout, cts.Token); + } + catch (OperationCanceledException e) + { + _logger.Error($"{command.GetType().Name} invoked by {user.KfUsername} timed out"); + _logger.Error(e); + await _bot.SendChatMessageAsync( + $"{user.FormatUsername()}, {command.GetType().Name} failed due to a timeout :(", true, + autoDeleteAfter: TimeSpan.FromSeconds(10)); + return; } catch (Exception e) { - _logger.Error("Caught an exception while waiting for the command to complete"); + _logger.Error($"{command.GetType().Name} invoked by {user.KfUsername} failed"); _logger.Error(e); + await _bot.SendChatMessageAsync( + $"{user.FormatUsername()}, {command.GetType().Name} failed due to a retarded error :(", true, + autoDeleteAfter: TimeSpan.FromSeconds(10)); return; } if (task.IsFaulted) { - _logger.Error("Command task failed"); + _logger.Error($"{command.GetType().Name} invoked by {user.KfUsername} faulted"); _logger.Error(task.Exception); + await _bot.SendChatMessageAsync( + $"{user.FormatUsername()}, {command.GetType().Name} failed due to shitty coding :(", true, + autoDeleteAfter: TimeSpan.FromSeconds(10)); return; }