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; 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..18ed732 --- /dev/null +++ b/modules/ChatManagement/Handler/Fun/Bayan.cs @@ -0,0 +1,39 @@ +#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.HasText | MessageFlag.IsReply, true)] + public async Task HandleBayan() + { + var msg = Message; + var replyMsg = msg?.ReplyToMessage!; + if (msg == null || !await HandleAnimation(replyMsg.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 new file mode 100644 index 0000000..8ebd425 --- /dev/null +++ b/modules/ChatManagement/Handler/Fun/Python.cs @@ -0,0 +1,48 @@ +#nullable enable + +using System.Threading.Tasks; +using BotFramework.Attributes; +using BotFramework.Enums; +using Kruzya.TelegramBot.Core.Data; +using Kruzya.TelegramBot.Core.Extensions; + +namespace West.TelegramBot.ChatManagement.Handler.Fun +{ + public class Python : AbstractAnimationReply + { + protected override string GetAnimationName() + { + 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 cmdEntity = Message?.Entities?.Find(e => e.Type == MessageEntityType.BotCommand); + // if (cmdEntity is { Length: > 0 }) + // { + // return; + // } + + if (Message!.Text!.Contains("/setpython")) + { + return; + } + + await HandleAnimation(); + } + + private async Task GetPythonFileOption() + { + return await Db.UserValues.FindOrCreateOption(Chat.Id, "pythonFile"); + } + + public Python(CoreContext db) : base(db) { } + } +} \ No newline at end of file