mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
101 lines
3.3 KiB
C#
101 lines
3.3 KiB
C#
#nullable enable
|
|
|
|
using System;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
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;
|
|
|
|
namespace West.Entertainment.Handler.Fun
|
|
{
|
|
public abstract class AbstractAnimationReply : BotEventHandler
|
|
{
|
|
private readonly CoreContext _db;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private const int Cooldown = 30;
|
|
|
|
protected Message? Message => RawUpdate.Message;
|
|
|
|
private User? RepliedUser => Message?.ReplyToMessage?.From;
|
|
|
|
protected abstract Regex AnimationMatch
|
|
{
|
|
get;
|
|
}
|
|
|
|
protected AbstractAnimationReply(CoreContext db, ILogger logger)
|
|
{
|
|
_db = db;
|
|
_logger = logger;
|
|
}
|
|
|
|
protected abstract string GetAnimationName();
|
|
|
|
protected virtual bool CanHandle() => true;
|
|
|
|
protected async Task HandleSetAnimation()
|
|
{
|
|
var animation = Message?.ReplyToMessage?.Animation;
|
|
if (await CanPunish() || animation == null) return;
|
|
|
|
var animationFileOption = await GetAnimationFileOption();
|
|
animationFileOption.SetValue(animation.FileId);
|
|
_db.MarkAsModified(animationFileOption);
|
|
}
|
|
|
|
// ReSharper disable once MemberCanBeProtected.Global
|
|
public virtual async Task<bool> HandleAnimation()
|
|
{
|
|
var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
|
|
var lastUseOption = await GetLastUseOption();
|
|
|
|
_logger.LogInformation("Animation: {Text}, ToLower: {ToLowerText}",
|
|
Message!.Text, Message!.Text!.ToLower());
|
|
|
|
if (animationFileId.IsNullOrEmpty()
|
|
|| !AnimationMatch.IsMatch(Message!.Text!.ToLower())
|
|
|| !CanHandle()) return false;
|
|
|
|
var nextUseDt = lastUseOption.GetValue<DateTime>();
|
|
if (nextUseDt > DateTime.Now)
|
|
{
|
|
_logger.LogInformation("{Name} is on cooldown for Chat {ChatTitle}. Will be available at {Time}",
|
|
GetAnimationName(), Chat.Title, nextUseDt);
|
|
|
|
return false;
|
|
}
|
|
|
|
await Bot.SendAnimationAsync(Chat.Id, animationFileId,
|
|
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
|
|
|
|
lastUseOption.SetValue(DateTime.Now + TimeSpan.FromSeconds(Cooldown));
|
|
_db.MarkAsModified(lastUseOption);
|
|
|
|
return true;
|
|
}
|
|
|
|
private async Task<BotUserValue> GetAnimationFileOption()
|
|
{
|
|
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File");
|
|
}
|
|
|
|
private async Task<BotUserValue> GetLastUseOption()
|
|
{
|
|
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}LastUse");
|
|
}
|
|
|
|
private async Task<bool> CanPunish()
|
|
{
|
|
return RepliedUser != null && await Bot.CanPunishMember(Chat, From, RepliedUser);
|
|
}
|
|
|
|
protected virtual int? GetMessageIdToReply() => Message?.MessageId;
|
|
}
|
|
} |