Add cooldown logging.

This commit is contained in:
West14
2022-01-03 15:55:42 +02:00
parent 36af127845
commit f9bfc88c25
3 changed files with 19 additions and 7 deletions
@@ -7,6 +7,7 @@ using BotFramework;
using Castle.Core.Internal; using Castle.Core.Internal;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.Extensions.Logging;
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types; using Telegram.Bot.Types;
@@ -16,6 +17,8 @@ namespace West.Entertainment.Handler.Fun
{ {
private readonly CoreContext _db; private readonly CoreContext _db;
private readonly ILogger _logger;
private const int Cooldown = 30; private const int Cooldown = 30;
protected Message? Message => RawUpdate.Message; protected Message? Message => RawUpdate.Message;
@@ -27,11 +30,12 @@ namespace West.Entertainment.Handler.Fun
get; get;
} }
protected AbstractAnimationReply(CoreContext db) protected AbstractAnimationReply(CoreContext db, ILogger logger)
{ {
_db = db; _db = db;
_logger = logger;
} }
protected abstract string GetAnimationName(); protected abstract string GetAnimationName();
protected virtual bool CanHandle() => true; protected virtual bool CanHandle() => true;
@@ -54,10 +58,16 @@ namespace West.Entertainment.Handler.Fun
if (animationFileId.IsNullOrEmpty() if (animationFileId.IsNullOrEmpty()
|| !AnimationMatch.IsMatch(Message!.Text!.ToLower()) || !AnimationMatch.IsMatch(Message!.Text!.ToLower())
|| !CanHandle() || !CanHandle()) return false;
|| lastUseOption.GetValue<DateTime>() > DateTime.Now) return false;
if (lastUseOption.GetValue<DateTime>() > DateTime.Now)
{
_logger.LogInformation("{Name} is on cooldown for Chat {ChatTitle}. Will be available at {Time}",
GetAnimationName(), Chat.Title, lastUseOption.GetValue<DateTime>());
return false;
}
await Bot.SendAnimationAsync(Chat.Id, animationFileId, await Bot.SendAnimationAsync(Chat.Id, animationFileId,
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId); replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
+2 -1
View File
@@ -5,13 +5,14 @@ using System.Threading.Tasks;
using BotFramework.Attributes; using BotFramework.Attributes;
using BotFramework.Enums; using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Microsoft.Extensions.Logging;
using Telegram.Bot; using Telegram.Bot;
namespace West.Entertainment.Handler.Fun namespace West.Entertainment.Handler.Fun
{ {
public class Bayan : AbstractAnimationReply public class Bayan : AbstractAnimationReply
{ {
public Bayan(CoreContext db) : base(db) { } public Bayan(CoreContext db, ILogger<Bayan> logger) : base(db, logger) { }
protected override Regex AnimationMatch protected override Regex AnimationMatch
=> new("^баян$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); => new("^баян$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
+2 -1
View File
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
using BotFramework.Attributes; using BotFramework.Attributes;
using BotFramework.Enums; using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Microsoft.Extensions.Logging;
namespace West.Entertainment.Handler.Fun namespace West.Entertainment.Handler.Fun
{ {
@@ -29,6 +30,6 @@ namespace West.Entertainment.Handler.Fun
=> await base.HandleAnimation(); => await base.HandleAnimation();
public Python(CoreContext db) : base(db) { } public Python(CoreContext db, ILogger<Python> logger) : base(db, logger) { }
} }
} }