mirror of
https://github.com/yawaflua/Telegram.Net.git
synced 2025-12-09 20:19:28 +02:00
Fix error with providing attributes
This commit is contained in:
@@ -20,12 +20,12 @@ public class TelegramHostedService : IHostedService
|
|||||||
private IServiceCollection isc { get; }
|
private IServiceCollection isc { get; }
|
||||||
internal TelegramBotClient Client { get; set; }
|
internal TelegramBotClient Client { get; set; }
|
||||||
private ITelegramBotConfig Config { get; }
|
private ITelegramBotConfig Config { get; }
|
||||||
internal Dictionary<string, Func<ITelegramBotClient, Message, CancellationToken, Task>> CommandHandler { get; } = new();
|
internal Dictionary<string, Func<ITelegramBotClient, Message, CancellationToken, Task>> CommandHandler { get; set; } = new();
|
||||||
internal List<Func<ITelegramBotClient, Message, CancellationToken, Task>> EditedMessageHandler { get; } = new();
|
internal List<Func<ITelegramBotClient, Message, CancellationToken, Task>> EditedMessageHandler { get; set; } = new();
|
||||||
internal Dictionary<string, Func<ITelegramBotClient, CallbackQuery,CancellationToken, Task>> CallbackQueryHandler { get; } = new();
|
internal Dictionary<string, Func<ITelegramBotClient, CallbackQuery,CancellationToken, Task>> CallbackQueryHandler { get; set; } = new();
|
||||||
internal Dictionary<string, Func<ITelegramBotClient, InlineQuery ,CancellationToken, Task>> InlineHandler { get; } = new();
|
internal Dictionary<string, Func<ITelegramBotClient, InlineQuery ,CancellationToken, Task>> InlineHandler { get; set; } = new();
|
||||||
internal Func<ITelegramBotClient, PreCheckoutQuery,CancellationToken, Task>? PreCheckoutHandler { get; set; }
|
internal Func<ITelegramBotClient, PreCheckoutQuery,CancellationToken, Task>? PreCheckoutHandler { get; set; }
|
||||||
internal List<Func<ITelegramBotClient, Update, CancellationToken, Task>> DefaultUpdateHandler { get; } = new();
|
internal List<Func<ITelegramBotClient, Update, CancellationToken, Task>> DefaultUpdateHandler { get; set; } = new();
|
||||||
internal static ILogger<TelegramHostedService> _logger;
|
internal static ILogger<TelegramHostedService> _logger;
|
||||||
|
|
||||||
public TelegramHostedService(ITelegramBotConfig config, IServiceCollection isc, ILogger<TelegramHostedService> logger)
|
public TelegramHostedService(ITelegramBotConfig config, IServiceCollection isc, ILogger<TelegramHostedService> logger)
|
||||||
@@ -120,31 +120,28 @@ public class TelegramHostedService : IHostedService
|
|||||||
|
|
||||||
constructor.Invoke(parameters);
|
constructor.Invoke(parameters);
|
||||||
|
|
||||||
void AddHandler<T>(Dictionary<string, Func<ITelegramBotClient, T, CancellationToken, Task>> dictionary, string key) where T : class
|
switch (method.GetCustomAttributes().First(t => attributeTypes.Contains(t.GetType())))
|
||||||
{
|
{
|
||||||
if (!IsValidHandlerMethod(method, typeof(T))) return;
|
case CommandAttribute command when IsValidHandlerMethod(method, typeof(Message)):
|
||||||
|
var commandHandler = CreateDelegate<Message>(method);
|
||||||
var handler = CreateDelegate<T>(method);
|
if (!CommandHandler.TryAdd(command.Command, commandHandler))
|
||||||
if (!dictionary.TryAdd(key, handler))
|
throw new Exception($"Failed to add command: {command.Command}");
|
||||||
throw new Exception($"Failed to add {key} to {dictionary.GetType().Name}");
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (method.GetCustomAttributes().FirstOrDefault())
|
|
||||||
{
|
|
||||||
case CommandAttribute command:
|
|
||||||
AddHandler(CommandHandler, command.Command);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CallbackAttribute callback:
|
case CallbackAttribute callback when IsValidHandlerMethod(method, typeof(CallbackQuery)):
|
||||||
AddHandler(CallbackQueryHandler, callback.QueryId);
|
var callbackHandler = CreateDelegate<CallbackQuery>(method);
|
||||||
|
if (!CallbackQueryHandler.TryAdd(callback.QueryId, callbackHandler))
|
||||||
|
throw new Exception($"Failed to add callback: {callback.QueryId}");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case EditMessageAttribute _ when IsValidHandlerMethod(method, typeof(Message)):
|
case EditMessageAttribute _ when IsValidHandlerMethod(method, typeof(Message)):
|
||||||
EditedMessageHandler.Add(CreateDelegate<Message>(method));
|
EditedMessageHandler.Add(CreateDelegate<Message>(method));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case InlineAttribute inline:
|
case InlineAttribute inline when IsValidHandlerMethod(method, typeof(InlineQuery)):
|
||||||
AddHandler(InlineHandler, inline.InlineId);
|
var inlineHandler = CreateDelegate<InlineQuery>(method);
|
||||||
|
if (!InlineHandler.TryAdd(inline.InlineId, inlineHandler))
|
||||||
|
throw new Exception($"Failed to add inline: {inline.InlineId}");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PreCheckoutAttribute _ when IsValidHandlerMethod(method, typeof(PreCheckoutQuery)):
|
case PreCheckoutAttribute _ when IsValidHandlerMethod(method, typeof(PreCheckoutQuery)):
|
||||||
|
|||||||
Reference in New Issue
Block a user