From c2e04598f67a58f5714c9482d8f945222ce05ac2 Mon Sep 17 00:00:00 2001 From: Dmitri Shimanski Date: Fri, 21 Mar 2025 01:05:55 +0200 Subject: [PATCH] Fix error with providing attributes --- .../Services/TelegramHostedService.cs | 39 +++++++++---------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/Telegram.Net/Services/TelegramHostedService.cs b/Telegram.Net/Services/TelegramHostedService.cs index 11c786b..030a079 100644 --- a/Telegram.Net/Services/TelegramHostedService.cs +++ b/Telegram.Net/Services/TelegramHostedService.cs @@ -20,12 +20,12 @@ public class TelegramHostedService : IHostedService private IServiceCollection isc { get; } internal TelegramBotClient Client { get; set; } private ITelegramBotConfig Config { get; } - internal Dictionary> CommandHandler { get; } = new(); - internal List> EditedMessageHandler { get; } = new(); - internal Dictionary> CallbackQueryHandler { get; } = new(); - internal Dictionary> InlineHandler { get; } = new(); + internal Dictionary> CommandHandler { get; set; } = new(); + internal List> EditedMessageHandler { get; set; } = new(); + internal Dictionary> CallbackQueryHandler { get; set; } = new(); + internal Dictionary> InlineHandler { get; set; } = new(); internal Func? PreCheckoutHandler { get; set; } - internal List> DefaultUpdateHandler { get; } = new(); + internal List> DefaultUpdateHandler { get; set; } = new(); internal static ILogger _logger; public TelegramHostedService(ITelegramBotConfig config, IServiceCollection isc, ILogger logger) @@ -120,31 +120,28 @@ public class TelegramHostedService : IHostedService constructor.Invoke(parameters); - void AddHandler(Dictionary> dictionary, string key) where T : class + switch (method.GetCustomAttributes().First(t => attributeTypes.Contains(t.GetType()))) { - if (!IsValidHandlerMethod(method, typeof(T))) return; - - var handler = CreateDelegate(method); - if (!dictionary.TryAdd(key, handler)) - throw new Exception($"Failed to add {key} to {dictionary.GetType().Name}"); - } - - switch (method.GetCustomAttributes().FirstOrDefault()) - { - case CommandAttribute command: - AddHandler(CommandHandler, command.Command); + case CommandAttribute command when IsValidHandlerMethod(method, typeof(Message)): + var commandHandler = CreateDelegate(method); + if (!CommandHandler.TryAdd(command.Command, commandHandler)) + throw new Exception($"Failed to add command: {command.Command}"); break; - case CallbackAttribute callback: - AddHandler(CallbackQueryHandler, callback.QueryId); + case CallbackAttribute callback when IsValidHandlerMethod(method, typeof(CallbackQuery)): + var callbackHandler = CreateDelegate(method); + if (!CallbackQueryHandler.TryAdd(callback.QueryId, callbackHandler)) + throw new Exception($"Failed to add callback: {callback.QueryId}"); break; case EditMessageAttribute _ when IsValidHandlerMethod(method, typeof(Message)): EditedMessageHandler.Add(CreateDelegate(method)); break; - case InlineAttribute inline: - AddHandler(InlineHandler, inline.InlineId); + case InlineAttribute inline when IsValidHandlerMethod(method, typeof(InlineQuery)): + var inlineHandler = CreateDelegate(method); + if (!InlineHandler.TryAdd(inline.InlineId, inlineHandler)) + throw new Exception($"Failed to add inline: {inline.InlineId}"); break; case PreCheckoutAttribute _ when IsValidHandlerMethod(method, typeof(PreCheckoutQuery)):