mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Add cooldown for animation replies.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using BotFramework;
|
||||
@@ -14,6 +15,8 @@ namespace West.Entertainment.Handler.Fun
|
||||
public abstract class AbstractAnimationReply : BotEventHandler
|
||||
{
|
||||
private readonly CoreContext _db;
|
||||
|
||||
private const int Cooldown = 30;
|
||||
|
||||
protected Message? Message => RawUpdate.Message;
|
||||
|
||||
@@ -47,11 +50,20 @@ namespace West.Entertainment.Handler.Fun
|
||||
public virtual async Task<bool> HandleAnimation()
|
||||
{
|
||||
var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
|
||||
if (animationFileId.IsNullOrEmpty() || !AnimationMatch.IsMatch(Message!.Text!) || !CanHandle()) return false;
|
||||
var lastUseOption = await GetLastUseOption();
|
||||
|
||||
if (animationFileId.IsNullOrEmpty()
|
||||
|| !AnimationMatch.IsMatch(Message!.Text!)
|
||||
|| !CanHandle()
|
||||
|| lastUseOption.GetValue<DateTime>() > DateTime.Now) return false;
|
||||
|
||||
|
||||
await Bot.SendAnimationAsync(Chat.Id, animationFileId,
|
||||
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
|
||||
|
||||
lastUseOption.SetValue(DateTime.Now + TimeSpan.FromSeconds(Cooldown));
|
||||
_db.MarkAsModified(lastUseOption);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -60,6 +72,11 @@ namespace West.Entertainment.Handler.Fun
|
||||
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File");
|
||||
}
|
||||
|
||||
private async Task<BotUserValue> GetLastUseOption()
|
||||
{
|
||||
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}LastUse");
|
||||
}
|
||||
|
||||
private async Task<bool> CanPunish()
|
||||
{
|
||||
return RepliedUser != null && await Bot.CanPunishMember(Chat, From, RepliedUser);
|
||||
|
||||
Reference in New Issue
Block a user