Animation trigger refactoring. BAYAN.

This commit is contained in:
West14
2021-12-29 00:57:37 +02:00
parent df11e1d327
commit ae5d8ade8c
3 changed files with 101 additions and 15 deletions
@@ -0,0 +1,42 @@
#nullable enable
using System.Threading.Tasks;
using Castle.Core.Internal;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
namespace West.TelegramBot.ChatManagement.Handler.Fun
{
public abstract class AbstractAnimationReply : ManagementHandler
{
protected abstract string GetAnimationName();
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);
}
protected async Task<bool> HandleAnimation(int? replyToMessageId = null)
{
var anumationFileId = (await GetAnimationFileOption()).GetValue(string.Empty);
if (anumationFileId.IsNullOrEmpty()) return false;
await Bot.SendAnimationAsync(Chat.Id, anumationFileId,
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) {}
}
}