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
+23 -1
View File
@@ -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<DeleteRequest> _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<Message> 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<Roll> 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<Roll> logger, ThreadSafeRandom rng, ConcurrentBag<DeleteRequest> requests) : base(db)
{
_reputation = reputation;
_userService = userService;
_logger = logger;
_rng = rng;
_requests = requests;
}
}