Add autocleanup for Guess and Roll handlers

This commit is contained in:
2023-01-29 10:05:44 +03:00
parent 682920cbfb
commit ee1f1cf30c
3 changed files with 44 additions and 3 deletions
+20 -1
View File
@@ -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<DeleteRequest> _requests;
public Guess(IGuessCache guessCache, ThreadSafeRandom rng, IReputation reputationService, CoreContext db) : base(db)
public Guess(IGuessCache guessCache, ThreadSafeRandom rng, IReputation reputationService, CoreContext db, ConcurrentBag<DeleteRequest> 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
});
}
}