diff --git a/Kruzya.TelegramBot.sln b/Kruzya.TelegramBot.sln index 80208c2..9cd7ce6 100644 --- a/Kruzya.TelegramBot.sln +++ b/Kruzya.TelegramBot.sln @@ -17,6 +17,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Destiny2.WhereIsXur", "modu EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatManagement", "modules\ChatManagement\ChatManagement.csproj", "{55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}" EndProject +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entertainment", "modules\Entertainment\Entertainment.csproj", "{1D62101B-C2B1-4E79-8F7A-690E44E3EE81}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -51,6 +54,10 @@ Global {55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}.Debug|Any CPU.Build.0 = Debug|Any CPU {55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}.Release|Any CPU.ActiveCfg = Release|Any CPU {55ACF7F7-409A-45EE-A41D-D8417DE7EAA7}.Release|Any CPU.Build.0 = Release|Any CPU + {1D62101B-C2B1-4E79-8F7A-690E44E3EE81}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1D62101B-C2B1-4E79-8F7A-690E44E3EE81}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1D62101B-C2B1-4E79-8F7A-690E44E3EE81}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1D62101B-C2B1-4E79-8F7A-690E44E3EE81}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {F1E1FBB7-ABA4-4304-9A42-D4975822B002} = {C7821F15-DEDD-474F-A575-A296D4B58F10} @@ -58,5 +65,6 @@ Global {B82E3339-3B34-4600-8C59-C5A3640B1982} = {C7821F15-DEDD-474F-A575-A296D4B58F10} {AE782132-BED8-4D1E-98FA-CB768171D332} = {C7821F15-DEDD-474F-A575-A296D4B58F10} {55ACF7F7-409A-45EE-A41D-D8417DE7EAA7} = {C7821F15-DEDD-474F-A575-A296D4B58F10} + {1D62101B-C2B1-4E79-8F7A-690E44E3EE81} = {C7821F15-DEDD-474F-A575-A296D4B58F10} EndGlobalSection EndGlobal diff --git a/docker/Dockerfile.aarch64 b/docker/Dockerfile.aarch64 index e6df425..3bdd7db 100644 --- a/docker/Dockerfile.aarch64 +++ b/docker/Dockerfile.aarch64 @@ -11,6 +11,7 @@ COPY ./modules/Destiny2.WhereIsXur/*.csproj ./modules/Destiny2.WhereIsXur/ COPY ./modules/RichSiteSummary/*.csproj ./modules/RichSiteSummary/ COPY ./modules/UrlLimitations/*.csproj ./modules/UrlLimitations/ COPY ./modules/ChatManagement/*.csproj ./modules/ChatManagement/ +COPY ./modules/Entertainment/*.csproj ./modules/Entertainment/ RUN dotnet restore Kruzya.TelegramBot.sln # Copy all project files diff --git a/docker/Dockerfile.amd64 b/docker/Dockerfile.amd64 index 8ef35e3..00ca0d8 100644 --- a/docker/Dockerfile.amd64 +++ b/docker/Dockerfile.amd64 @@ -11,6 +11,7 @@ COPY ./modules/Destiny2.WhereIsXur/*.csproj ./modules/Destiny2.WhereIsXur/ COPY ./modules/RichSiteSummary/*.csproj ./modules/RichSiteSummary/ COPY ./modules/UrlLimitations/*.csproj ./modules/UrlLimitations/ COPY ./modules/ChatManagement/*.csproj ./modules/ChatManagement/ +COPY ./modules/Entertainment/*.csproj ./modules/Entertainment/ RUN dotnet restore Kruzya.TelegramBot.sln # Copy all project files diff --git a/modules/ChatManagement/Handler/Fun/Bayan.cs b/modules/ChatManagement/Handler/Fun/Bayan.cs deleted file mode 100644 index 18ed732..0000000 --- a/modules/ChatManagement/Handler/Fun/Bayan.cs +++ /dev/null @@ -1,39 +0,0 @@ -#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 deleted file mode 100644 index 8ebd425..0000000 --- a/modules/ChatManagement/Handler/Fun/Python.cs +++ /dev/null @@ -1,48 +0,0 @@ -#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 diff --git a/modules/Entertainment/Entertainment.cs b/modules/Entertainment/Entertainment.cs new file mode 100644 index 0000000..941b880 --- /dev/null +++ b/modules/Entertainment/Entertainment.cs @@ -0,0 +1,13 @@ +using Kruzya.TelegramBot.Core; +using West.TelegramBot.ChatManagement; + +namespace West.Entertainment +{ + public class Entertainment : Module + { + public Entertainment(Core core) : base(core) + { + core.EnsureLoaded(); + } + } +} \ No newline at end of file diff --git a/modules/Entertainment/Entertainment.csproj b/modules/Entertainment/Entertainment.csproj new file mode 100644 index 0000000..18890b1 --- /dev/null +++ b/modules/Entertainment/Entertainment.csproj @@ -0,0 +1,22 @@ + + + + net5.0 + TelegramBot.Entertainment + West.Entertainment + + + + + + + + + + + + + + + + diff --git a/modules/ChatManagement/Handler/Fun/AbstractAnimationReply.cs b/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs similarity index 59% rename from modules/ChatManagement/Handler/Fun/AbstractAnimationReply.cs rename to modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs index cb5d6c1..45edef0 100644 --- a/modules/ChatManagement/Handler/Fun/AbstractAnimationReply.cs +++ b/modules/Entertainment/Handler/Fun/AbstractAnimationReply.cs @@ -1,17 +1,28 @@ #nullable enable +using System.Text.RegularExpressions; 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; +using West.TelegramBot.ChatManagement.Handler; -namespace West.TelegramBot.ChatManagement.Handler.Fun +namespace West.Entertainment.Handler.Fun { public abstract class AbstractAnimationReply : ManagementHandler { + protected abstract Regex AnimationMatch + { + get; + } + protected abstract string GetAnimationName(); - + + protected virtual bool CanHandle() => true; + protected async Task HandleSetAnimation() { var animation = Message?.ReplyToMessage?.Animation; @@ -22,12 +33,14 @@ namespace West.TelegramBot.ChatManagement.Handler.Fun Db.MarkAsModified(animationFileOption); } - protected async Task HandleAnimation(int? replyToMessageId = null) + [Message(InChat.Public, MessageFlag.HasText)] + protected virtual async Task HandleAnimation(int? replyToMessageId = null) { - var anumationFileId = (await GetAnimationFileOption()).GetValue(string.Empty); - if (anumationFileId.IsNullOrEmpty()) return false; + var animationFileId = (await GetAnimationFileOption()).GetValue(string.Empty); + if (animationFileId.IsNullOrEmpty() || !AnimationMatch.IsMatch(Message!.Text!) || !CanHandle()) return false; + - await Bot.SendAnimationAsync(Chat.Id, anumationFileId, + await Bot.SendAnimationAsync(Chat.Id, animationFileId, replyToMessageId: replyToMessageId ?? Message?.MessageId); return true; } diff --git a/modules/Entertainment/Handler/Fun/Bayan.cs b/modules/Entertainment/Handler/Fun/Bayan.cs new file mode 100644 index 0000000..8635329 --- /dev/null +++ b/modules/Entertainment/Handler/Fun/Bayan.cs @@ -0,0 +1,37 @@ +#nullable enable + +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using BotFramework.Attributes; +using BotFramework.Enums; +using Kruzya.TelegramBot.Core.Data; +using Telegram.Bot; + +namespace West.Entertainment.Handler.Fun +{ + public class Bayan : AbstractAnimationReply + { + public Bayan(CoreContext db) : base(db) { } + + protected override Regex AnimationMatch => new("^баян$", RegexOptions.IgnoreCase); + + protected override string GetAnimationName() => "bayan"; + + + [Command(InChat.Public, "setbayan", CommandParseMode.Both)] + public async Task HandleSetBayan() => await HandleSetAnimation(); + + protected override async Task HandleAnimation(int? replyToMessageId = null) + { + if (await base.HandleAnimation(replyToMessageId)) + { + await Bot.DeleteMessageAsync(Chat.Id, Message!.MessageId); + } + + return true; + } + + protected override bool CanHandle() => Message?.ReplyToMessage != null; + + } +} \ No newline at end of file diff --git a/modules/Entertainment/Handler/Fun/Python.cs b/modules/Entertainment/Handler/Fun/Python.cs new file mode 100644 index 0000000..73e7aa0 --- /dev/null +++ b/modules/Entertainment/Handler/Fun/Python.cs @@ -0,0 +1,25 @@ +#nullable enable + +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using BotFramework.Attributes; +using BotFramework.Enums; +using Kruzya.TelegramBot.Core.Data; + +namespace West.Entertainment.Handler.Fun +{ + public class Python : AbstractAnimationReply + { + protected override Regex AnimationMatch => + new("питон|пайтон|python|путхон|питухон|петухон", RegexOptions.IgnoreCase); + + protected override string GetAnimationName() => "python"; + + protected override bool CanHandle() => !Message!.Text!.Contains("/setpython"); + + [Command(InChat.Public, "setpython", CommandParseMode.Both)] + public async Task HandleSetPython() => await HandleSetAnimation(); + + public Python(CoreContext db) : base(db) { } + } +} \ No newline at end of file