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 20:58:26 +02:00
|
|
|
private static TimeSpan Cooldown => TimeSpan.FromMinutes(new Random().Next(10, 1337));
|
|
|
|
|
|
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-03 20:58:26 +02:00
|
|
|
var text = Message!.Text!.ToLower();
|
|
|
|
|
if (!AnimationMatch.IsMatch(text.ToLower()) || text.StartsWith("/set") || !CanHandle()) return false;
|
|
|
|
|
|
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();
|
|
|
|
|
|
2022-01-03 20:58:26 +02:00
|
|
|
_logger.LogDebug("Animation: {Text}, ToLower: {ToLowerText}",
|
2022-01-03 19:22:03 +02:00
|
|
|
Message!.Text, Message!.Text!.ToLower());
|
|
|
|
|
|
2022-01-03 20:58:26 +02:00
|
|
|
if (animationFileId.IsNullOrEmpty()) return false;
|
2022-01-03 19:22:03 +02:00
|
|
|
|
|
|
|
|
var nextUseDt = lastUseOption.GetValue<DateTime>();
|
|
|
|
|
if (nextUseDt > DateTime.Now)
|
2022-01-03 15:55:42 +02:00
|
|
|
{
|
2022-01-03 20:58:26 +02:00
|
|
|
_logger.LogInformation("{Name} is on cooldown for chat \"{ChatTitle}\". Will be available at {Time}",
|
2022-01-03 19:22:03 +02:00
|
|
|
GetAnimationName(), Chat.Title, nextUseDt);
|
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
|
|
|
|
2022-01-03 20:58:26 +02:00
|
|
|
lastUseOption.SetValue(DateTime.Now + Cooldown);
|
2022-01-03 15:28:00 +02:00
|
|
|
_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()
|
|
|
|
|
{
|
2022-01-03 20:58:26 +02:00
|
|
|
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}NextUse");
|
2022-01-03 15:28:00 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|