* Fixed "MentionedAttribute"

* Improved "MentionedFilter"
This commit is contained in:
2025-07-28 02:48:06 +04:00
parent 76cf6eb4a2
commit 5a93623469
4 changed files with 27 additions and 6 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ namespace Telegrator.Annotations
/// Initializes a new instance of the MentionedAttribute that matches any mention.
/// </summary>
public MentionedAttribute()
: base(new MessageHasEntityFilter(MessageEntityType.Mention, 0, null), new MentionedFilter()) { }
: base(new MessageHasEntityFilter(MessageEntityType.Mention, null, null), new MentionedFilter()) { }
/// <summary>
/// Initializes a new instance of the MentionedAttribute that matches mentions at a specific offset.
+14 -2
View File
@@ -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<MessageHasEntityFilter> entityFilter = context.CompletedFilters.Get<MessageHasEntityFilter>();
return entityFilter.Any(fltr => fltr.FoundEntities.Any(ent => Target.Text.Substring(ent.Offset + 1, ent.Length - 1) == userName));
IEnumerable<MessageEntity> entities = context.CompletedFilters
.Get<MessageHasEntityFilter>()
.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;
}
}
}
+3 -3
View File
@@ -202,7 +202,7 @@ namespace Telegrator.Filters
/// <param name="type">The entity type to filter by.</param>
/// <param name="offset">The offset to filter by.</param>
/// <param name="length">The length to filter by.</param>
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
/// <param name="type">The entity type to filter by.</param>
/// <param name="content">The content to filter by.</param>
/// <param name="stringComparison">The string comparison to use.</param>
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
/// <param name="length">The length to filter by.</param>
/// <param name="content">The content to filter by.</param>
/// <param name="stringComparison">The string comparison to use.</param>
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;
@@ -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<IFilter<Update>> branchFiltersList = HandlerInspector.GetFilterAttributes(branch, HandlingUpdateType).ToList();
branchFiltersList.AddRange(handlerFilters);