mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Add autocleanup for Guess and Roll handlers
This commit is contained in:
@@ -24,7 +24,7 @@ namespace Kruzya.TelegramBot.Core
|
|||||||
Db = db;
|
Db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async Task<Message> Reply(string text)
|
protected virtual async Task<Message> Reply(string text)
|
||||||
{
|
{
|
||||||
return await Bot.SendTextMessageAsync(Chat.Id, text,
|
return await Bot.SendTextMessageAsync(Chat.Id, text,
|
||||||
replyToMessageId: Message?.MessageId, parseMode: ParseMode.Html);
|
replyToMessageId: Message?.MessageId, parseMode: ParseMode.Html);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
|||||||
using BotFramework.Attributes;
|
using BotFramework.Attributes;
|
||||||
using BotFramework.Enums;
|
using BotFramework.Enums;
|
||||||
using Kruzya.TelegramBot.Core;
|
using Kruzya.TelegramBot.Core;
|
||||||
|
using Kruzya.TelegramBot.Core.AutoDelete;
|
||||||
using Kruzya.TelegramBot.Core.Data;
|
using Kruzya.TelegramBot.Core.Data;
|
||||||
using Kruzya.TelegramBot.Core.Extensions;
|
using Kruzya.TelegramBot.Core.Extensions;
|
||||||
using Kruzya.TelegramBot.Core.Service;
|
using Kruzya.TelegramBot.Core.Service;
|
||||||
@@ -21,13 +22,15 @@ public class Guess : CommonHandler
|
|||||||
private readonly ThreadSafeRandom _rng;
|
private readonly ThreadSafeRandom _rng;
|
||||||
private readonly IReputation _reputationService;
|
private readonly IReputation _reputationService;
|
||||||
private readonly CoreContext _db;
|
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;
|
_guessCache = guessCache;
|
||||||
_rng = rng;
|
_rng = rng;
|
||||||
_reputationService = reputationService;
|
_reputationService = reputationService;
|
||||||
_db = db;
|
_db = db;
|
||||||
|
_requests = requests;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Command(InChat.Public, "guess", CommandParseMode.Both)]
|
[Command(InChat.Public, "guess", CommandParseMode.Both)]
|
||||||
@@ -76,6 +79,14 @@ public class Guess : CommonHandler
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_guessCache.SetValue(Chat.Id, guessDict, -1);
|
_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)]
|
[Update(InChat.Public, UpdateFlag.CallbackQuery)]
|
||||||
@@ -137,5 +148,13 @@ public class Guess : CommonHandler
|
|||||||
_db.AddOrUpdate(cooldownOption);
|
_db.AddOrUpdate(cooldownOption);
|
||||||
|
|
||||||
await Bot.EditMessageTextAsync(Chat, (int) messageId, message + postFix, parseMode: ParseMode.Html);
|
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
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,11 @@ using Kruzya.TelegramBot.Core.Extensions;
|
|||||||
using Kruzya.TelegramBot.Core.Service;
|
using Kruzya.TelegramBot.Core.Service;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Telegram.Bot;
|
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;
|
namespace West.Entertainment.Handler;
|
||||||
|
|
||||||
@@ -19,6 +24,7 @@ public class Roll : CommonHandler
|
|||||||
private readonly ThreadSafeRandom _rng;
|
private readonly ThreadSafeRandom _rng;
|
||||||
private readonly IReputation _reputation;
|
private readonly IReputation _reputation;
|
||||||
private readonly UserService _userService;
|
private readonly UserService _userService;
|
||||||
|
private readonly ConcurrentBag<DeleteRequest> _requests;
|
||||||
|
|
||||||
[Command(InChat.Public, "roll", CommandParseMode.Both)]
|
[Command(InChat.Public, "roll", CommandParseMode.Both)]
|
||||||
public async Task HandleRoll()
|
public async Task HandleRoll()
|
||||||
@@ -115,12 +121,28 @@ public class Roll : CommonHandler
|
|||||||
|
|
||||||
await Bot.SendTextMessageAsync(Chat, $"Positive: {positive}.\nNegative: {negative}");
|
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;
|
_reputation = reputation;
|
||||||
_userService = userService;
|
_userService = userService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_rng = rng;
|
_rng = rng;
|
||||||
|
_requests = requests;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user