From 5a93623469cb986d5459cb98d3ba609e9ce9c62d Mon Sep 17 00:00:00 2001 From: Rikitav Date: Mon, 28 Jul 2025 02:48:06 +0400 Subject: [PATCH] * Fixed "MentionedAttribute" * Improved "MentionedFilter" --- Telegrator/Annotations/MentionedAttribute.cs | 2 +- Telegrator/Filters/MentionedFilter.cs | 16 ++++++++++++++-- Telegrator/Filters/MessageFilters.cs | 6 +++--- .../Components/BranchingUpdateHandler.cs | 9 +++++++++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Telegrator/Annotations/MentionedAttribute.cs b/Telegrator/Annotations/MentionedAttribute.cs index e70f3d4..76809bd 100644 --- a/Telegrator/Annotations/MentionedAttribute.cs +++ b/Telegrator/Annotations/MentionedAttribute.cs @@ -13,7 +13,7 @@ namespace Telegrator.Annotations /// Initializes a new instance of the MentionedAttribute that matches any mention. /// public MentionedAttribute() - : base(new MessageHasEntityFilter(MessageEntityType.Mention, 0, null), new MentionedFilter()) { } + : base(new MessageHasEntityFilter(MessageEntityType.Mention, null, null), new MentionedFilter()) { } /// /// Initializes a new instance of the MentionedAttribute that matches mentions at a specific offset. diff --git a/Telegrator/Filters/MentionedFilter.cs b/Telegrator/Filters/MentionedFilter.cs index e3351cc..bd761ce 100644 --- a/Telegrator/Filters/MentionedFilter.cs +++ b/Telegrator/Filters/MentionedFilter.cs @@ -1,4 +1,5 @@ using Telegram.Bot.Types; +using Telegram.Bot.Types.Enums; using Telegrator.Filters.Components; namespace Telegrator.Filters @@ -45,8 +46,19 @@ namespace Telegrator.Filters return false; string userName = Mention ?? context.BotInfo.User.Username ?? throw new ArgumentNullException(nameof(context), "MentionedFilter requires BotInfo to be initialized"); - IEnumerable entityFilter = context.CompletedFilters.Get(); - return entityFilter.Any(fltr => fltr.FoundEntities.Any(ent => Target.Text.Substring(ent.Offset + 1, ent.Length - 1) == userName)); + IEnumerable entities = context.CompletedFilters + .Get() + .SelectMany(ent => ent.FoundEntities) + .Where(ent => ent.Type == MessageEntityType.Mention); + + foreach (MessageEntity ent in entities) + { + string mention = Target.Text.Substring(ent.Offset + 1, ent.Length - 1); + if (mention == userName) + return true; + } + + return false; } } } diff --git a/Telegrator/Filters/MessageFilters.cs b/Telegrator/Filters/MessageFilters.cs index cadd02c..fd7cb3e 100644 --- a/Telegrator/Filters/MessageFilters.cs +++ b/Telegrator/Filters/MessageFilters.cs @@ -202,7 +202,7 @@ namespace Telegrator.Filters /// The entity type to filter by. /// The offset to filter by. /// The length to filter by. - public MessageHasEntityFilter(MessageEntityType type, int offset, int? length) + public MessageHasEntityFilter(MessageEntityType type, int? offset, int? length) { EntityType = type; Offset = offset; @@ -215,7 +215,7 @@ namespace Telegrator.Filters /// The entity type to filter by. /// The content to filter by. /// The string comparison to use. - public MessageHasEntityFilter(MessageEntityType type, string content, StringComparison stringComparison = StringComparison.CurrentCulture) + public MessageHasEntityFilter(MessageEntityType type, string? content, StringComparison stringComparison = StringComparison.CurrentCulture) { EntityType = type; Content = content; @@ -230,7 +230,7 @@ namespace Telegrator.Filters /// The length to filter by. /// The content to filter by. /// The string comparison to use. - public MessageHasEntityFilter(MessageEntityType type, int offset, int? length, string content, StringComparison stringComparison = StringComparison.CurrentCulture) + public MessageHasEntityFilter(MessageEntityType type, int? offset, int? length, string? content, StringComparison stringComparison = StringComparison.CurrentCulture) { EntityType = type; Offset = offset; diff --git a/Telegrator/Handlers/Components/BranchingUpdateHandler.cs b/Telegrator/Handlers/Components/BranchingUpdateHandler.cs index 670bdfe..0352127 100644 --- a/Telegrator/Handlers/Components/BranchingUpdateHandler.cs +++ b/Telegrator/Handlers/Components/BranchingUpdateHandler.cs @@ -88,6 +88,15 @@ namespace Telegrator.Handlers.Components if (!AllowedBranchReturnTypes.Any(branch.ReturnType.Equals)) throw new Exception(); + try + { + handlerAttribute = HandlerInspector.GetHandlerAttribute(branch); + } + finally + { + _ = 0xBAD + 0xC0DE; + } + List> branchFiltersList = HandlerInspector.GetFilterAttributes(branch, HandlingUpdateType).ToList(); branchFiltersList.AddRange(handlerFilters);