* Added new method for filters report inspection
* Added ArgumentCount filter * Fixed AddHandler method throwing exception on handler without parameterless constructors
This commit is contained in:
@@ -3,6 +3,10 @@ using Telegrator.Filters;
|
||||
|
||||
namespace Telegrator.Annotations
|
||||
{
|
||||
public class ArgumentCountAttribute(int count)
|
||||
: MessageFilterAttribute(new ArgumentCountFilter(count))
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Attribute for filtering messages where a command argument starts with the specified content.
|
||||
/// </summary>
|
||||
@@ -50,7 +54,7 @@ namespace Telegrator.Annotations
|
||||
/// <param name="options">The regex options to use for the pattern matching.</param>
|
||||
/// <param name="matchTimeout">The timeout for the regex match operation.</param>
|
||||
/// <param name="index">The index of the argument to check (0-based).</param>
|
||||
public class ArgumentRegexAttribute(string pattern, RegexOptions options = RegexOptions.None, TimeSpan matchTimeout = default, int index = 0)
|
||||
: MessageFilterAttribute(new ArgumentRegexFilter(pattern, options, matchTimeout, index))
|
||||
public class ArgumentRegexAttribute(string pattern, RegexOptions options = RegexOptions.None, int index = 0)
|
||||
: MessageFilterAttribute(new ArgumentRegexFilter(pattern, options, index: index))
|
||||
{ }
|
||||
}
|
||||
|
||||
@@ -42,6 +42,18 @@ namespace Telegrator.Filters
|
||||
protected abstract bool CanPassNext(FilterExecutionContext<Message> context);
|
||||
}
|
||||
|
||||
public class ArgumentCountFilter(int count) : Filter<Message>
|
||||
{
|
||||
private readonly int Count = count;
|
||||
|
||||
public override bool CanPass(FilterExecutionContext<Message> context)
|
||||
{
|
||||
CommandHandlerAttribute attr = context.CompletedFilters.Get<CommandHandlerAttribute>(0);
|
||||
string[] args = attr.Arguments ??= context.Input.SplitArgs();
|
||||
return args.Length >= Count;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Filter that checks if a command argument starts with a specified content.
|
||||
/// </summary>
|
||||
|
||||
@@ -38,27 +38,53 @@ namespace Telegrator.Handlers.Components
|
||||
/// </summary>
|
||||
public List<FilterFallbackInfo> UpdateFilters { get; } = [];
|
||||
|
||||
public bool Only(string name, int index = 0)
|
||||
{
|
||||
FilterFallbackInfo? info = UpdateFilters.SingleSafe(info => info.Failed);
|
||||
if (info != null && info.Name != name)
|
||||
return false;
|
||||
|
||||
FilterFallbackInfo? target = UpdateFilters.ElementAtOrDefault(index);
|
||||
return ReferenceEquals(target, info);
|
||||
}
|
||||
|
||||
public bool Only(string[] names)
|
||||
{
|
||||
return UpdateFilters
|
||||
.Where(info => info.Failed)
|
||||
.Select(info => info.Name)
|
||||
.SequenceEqual(names);
|
||||
}
|
||||
|
||||
public bool Except(string name, int index = 0)
|
||||
{
|
||||
FilterFallbackInfo? info = UpdateFilters.SingleSafe(info => !info.Failed);
|
||||
if (info != null && info.Name != name)
|
||||
return false;
|
||||
|
||||
FilterFallbackInfo? target = UpdateFilters.ElementAtOrDefault(index);
|
||||
return ReferenceEquals(target, info);
|
||||
}
|
||||
|
||||
public bool Except(string[] names)
|
||||
{
|
||||
return UpdateFilters
|
||||
.Where(info => !info.Failed)
|
||||
.Select(info => info.Name)
|
||||
.SequenceEqual(names);
|
||||
}
|
||||
|
||||
public bool ExceptAttribute<T>(int index = 0) where T : UpdateFilterAttributeBase
|
||||
=> Except(nameof(T), index);
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the failure is due to a specific attribute type, excluding other failures.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The attribute type to check for.</typeparam>
|
||||
/// <param name="index">The index of the filter to check (default: 0).</param>
|
||||
/// <returns>True if the failure is exclusively due to the specified attribute type; otherwise, false.</returns>
|
||||
public bool ExceptAttribute<T>(int index = 0) where T : UpdateFilterAttributeBase
|
||||
{
|
||||
string name = typeof(T).Name;
|
||||
|
||||
IEnumerable<FilterFallbackInfo> failed = UpdateFilters.Where(info => info.Failed);
|
||||
if (failed.Count() != 1)
|
||||
return false;
|
||||
|
||||
FilterFallbackInfo info = failed.ElementAt(0);
|
||||
if (info.Name != name)
|
||||
return false;
|
||||
|
||||
FilterFallbackInfo? target = UpdateFilters.ElementAtOrDefault(index);
|
||||
return target == info;
|
||||
}
|
||||
public bool OnlyAttribute<T>(int index = 0) where T : UpdateFilterAttributeBase
|
||||
=> Only(nameof(T), index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -3,7 +3,6 @@ using Telegram.Bot.Types.Enums;
|
||||
using Telegrator.Annotations;
|
||||
using Telegrator.Attributes;
|
||||
using Telegrator.Configuration;
|
||||
using Telegrator.Handlers.Components;
|
||||
using Telegrator.MadiatorCore;
|
||||
using Telegrator.MadiatorCore.Descriptors;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Reflection;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
@@ -643,9 +644,6 @@ namespace Telegrator
|
||||
|
||||
if (handlerType.IsCustomDescriptorsProvider())
|
||||
{
|
||||
if (!handlerType.HasParameterlessCtor())
|
||||
throw new Exception();
|
||||
|
||||
ICustomDescriptorsProvider provider = (ICustomDescriptorsProvider)Activator.CreateInstance(handlerType);
|
||||
foreach (HandlerDescriptor handlerDescriptor in provider.DescribeHandlers())
|
||||
handlers.AddDescriptor(handlerDescriptor);
|
||||
@@ -944,23 +942,6 @@ namespace Telegrator
|
||||
return source;
|
||||
}
|
||||
|
||||
/* Found built in method :_(
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="IEnumerable{T}"/> with the elements of the <paramref name="source"/> that were successfully cast to the <typeparamref name="TResult"/>
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult"></typeparam>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<TResult> WhereCast<TResult>(this IEnumerable source)
|
||||
{
|
||||
foreach (object value in source)
|
||||
{
|
||||
if (value is TResult result)
|
||||
yield return result;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of a key in a dictionary, or if the key does not exist, adds it
|
||||
/// </summary>
|
||||
@@ -1027,6 +1008,32 @@ namespace Telegrator
|
||||
list.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public static int IndexOf<T>(this IEnumerable<T> source, Func<T, bool> predicate)
|
||||
{
|
||||
int index = 0;
|
||||
foreach (T item in source)
|
||||
{
|
||||
if (predicate.Invoke(item))
|
||||
return index;
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Repeat<T>(this T item, int times)
|
||||
=> Enumerable.Range(0, times).Select(_ => item);
|
||||
|
||||
public static T? SingleSafe<T>(this IEnumerable<T> source)
|
||||
=> source.Count() == 1 ? source.ElementAt(0) : default;
|
||||
|
||||
public static T? SingleSafe<T>(this IEnumerable<T> source, Func<T, bool> predicate)
|
||||
{
|
||||
source = source.Where(predicate);
|
||||
return source.Count() == 1 ? source.ElementAt(0) : default;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1190,6 +1197,22 @@ namespace Telegrator
|
||||
yield return chunk.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public static string FirstLetterToUpper(this string target)
|
||||
{
|
||||
char[] chars = target.ToCharArray();
|
||||
int index = chars.IndexOf(char.IsLetter);
|
||||
chars[index] = char.ToUpper(chars[index]);
|
||||
return new string(chars);
|
||||
}
|
||||
|
||||
public static string FirstLetterToLower(this string target)
|
||||
{
|
||||
char[] chars = target.ToCharArray();
|
||||
int index = chars.IndexOf(char.IsLetter);
|
||||
chars[index] = char.ToLower(chars[index]);
|
||||
return new string(chars);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user