2021-12-29 00:57:37 +02:00
|
|
|
#nullable enable
|
|
|
|
|
|
2022-01-03 15:28:00 +02:00
|
|
|
using System;
|
2022-01-02 21:56:14 +02:00
|
|
|
using System.Text.RegularExpressions;
|
2021-12-29 00:57:37 +02:00
|
|
|
using System.Threading.Tasks;
|
2022-01-03 14:58:53 +02:00
|
|
|
using BotFramework;
|
2021-12-29 00:57:37 +02:00
|
|
|
using Castle.Core.Internal;
|
|
|
|
|
using Kruzya.TelegramBot.Core.Data;
|
|
|
|
|
using Kruzya.TelegramBot.Core.Extensions;
|
2022-01-03 15:55:42 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-12-29 00:57:37 +02:00
|
|
|
using Telegram.Bot;
|
2022-01-03 14:58:53 +02:00
|
|
|
using Telegram.Bot.Types;
|
2021-12-29 00:57:37 +02:00
|
|
|
|
2022-01-02 21:56:14 +02:00
|
|
|
namespace West.Entertainment.Handler.Fun
|
2021-12-29 00:57:37 +02:00
|
|
|
{
|
2022-01-03 14:58:53 +02:00
|
|
|
public abstract class AbstractAnimationReply : BotEventHandler
|
2021-12-29 00:57:37 +02:00
|
|
|
{
|
2022-01-03 14:58:53 +02:00
|
|
|
private readonly CoreContext _db;
|
2022-01-03 15:28:00 +02:00
|
|
|
|
2022-01-03 15:55:42 +02:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
2022-01-03 15:28:00 +02:00
|
|
|
private const int Cooldown = 30;
|
2022-01-03 14:58:53 +02:00
|
|
|
|
|
|
|
|
protected Message? Message => RawUpdate.Message;
|
|
|
|
|
|
|
|
|
|
private User? RepliedUser => Message?.ReplyToMessage?.From;
|
|
|
|
|
|
2022-01-02 21:56:14 +02:00
|
|
|
protected abstract Regex AnimationMatch
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 15:55:42 +02:00
|
|
|
protected AbstractAnimationReply(CoreContext db, ILogger logger)
|
2022-01-03 14:58:53 +02:00
|
|
|
{
|
|
|
|
|
_db = db;
|
2022-01-03 15:55:42 +02:00
|
|
|
_logger = logger;
|
2022-01-03 14:58:53 +02:00
|
|
|
}
|
2022-01-03 15:55:42 +02:00
|
|
|
|
2021-12-29 00:57:37 +02:00
|
|
|
protected abstract string GetAnimationName();
|
2022-01-02 21:56:14 +02:00
|
|
|
|
|
|
|
|
protected virtual bool CanHandle() => true;
|
|
|
|
|
|
2021-12-29 00:57:37 +02:00
|
|
|
protected async Task HandleSetAnimation()
|
|
|
|
|
{
|
|
|
|
|
var animation = Message?.ReplyToMessage?.Animation;
|
|
|
|
|
if (await CanPunish() || animation == null) return;
|
|
|
|
|
|
|
|
|
|
var animationFileOption = await GetAnimationFileOption();
|
|
|
|
|
animationFileOption.SetValue(animation.FileId);
|
2022-01-03 14:58:53 +02:00
|
|
|
_db.MarkAsModified(animationFileOption);
|
2021-12-29 00:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-03 14:58:53 +02:00
|
|
|
// ReSharper disable once MemberCanBeProtected.Global
|
|
|
|
|
public virtual async Task<bool> HandleAnimation()
|
2021-12-29 00:57:37 +02:00
|
|
|
{
|
2022-01-02 21:56:14 +02:00
|
|
|
var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
|
2022-01-03 15:28:00 +02:00
|
|
|
var lastUseOption = await GetLastUseOption();
|
|
|
|
|
|
|
|
|
|
if (animationFileId.IsNullOrEmpty()
|
2022-01-03 15:32:40 +02:00
|
|
|
|| !AnimationMatch.IsMatch(Message!.Text!.ToLower())
|
2022-01-03 15:55:42 +02:00
|
|
|
|| !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>());
|
2021-12-29 00:57:37 +02:00
|
|
|
|
2022-01-03 15:55:42 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-02 21:56:14 +02:00
|
|
|
await Bot.SendAnimationAsync(Chat.Id, animationFileId,
|
2022-01-03 14:58:53 +02:00
|
|
|
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
|
2022-01-03 15:28:00 +02:00
|
|
|
|
|
|
|
|
lastUseOption.SetValue(DateTime.Now + TimeSpan.FromSeconds(Cooldown));
|
|
|
|
|
_db.MarkAsModified(lastUseOption);
|
|
|
|
|
|
2021-12-29 00:57:37 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<BotUserValue> GetAnimationFileOption()
|
|
|
|
|
{
|
2022-01-03 14:58:53 +02:00
|
|
|
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File");
|
2021-12-29 00:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
2022-01-03 15:28:00 +02:00
|
|
|
private async Task<BotUserValue> GetLastUseOption()
|
|
|
|
|
{
|
|
|
|
|
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}LastUse");
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 14:58:53 +02:00
|
|
|
private async Task<bool> CanPunish()
|
|
|
|
|
{
|
|
|
|
|
return RepliedUser != null && await Bot.CanPunishMember(Chat, From, RepliedUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual int? GetMessageIdToReply() => Message?.MessageId;
|
2021-12-29 00:57:37 +02:00
|
|
|
}
|
|
|
|
|
}
|