From df11e1d327f6db3436b0b8258900244fffa75e1c Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Wed, 29 Dec 2021 00:22:16 +0200 Subject: [PATCH 1/5] Add python trigger. --- modules/ChatManagement/Handler/Fun/Python.cs | 42 ++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 modules/ChatManagement/Handler/Fun/Python.cs diff --git a/modules/ChatManagement/Handler/Fun/Python.cs b/modules/ChatManagement/Handler/Fun/Python.cs new file mode 100644 index 0000000..78f5112 --- /dev/null +++ b/modules/ChatManagement/Handler/Fun/Python.cs @@ -0,0 +1,42 @@ +#nullable enable + +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 + { + [Command(InChat.Public, "setpython", CommandParseMode.Both)] + public async Task HandleSetpython() + { + var animation = Message?.ReplyToMessage?.Animation; + if (await CanPunish() || animation == null) return; + + var pythonFile = await GetPythonFileOption(); + pythonFile.SetValue(animation.FileId); + Db.MarkAsModified(pythonFile); + } + + [Message(InChat.Public, "(?i)питон|пайтон|python|путхон", MessageFlag.HasText, true)] + public async Task HandlePython() + { + var pythonFileId = (await GetPythonFileOption()).GetValue(string.Empty); + if (pythonFileId.IsNullOrEmpty()) return; + + await Bot.SendAnimationAsync(Chat.Id, pythonFileId, replyToMessageId: Message?.MessageId); + } + + private async Task GetPythonFileOption() + { + return await Db.UserValues.FindOrCreateOption(Chat.Id, "pythonFile"); + } + + public Python(CoreContext db) : base(db) { } + } +} \ No newline at end of file From ae5d8ade8c12d80c34860bf6a2f5067d96c97266 Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Wed, 29 Dec 2021 00:57:37 +0200 Subject: [PATCH 2/5] Animation trigger refactoring. BAYAN. --- .../Handler/Fun/AbstractAnimationReply.cs | 42 +++++++++++++++++++ modules/ChatManagement/Handler/Fun/Bayan.cs | 38 +++++++++++++++++ modules/ChatManagement/Handler/Fun/Python.cs | 36 +++++++++------- 3 files changed, 101 insertions(+), 15 deletions(-) create mode 100644 modules/ChatManagement/Handler/Fun/AbstractAnimationReply.cs create mode 100644 modules/ChatManagement/Handler/Fun/Bayan.cs 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() From e77ef7a9bd06f26386d0607532849b2349dd5950 Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Wed, 29 Dec 2021 01:32:14 +0200 Subject: [PATCH 3/5] Fix bayan reply to bot. --- modules/ChatManagement/Handler/Fun/Bayan.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/ChatManagement/Handler/Fun/Bayan.cs b/modules/ChatManagement/Handler/Fun/Bayan.cs index 380a164..1fb35b2 100644 --- a/modules/ChatManagement/Handler/Fun/Bayan.cs +++ b/modules/ChatManagement/Handler/Fun/Bayan.cs @@ -23,11 +23,12 @@ namespace West.TelegramBot.ChatManagement.Handler.Fun await HandleSetAnimation(); } - [Message(InChat.Public, "(?i)баян", MessageFlag.IsReply, true)] + [Message(InChat.Public, "(?i)баян|боян", regex: true)] public async Task HandleBayan() { var msg = Message; - if (msg == null || !await HandleAnimation(msg.ReplyToMessage!.MessageId)) + var replyMsg = msg?.ReplyToMessage; + if (msg == null || replyMsg == null || replyMsg.From!.IsBot || !await HandleAnimation(replyMsg.MessageId)) { return; } From a78851e7f575f420e29c52c95333f725666c925b Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Wed, 29 Dec 2021 02:06:53 +0200 Subject: [PATCH 4/5] Refactor bayan. --- modules/ChatManagement/Handler/Fun/Bayan.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/ChatManagement/Handler/Fun/Bayan.cs b/modules/ChatManagement/Handler/Fun/Bayan.cs index 1fb35b2..18ed732 100644 --- a/modules/ChatManagement/Handler/Fun/Bayan.cs +++ b/modules/ChatManagement/Handler/Fun/Bayan.cs @@ -23,12 +23,12 @@ namespace West.TelegramBot.ChatManagement.Handler.Fun await HandleSetAnimation(); } - [Message(InChat.Public, "(?i)баян|боян", regex: true)] + [Message(InChat.Public, "(?i)баян|боян", MessageFlag.HasText | MessageFlag.IsReply, true)] public async Task HandleBayan() { var msg = Message; - var replyMsg = msg?.ReplyToMessage; - if (msg == null || replyMsg == null || replyMsg.From!.IsBot || !await HandleAnimation(replyMsg.MessageId)) + var replyMsg = msg?.ReplyToMessage!; + if (msg == null || !await HandleAnimation(replyMsg.MessageId)) { return; } From 1d46b1e380b873b70c9cb0e54d53434043fa2bd9 Mon Sep 17 00:00:00 2001 From: West14 <30056636+West14@users.noreply.github.com> Date: Wed, 29 Dec 2021 14:51:49 +0200 Subject: [PATCH 5/5] Fix bot punishements. --- modules/ChatManagement/Extensions/BotExtension.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/ChatManagement/Extensions/BotExtension.cs b/modules/ChatManagement/Extensions/BotExtension.cs index 123d715..3203be8 100644 --- a/modules/ChatManagement/Extensions/BotExtension.cs +++ b/modules/ChatManagement/Extensions/BotExtension.cs @@ -19,7 +19,7 @@ namespace West.TelegramBot.ChatManagement.Extensions if (victim != null && canUse) { var victimMember = await bot.GetChatMemberAsync(chat, victim.Id); - canUse = victimMember is not ChatMemberOwner && victimMember is not ChatMemberAdministrator; + canUse = victimMember is not ChatMemberOwner && victimMember is not ChatMemberAdministrator && !victim.IsBot; } return canUse;