mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Add cooldown logging.
This commit is contained in:
@@ -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>() > DateTime.Now) return false;
|
||||
|
||||
|| !CanHandle()) 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,
|
||||
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
|
||||
|
||||
|
||||
@@ -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<Bayan> logger) : base(db, logger) { }
|
||||
|
||||
protected override Regex AnimationMatch
|
||||
=> new("^баян$", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
|
||||
|
||||
@@ -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<Python> logger) : base(db, logger) { }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user