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) {}
}
}
@@ -0,0 +1,38 @@
#nullable enable
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Data;
using Telegram.Bot;
namespace West.TelegramBot.ChatManagement.Handler.Fun
{
public class Bayan : AbstractAnimationReply
{
public Bayan(CoreContext db) : base(db) { }
protected override string GetAnimationName()
{
return "bayan";
}
[Command(InChat.Public, "setbayan", CommandParseMode.Both)]
public async Task HandleSetBayan()
{
await HandleSetAnimation();
}
[Message(InChat.Public, "(?i)баян", MessageFlag.IsReply, true)]
public async Task HandleBayan()
{
var msg = Message;
if (msg == null || !await HandleAnimation(msg.ReplyToMessage!.MessageId))
{
return;
}
await Bot.DeleteMessageAsync(Chat.Id, msg.MessageId);
}
}
}
+21 -15
View File
@@ -3,33 +3,39 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using BotFramework.Attributes; using BotFramework.Attributes;
using BotFramework.Enums; using BotFramework.Enums;
using Castle.Core.Internal;
using Kruzya.TelegramBot.Core.Data; using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions; using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot;
namespace West.TelegramBot.ChatManagement.Handler.Fun namespace West.TelegramBot.ChatManagement.Handler.Fun
{ {
public class Python : ManagementHandler public class Python : AbstractAnimationReply
{ {
[Command(InChat.Public, "setpython", CommandParseMode.Both)] protected override string GetAnimationName()
public async Task HandleSetpython()
{ {
var animation = Message?.ReplyToMessage?.Animation; return "python";
if (await CanPunish() || animation == null) return;
var pythonFile = await GetPythonFileOption();
pythonFile.SetValue(animation.FileId);
Db.MarkAsModified(pythonFile);
} }
[Command(InChat.Public, "setpython", CommandParseMode.Both)]
public async Task HandleSetPython()
{
await HandleSetAnimation();
}
[Message(InChat.Public, "(?i)питон|пайтон|python|путхон", MessageFlag.HasText, true)] [Message(InChat.Public, "(?i)питон|пайтон|python|путхон", MessageFlag.HasText, true)]
public async Task HandlePython() public async Task HandlePython()
{ {
var pythonFileId = (await GetPythonFileOption()).GetValue(string.Empty); // var cmdEntity = Message?.Entities?.Find(e => e.Type == MessageEntityType.BotCommand);
if (pythonFileId.IsNullOrEmpty()) return; // if (cmdEntity is { Length: > 0 })
// {
// return;
// }
await Bot.SendAnimationAsync(Chat.Id, pythonFileId, replyToMessageId: Message?.MessageId); if (Message!.Text!.Contains("/setpython"))
{
return;
}
await HandleAnimation();
} }
private async Task<BotUserValue> GetPythonFileOption() private async Task<BotUserValue> GetPythonFileOption()