Files
telegram-bot/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs
T
2022-01-02 21:56:14 +02:00

55 lines
1.8 KiB
C#

#nullable enable
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using Castle.Core.Internal;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
using West.TelegramBot.ChatManagement.Handler;
namespace West.Entertainment.Handler.Fun
{
public abstract class AbstractAnimationReply : ManagementHandler
{
protected abstract Regex AnimationMatch
{
get;
}
protected abstract string GetAnimationName();
protected virtual bool CanHandle() => true;
protected async Task HandleSetAnimation()
{
var animation = Message?.ReplyToMessage?.Animation;
if (await CanPunish() || animation == null) return;
var animationFileOption = await GetAnimationFileOption();
animationFileOption.SetValue(animation.FileId);
Db.MarkAsModified(animationFileOption);
}
[Message(InChat.Public, MessageFlag.HasText)]
protected virtual async Task<bool> HandleAnimation(int? replyToMessageId = null)
{
var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
if (animationFileId.IsNullOrEmpty() || !AnimationMatch.IsMatch(Message!.Text!) || !CanHandle()) return false;
await Bot.SendAnimationAsync(Chat.Id, animationFileId,
replyToMessageId: replyToMessageId ?? Message?.MessageId);
return true;
}
private async Task<BotUserValue> GetAnimationFileOption()
{
return await Db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File");
}
protected AbstractAnimationReply(CoreContext db) : base(db) {}
}
}