Files
telegram-bot/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs
T

109 lines
3.8 KiB
C#
Raw Normal View History

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;
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
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;
private readonly ThreadSafeRandom _rng;
2022-01-03 15:55:42 +02:00
protected virtual TimeSpan Cooldown => TimeSpan.FromMinutes(_rng.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;
}
protected AbstractAnimationReply(CoreContext db, ILogger logger, ThreadSafeRandom rng)
2022-01-03 14:58:53 +02:00
{
_db = db;
2022-01-03 15:55:42 +02:00
_logger = logger;
_rng = rng;
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;
var isAdmin = await Bot.IsUserAdminAsync(Chat, From);
_logger.LogDebug("IsAdmin: {IsAdmin}. FileId: {FileId}", isAdmin, animation?.FileId);
if (!isAdmin || animation == null) return;
2021-12-29 00:57:37 +02:00
var animationFileOption = await GetAnimationFileOption();
animationFileOption.SetValue(animation.FileId);
2022-01-25 23:34:37 +03:00
_db.AddOrUpdate(animationFileOption);
await Bot.SendTextMessageAsync(Chat.Id, "Animation has been set.", replyToMessageId: Message!.ReplyToMessage!.MessageId);
2021-12-29 00:57:37 +02:00
}
protected virtual async Task<bool> HandleAnimation()
2021-12-29 00:57:37 +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-04 17:45:06 +02:00
var nextUseOption = await GetNextUseOption();
2022-01-03 15:28:00 +02:00
_logger.LogDebug("Animation: {Text}, ToLower: {ToLowerText}",
2022-01-03 19:22:03 +02:00
Message!.Text, Message!.Text!.ToLower());
_logger.LogDebug("AnimationFileId: {FileId}", animationFileId);
2023-01-02 23:42:07 +02:00
if (string.IsNullOrEmpty(animationFileId))
{
_logger.LogDebug("AnimationFileId is empty. Ignoring.");
return false;
}
2022-01-03 19:22:03 +02:00
2022-01-04 17:45:06 +02:00
var nextUseDt = nextUseOption.GetValue<DateTime>();
2022-01-03 19:22:03 +02:00
if (nextUseDt > DateTime.Now)
2022-01-03 15:55:42 +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;
}
2023-01-02 19:01:03 +02:00
await Bot.SendAnimationAsync(Chat.Id, new InputFileId(animationFileId),
2022-01-03 14:58:53 +02:00
replyToMessageId: GetMessageIdToReply() ?? Message?.MessageId);
2022-01-03 15:28:00 +02:00
2022-01-04 17:45:06 +02:00
nextUseOption.SetValue(DateTime.Now + Cooldown);
2022-01-25 23:34:37 +03:00
_db.AddOrUpdate(nextUseOption);
2022-01-03 15:28:00 +02:00
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-04 17:45:06 +02:00
private async Task<BotUserValue> GetNextUseOption()
2022-01-03 15:28:00 +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
protected virtual int? GetMessageIdToReply() => Message?.MessageId;
2021-12-29 00:57:37 +02:00
}
}