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

87 lines
2.7 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.Data;
using Kruzya.TelegramBot.Core.Extensions;
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
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 14:58:53 +02:00
protected AbstractAnimationReply(CoreContext db)
{
_db = db;
}
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()
|| !AnimationMatch.IsMatch(Message!.Text!.ToLower())
2022-01-03 15:28:00 +02:00
|| !CanHandle()
|| lastUseOption.GetValue<DateTime>() > DateTime.Now) return false;
2022-01-02 21:56:14 +02:00
2021-12-29 00:57:37 +02:00
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
}
}