diff --git a/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs b/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs index 8faf151..8de1599 100644 --- a/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs +++ b/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs @@ -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 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.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 GetLastUseOption() + { + return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}LastUse"); + } + private async Task CanPunish() { return RepliedUser != null && await Bot.CanPunishMember(Chat, From, RepliedUser);