From ee1f1cf30cafe396d4003477d8c471efbef60acd Mon Sep 17 00:00:00 2001 From: Sergey Gut Date: Sun, 29 Jan 2023 10:05:44 +0300 Subject: [PATCH] Add autocleanup for Guess and Roll handlers --- Core/CommonHandler.cs | 2 +- modules/Entertainment/Handler/Guess.cs | 21 ++++++++++++++++++++- modules/Entertainment/Handler/Roll.cs | 24 +++++++++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/Core/CommonHandler.cs b/Core/CommonHandler.cs index a575aa0..142c0bc 100644 --- a/Core/CommonHandler.cs +++ b/Core/CommonHandler.cs @@ -24,7 +24,7 @@ namespace Kruzya.TelegramBot.Core Db = db; } - protected async Task Reply(string text) + protected virtual async Task Reply(string text) { return await Bot.SendTextMessageAsync(Chat.Id, text, replyToMessageId: Message?.MessageId, parseMode: ParseMode.Html); diff --git a/modules/Entertainment/Handler/Guess.cs b/modules/Entertainment/Handler/Guess.cs index 2820dbc..fbb30b8 100644 --- a/modules/Entertainment/Handler/Guess.cs +++ b/modules/Entertainment/Handler/Guess.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using BotFramework.Attributes; using BotFramework.Enums; using Kruzya.TelegramBot.Core; +using Kruzya.TelegramBot.Core.AutoDelete; using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Service; @@ -21,13 +22,15 @@ public class Guess : CommonHandler private readonly ThreadSafeRandom _rng; private readonly IReputation _reputationService; private readonly CoreContext _db; + private readonly ConcurrentBag _requests; - public Guess(IGuessCache guessCache, ThreadSafeRandom rng, IReputation reputationService, CoreContext db) : base(db) + public Guess(IGuessCache guessCache, ThreadSafeRandom rng, IReputation reputationService, CoreContext db, ConcurrentBag requests) : base(db) { _guessCache = guessCache; _rng = rng; _reputationService = reputationService; _db = db; + _requests = requests; } [Command(InChat.Public, "guess", CommandParseMode.Both)] @@ -76,6 +79,14 @@ 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)] @@ -137,5 +148,13 @@ public class Guess : CommonHandler _db.AddOrUpdate(cooldownOption); await Bot.EditMessageTextAsync(Chat, (int) messageId, message + postFix, parseMode: ParseMode.Html); + + // Drop bot message. + _requests.Add(new DeleteRequest + { + ChatId = Chat.Id, + DeleteAt = DateTime.UtcNow.AddSeconds(20), + MessageId = (int) messageId + }); } } \ No newline at end of file diff --git a/modules/Entertainment/Handler/Roll.cs b/modules/Entertainment/Handler/Roll.cs index eadf62a..5d332dc 100644 --- a/modules/Entertainment/Handler/Roll.cs +++ b/modules/Entertainment/Handler/Roll.cs @@ -10,6 +10,11 @@ using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Service; using Microsoft.Extensions.Logging; using Telegram.Bot; +using Kruzya.TelegramBot.Core.AutoDelete; +using System.Collections.Concurrent; +using Telegram.Bot.Types.Enums; +using Telegram.Bot.Types; +using Microsoft.AspNetCore.Components.Forms; namespace West.Entertainment.Handler; @@ -19,6 +24,7 @@ public class Roll : CommonHandler private readonly ThreadSafeRandom _rng; private readonly IReputation _reputation; private readonly UserService _userService; + private readonly ConcurrentBag _requests; [Command(InChat.Public, "roll", CommandParseMode.Both)] public async Task HandleRoll() @@ -115,12 +121,28 @@ public class Roll : CommonHandler await Bot.SendTextMessageAsync(Chat, $"Positive: {positive}.\nNegative: {negative}"); } + 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); - public Roll(CoreContext db, IReputation reputation, UserService userService, ILogger logger, ThreadSafeRandom rng) : base(db) + _requests.Add(new DeleteRequest + { + ChatId = Chat.Id, + DeleteAt = DateTime.UtcNow.AddSeconds(20), + MessageId = message.MessageId + }); + + return message; + } + + public Roll(CoreContext db, IReputation reputation, UserService userService, ILogger logger, ThreadSafeRandom rng, ConcurrentBag requests) : base(db) { _reputation = reputation; _userService = userService; _logger = logger; _rng = rng; + _requests = requests; } } \ No newline at end of file