From acc619a21ef3d8f5da7f90550e7d22b96e6851b9 Mon Sep 17 00:00:00 2001 From: Sergey Gut Date: Sun, 29 Jan 2023 10:11:55 +0300 Subject: [PATCH] Fixes for cleanup all messages --- modules/Entertainment/Handler/Guess.cs | 33 +++++++++++++++++++------- modules/Entertainment/Handler/Roll.cs | 8 +++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/modules/Entertainment/Handler/Guess.cs b/modules/Entertainment/Handler/Guess.cs index fbb30b8..c701460 100644 --- a/modules/Entertainment/Handler/Guess.cs +++ b/modules/Entertainment/Handler/Guess.cs @@ -11,6 +11,7 @@ using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Service; using Telegram.Bot; +using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.ReplyMarkups; @@ -36,6 +37,14 @@ public class Guess : CommonHandler [Command(InChat.Public, "guess", CommandParseMode.Both)] public async Task HandleGuess() { + // Drop user message. + _requests.Add(new DeleteRequest + { + ChatId = Chat.Id, + DeleteAt = DateTime.UtcNow.AddSeconds(20), + MessageId = Message.MessageId + }); + var cooldownOption = await _db.UserValues.FindOrCreateOption(Chat.Id, From.Id, "guessCooldown"); var cooldown = cooldownOption.GetValue(); if (cooldown > DateTime.Now) @@ -79,14 +88,6 @@ public class Guess : CommonHandler return; } _guessCache.SetValue(Chat.Id, guessDict, -1); - - // Drop user message. - _requests.Add(new DeleteRequest - { - ChatId = Chat.Id, - DeleteAt = DateTime.UtcNow.AddSeconds(20), - MessageId = messageId - }); } [Update(InChat.Public, UpdateFlag.CallbackQuery)] @@ -157,4 +158,20 @@ public class Guess : CommonHandler MessageId = (int) messageId }); } + + protected override async Task Reply(string text) + { + // TODO: move feature with autocleaning to Core CommonHandler. + var message = await Bot.SendTextMessageAsync(Chat.Id, text, + replyToMessageId: Message?.MessageId, parseMode: ParseMode.Html); + + _requests.Add(new DeleteRequest + { + ChatId = Chat.Id, + DeleteAt = DateTime.UtcNow.AddSeconds(20), + MessageId = message.MessageId + }); + + return message; + } } \ No newline at end of file diff --git a/modules/Entertainment/Handler/Roll.cs b/modules/Entertainment/Handler/Roll.cs index 5d332dc..2fef7f9 100644 --- a/modules/Entertainment/Handler/Roll.cs +++ b/modules/Entertainment/Handler/Roll.cs @@ -39,6 +39,14 @@ public class Roll : CommonHandler cd ); + // Drop user message. + _requests.Add(new DeleteRequest + { + ChatId = Chat.Id, + DeleteAt = DateTime.UtcNow.AddSeconds(20), + MessageId = Message.MessageId + }); + if (cd > DateTime.Now) { await Reply($"Следующее использование через {cd - DateTime.Now:hh\\:mm\\:ss}.");