From f9bfc88c25ba6708f96fbbe007352907c042393c Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Mon, 3 Jan 2022 15:55:42 +0200 Subject: [PATCH] Add cooldown logging. --- .../Handler/Fun/AbstractAnimationReply.cs | 20 ++++++++++++++----- modules/Entertainment/Handler/Fun/Bayan.cs | 3 ++- modules/Entertainment/Handler/Fun/Python.cs | 3 ++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs b/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs index 4ffbf7e..a733a2f 100644 --- a/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs +++ b/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs @@ -7,6 +7,7 @@ using BotFramework; using Castle.Core.Internal; using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Extensions; +using Microsoft.Extensions.Logging; using Telegram.Bot; using Telegram.Bot.Types; @@ -16,6 +17,8 @@ namespace West.Entertainment.Handler.Fun { private readonly CoreContext _db; + private readonly ILogger _logger; + private const int Cooldown = 30; protected Message? Message => RawUpdate.Message; @@ -27,11 +30,12 @@ namespace West.Entertainment.Handler.Fun get; } - protected AbstractAnimationReply(CoreContext db) + protected AbstractAnimationReply(CoreContext db, ILogger logger) { _db = db; + _logger = logger; } - + protected abstract string GetAnimationName(); protected virtual bool CanHandle() => true; @@ -54,10 +58,16 @@ namespace West.Entertainment.Handler.Fun if (animationFileId.IsNullOrEmpty() || !AnimationMatch.IsMatch(Message!.Text!.ToLower()) - || !CanHandle() - || lastUseOption.GetValue() > DateTime.Now) return false; - + || !CanHandle()) return false; + if (lastUseOption.GetValue() > DateTime.Now) + { + _logger.LogInformation("{Name} is on cooldown for Chat {ChatTitle}. Will be available at {Time}", + GetAnimationName(), Chat.Title, lastUseOption.GetValue()); + + return false; + } + await Bot.SendAnimationAsync(Chat.Id, animationFileId, replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId); diff --git a/modules/Entertainment/Handler/Fun/Bayan.cs b/modules/Entertainment/Handler/Fun/Bayan.cs index 50ed3dc..50a036c 100644 --- a/modules/Entertainment/Handler/Fun/Bayan.cs +++ b/modules/Entertainment/Handler/Fun/Bayan.cs @@ -5,13 +5,14 @@ using System.Threading.Tasks; using BotFramework.Attributes; using BotFramework.Enums; using Kruzya.TelegramBot.Core.Data; +using Microsoft.Extensions.Logging; using Telegram.Bot; namespace West.Entertainment.Handler.Fun { public class Bayan : AbstractAnimationReply { - public Bayan(CoreContext db) : base(db) { } + public Bayan(CoreContext db, ILogger logger) : base(db, logger) { } protected override Regex AnimationMatch => new("^баян$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); diff --git a/modules/Entertainment/Handler/Fun/Python.cs b/modules/Entertainment/Handler/Fun/Python.cs index b095066..0e75d1d 100644 --- a/modules/Entertainment/Handler/Fun/Python.cs +++ b/modules/Entertainment/Handler/Fun/Python.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using BotFramework.Attributes; using BotFramework.Enums; using Kruzya.TelegramBot.Core.Data; +using Microsoft.Extensions.Logging; namespace West.Entertainment.Handler.Fun { @@ -29,6 +30,6 @@ namespace West.Entertainment.Handler.Fun => await base.HandleAnimation(); - public Python(CoreContext db) : base(db) { } + public Python(CoreContext db, ILogger logger) : base(db, logger) { } } } \ No newline at end of file