From 68deb1ec8bab692b5afa5e587f76ba6418277e3a Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Mon, 3 Jan 2022 15:28:00 +0200 Subject: [PATCH] Add cooldown for animation replies. --- .../Handler/Fun/AbstractAnimationReply.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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);