diff --git a/modules/ChatManagement/Handler/Fun/AbstractAnimationReply.cs b/modules/ChatManagement/Handler/Fun/AbstractAnimationReply.cs new file mode 100644 index 0000000..cb5d6c1 --- /dev/null +++ b/modules/ChatManagement/Handler/Fun/AbstractAnimationReply.cs @@ -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 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 GetAnimationFileOption() + { + return await Db.UserValues.FindOrCreateOption(Chat.Id, $"{GetAnimationName()}File"); + } + + protected AbstractAnimationReply(CoreContext db) : base(db) {} + } +} \ No newline at end of file diff --git a/modules/ChatManagement/Handler/Fun/Bayan.cs b/modules/ChatManagement/Handler/Fun/Bayan.cs new file mode 100644 index 0000000..380a164 --- /dev/null +++ b/modules/ChatManagement/Handler/Fun/Bayan.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/modules/ChatManagement/Handler/Fun/Python.cs b/modules/ChatManagement/Handler/Fun/Python.cs index 78f5112..8ebd425 100644 --- a/modules/ChatManagement/Handler/Fun/Python.cs +++ b/modules/ChatManagement/Handler/Fun/Python.cs @@ -3,33 +3,39 @@ 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; namespace West.TelegramBot.ChatManagement.Handler.Fun { - public class Python : ManagementHandler + public class Python : AbstractAnimationReply { - [Command(InChat.Public, "setpython", CommandParseMode.Both)] - public async Task HandleSetpython() + protected override string GetAnimationName() { - var animation = Message?.ReplyToMessage?.Animation; - if (await CanPunish() || animation == null) return; - - var pythonFile = await GetPythonFileOption(); - pythonFile.SetValue(animation.FileId); - Db.MarkAsModified(pythonFile); + return "python"; } - + + [Command(InChat.Public, "setpython", CommandParseMode.Both)] + public async Task HandleSetPython() + { + await HandleSetAnimation(); + } + [Message(InChat.Public, "(?i)питон|пайтон|python|путхон", MessageFlag.HasText, true)] public async Task HandlePython() { - var pythonFileId = (await GetPythonFileOption()).GetValue(string.Empty); - if (pythonFileId.IsNullOrEmpty()) return; + // var cmdEntity = Message?.Entities?.Find(e => e.Type == MessageEntityType.BotCommand); + // 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 GetPythonFileOption()