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;
|
2023-01-17 23:13:06 +02:00
|
|
|
using Kruzya.TelegramBot.Core;
|
2021-12-29 00:57:37 +02:00
|
|
|
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
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
namespace West.Entertainment.Handler.Fun;
|
|
|
|
|
|
|
|
|
|
public abstract class AbstractAnimationReply : BotEventHandler
|
2021-12-29 00:57:37 +02:00
|
|
|
{
|
2023-07-29 15:14:52 +03:00
|
|
|
private readonly CoreContext _db;
|
2022-01-03 15:28:00 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly ThreadSafeRandom _rng;
|
2022-01-03 15:55:42 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
protected virtual TimeSpan Cooldown => TimeSpan.FromMinutes(_rng.Next(10, 1337));
|
2022-01-03 20:58:26 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
protected Message? Message => RawUpdate.Message;
|
2022-01-03 14:58:53 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
private User? RepliedUser => Message?.ReplyToMessage?.From;
|
2022-01-03 14:58:53 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
protected abstract Regex AnimationMatch
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
}
|
2022-01-02 21:56:14 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
protected AbstractAnimationReply(CoreContext db, ILogger logger, ThreadSafeRandom rng)
|
|
|
|
|
{
|
|
|
|
|
_db = db;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_rng = rng;
|
|
|
|
|
}
|
2022-01-03 15:55:42 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
protected abstract string GetAnimationName();
|
2022-01-02 21:56:14 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
protected virtual bool CanHandle() => true;
|
2022-01-02 21:56:14 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
protected async Task HandleSetAnimation()
|
|
|
|
|
{
|
|
|
|
|
var animation = Message?.ReplyToMessage?.Animation;
|
|
|
|
|
var isAdmin = await Bot.IsUserAdminAsync(Chat, From);
|
2022-02-08 20:28:15 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
_logger.LogDebug("IsAdmin: {IsAdmin}. FileId: {FileId}", isAdmin, animation?.FileId);
|
|
|
|
|
if (!isAdmin || animation == null) return;
|
2021-12-29 00:57:37 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
var animationFileOption = await GetAnimationFileOption();
|
|
|
|
|
animationFileOption.SetValue(animation.FileId);
|
|
|
|
|
_db.AddOrUpdate(animationFileOption);
|
2022-07-29 12:20:24 +03:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
await Bot.SendTextMessageAsync(Chat.Id, "Animation has been set.", replyToMessageId: Message!.ReplyToMessage!.MessageId);
|
|
|
|
|
}
|
2021-12-29 00:57:37 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
protected virtual async Task<bool> HandleAnimation()
|
|
|
|
|
{
|
|
|
|
|
var text = Message!.Text!.ToLower();
|
|
|
|
|
if (!AnimationMatch.IsMatch(text.ToLower()) || text.StartsWith("/set") || !CanHandle()) return false;
|
2022-01-03 20:58:26 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
|
|
|
|
|
var nextUseOption = await GetNextUseOption();
|
2022-01-03 15:28:00 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
_logger.LogDebug("Animation: {Text}, ToLower: {ToLowerText}",
|
|
|
|
|
Message!.Text, Message!.Text!.ToLower());
|
|
|
|
|
_logger.LogDebug("AnimationFileId: {FileId}", animationFileId);
|
2022-02-08 20:28:15 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
if (string.IsNullOrEmpty(animationFileId))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogDebug("AnimationFileId is empty. Ignoring.");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-01-03 19:22:03 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
var nextUseDt = nextUseOption.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;
|
|
|
|
|
}
|
2022-01-03 15:55:42 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
await Bot.SendAnimationAsync(Chat.Id, new InputFileId(animationFileId),
|
|
|
|
|
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
|
2022-01-03 15:28:00 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
nextUseOption.SetValue(DateTime.Now + Cooldown);
|
|
|
|
|
_db.AddOrUpdate(nextUseOption);
|
2022-01-03 15:28:00 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
return true;
|
|
|
|
|
}
|
2021-12-29 00:57:37 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
private async Task<BotUserValue> GetAnimationFileOption()
|
|
|
|
|
{
|
|
|
|
|
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File");
|
|
|
|
|
}
|
2021-12-29 00:57:37 +02:00
|
|
|
|
2023-07-29 15:14:52 +03:00
|
|
|
private async Task<BotUserValue> GetNextUseOption()
|
|
|
|
|
{
|
|
|
|
|
return await _db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}NextUse");
|
2021-12-29 00:57:37 +02:00
|
|
|
}
|
2023-07-29 15:14:52 +03:00
|
|
|
|
|
|
|
|
protected virtual int? GetMessageIdToReply() => Message?.MessageId;
|
2021-12-29 00:57:37 +02:00
|
|
|
}
|