From 6c77e1ad419baceac01311ec873fad446a214a79 Mon Sep 17 00:00:00 2001 From: Ronny Gunawan <3048897+ronnygunawan@users.noreply.github.com> Date: Wed, 31 Jan 2024 00:40:56 +0700 Subject: [PATCH] Fix command priority --- .../AI/OpenAI/AskCommandHandler.cs | 6 +++- .../OpenAIImageGenerationPromptHandler.cs | 5 ++- .../AI/OpenAI/OpenAIImagePromptHandler.cs | 6 +++- .../AI/OpenAI/OpenAITextPromptHandler.cs | 6 +++- .../StabilityTextToImagePromptHandler.cs | 7 ++-- .../BotUpdate/Message/MessageUpdateHandler.cs | 8 ++++- .../BotUpdate/Message/AICallCommand.cs | 8 +++-- .../BotUpdate/Message/AIFollowUpMessage.cs | 6 ++-- .../BotUpdate/Message/AIResponseMessage.cs | 6 ++-- .../BotUpdate/Message/NormalMessage.cs | 9 ++--- .../BotUpdate/Message/SlashCommand.cs | 8 +++-- BotNet.Commands/ChatAggregate/Chat.cs | 6 +++- .../CommandPrioritization/CommandPriority.cs | 9 ----- .../CommandPriorityCategorizer.cs | 35 +++---------------- BotNet.Commands/SenderAggregate/Sender.cs | 18 +++++++--- 15 files changed, 77 insertions(+), 66 deletions(-) delete mode 100644 BotNet.Commands/CommandPrioritization/CommandPriority.cs diff --git a/BotNet.CommandHandlers/AI/OpenAI/AskCommandHandler.cs b/BotNet.CommandHandlers/AI/OpenAI/AskCommandHandler.cs index 508f939..b3c58bf 100644 --- a/BotNet.CommandHandlers/AI/OpenAI/AskCommandHandler.cs +++ b/BotNet.CommandHandlers/AI/OpenAI/AskCommandHandler.cs @@ -2,6 +2,7 @@ using BotNet.Commands.AI.OpenAI; using BotNet.Commands.BotUpdate.Message; using BotNet.Commands.ChatAggregate; +using BotNet.Commands.CommandPrioritization; using BotNet.Commands.SenderAggregate; using BotNet.Services.MarkdownV2; using BotNet.Services.OpenAI; @@ -17,11 +18,13 @@ public sealed class AskCommandHandler( ITelegramBotClient telegramBotClient, OpenAIClient openAIClient, ITelegramMessageCache telegramMessageCache, + CommandPriorityCategorizer commandPriorityCategorizer, ILogger logger ) : ICommandHandler { private readonly ITelegramBotClient _telegramBotClient = telegramBotClient; private readonly OpenAIClient _openAIClient = openAIClient; private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache; + private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer; private readonly ILogger _logger = logger; public async Task Handle(AskCommand askCommand, CancellationToken cancellationToken) { @@ -92,7 +95,8 @@ select ChatMessage.FromText( message: AIResponseMessage.FromMessage( message: responseMessage, replyToMessage: askCommand.Command, - callSign: "AI" + callSign: "AI", + commandPriorityCategorizer: _commandPriorityCategorizer ) ); }); diff --git a/BotNet.CommandHandlers/AI/OpenAI/OpenAIImageGenerationPromptHandler.cs b/BotNet.CommandHandlers/AI/OpenAI/OpenAIImageGenerationPromptHandler.cs index 9df6763..fd1833d 100644 --- a/BotNet.CommandHandlers/AI/OpenAI/OpenAIImageGenerationPromptHandler.cs +++ b/BotNet.CommandHandlers/AI/OpenAI/OpenAIImageGenerationPromptHandler.cs @@ -1,6 +1,7 @@ using BotNet.Commands; using BotNet.Commands.AI.OpenAI; using BotNet.Commands.BotUpdate.Message; +using BotNet.Commands.CommandPrioritization; using BotNet.Services.OpenAI.Skills; using Microsoft.Extensions.Logging; using Telegram.Bot; @@ -12,11 +13,13 @@ public sealed class OpenAIImageGenerationPromptHandler( ITelegramBotClient telegramBotClient, ImageGenerationBot imageGenerationBot, ITelegramMessageCache telegramMessageCache, + CommandPriorityCategorizer commandPriorityCategorizer, ILogger logger ) : ICommandHandler { private readonly ITelegramBotClient _telegramBotClient = telegramBotClient; private readonly ImageGenerationBot _imageGenerationBot = imageGenerationBot; private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache; + private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer; private readonly ILogger _logger = logger; public Task Handle(OpenAIImageGenerationPrompt command, CancellationToken cancellationToken) { @@ -62,7 +65,7 @@ await _telegramBotClient.DeleteMessageAsync( // Track thread _telegramMessageCache.Add( - NormalMessage.FromMessage(responseMessage) + NormalMessage.FromMessage(responseMessage, _commandPriorityCategorizer) ); } catch (OperationCanceledException) { // Terminate gracefully diff --git a/BotNet.CommandHandlers/AI/OpenAI/OpenAIImagePromptHandler.cs b/BotNet.CommandHandlers/AI/OpenAI/OpenAIImagePromptHandler.cs index 9c9dad1..8b50491 100644 --- a/BotNet.CommandHandlers/AI/OpenAI/OpenAIImagePromptHandler.cs +++ b/BotNet.CommandHandlers/AI/OpenAI/OpenAIImagePromptHandler.cs @@ -4,6 +4,7 @@ using BotNet.Commands.AI.Stability; using BotNet.Commands.BotUpdate.Message; using BotNet.Commands.ChatAggregate; +using BotNet.Commands.CommandPrioritization; using BotNet.Commands.SenderAggregate; using BotNet.Services.MarkdownV2; using BotNet.Services.OpenAI; @@ -21,6 +22,7 @@ public sealed class OpenAIImagePromptHandler( ICommandQueue commandQueue, ITelegramMessageCache telegramMessageCache, OpenAIClient openAIClient, + CommandPriorityCategorizer commandPriorityCategorizer, ILogger logger ) : ICommandHandler { internal static readonly RateLimiter VISION_RATE_LIMITER = RateLimiter.PerUserPerChat(1, TimeSpan.FromMinutes(15)); @@ -29,6 +31,7 @@ ILogger logger private readonly ICommandQueue _commandQueue = commandQueue; private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache; private readonly OpenAIClient _openAIClient = openAIClient; + private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer; private readonly ILogger _logger = logger; public Task Handle(OpenAIImagePrompt imagePrompt, CancellationToken cancellationToken) { @@ -185,7 +188,8 @@ await _telegramBotClient.EditMessageTextAsync( message: AIResponseMessage.FromMessage( message: responseMessage, replyToMessage: imagePrompt.Command, - callSign: imagePrompt.CallSign + callSign: imagePrompt.CallSign, + commandPriorityCategorizer: _commandPriorityCategorizer ) ); }); diff --git a/BotNet.CommandHandlers/AI/OpenAI/OpenAITextPromptHandler.cs b/BotNet.CommandHandlers/AI/OpenAI/OpenAITextPromptHandler.cs index a95e55b..ebe4527 100644 --- a/BotNet.CommandHandlers/AI/OpenAI/OpenAITextPromptHandler.cs +++ b/BotNet.CommandHandlers/AI/OpenAI/OpenAITextPromptHandler.cs @@ -4,6 +4,7 @@ using BotNet.Commands.AI.Stability; using BotNet.Commands.BotUpdate.Message; using BotNet.Commands.ChatAggregate; +using BotNet.Commands.CommandPrioritization; using BotNet.Commands.SenderAggregate; using BotNet.Services.MarkdownV2; using BotNet.Services.OpenAI; @@ -20,6 +21,7 @@ public sealed class OpenAITextPromptHandler( ICommandQueue commandQueue, ITelegramMessageCache telegramMessageCache, OpenAIClient openAIClient, + CommandPriorityCategorizer commandPriorityCategorizer, ILogger logger ) : ICommandHandler { internal static readonly RateLimiter CHAT_RATE_LIMITER = RateLimiter.PerUserPerChat(5, TimeSpan.FromMinutes(15)); @@ -28,6 +30,7 @@ ILogger logger private readonly ICommandQueue _commandQueue = commandQueue; private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache; private readonly OpenAIClient _openAIClient = openAIClient; + private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer; private readonly ILogger _logger = logger; public Task Handle(OpenAITextPrompt textPrompt, CancellationToken cancellationToken) { @@ -156,7 +159,8 @@ await _telegramBotClient.EditMessageTextAsync( message: AIResponseMessage.FromMessage( message: responseMessage, replyToMessage: textPrompt.Command, - callSign: textPrompt.CallSign + callSign: textPrompt.CallSign, + commandPriorityCategorizer: _commandPriorityCategorizer ) ); }); diff --git a/BotNet.CommandHandlers/AI/Stability/StabilityTextToImagePromptHandler.cs b/BotNet.CommandHandlers/AI/Stability/StabilityTextToImagePromptHandler.cs index 64fc171..6222aaf 100644 --- a/BotNet.CommandHandlers/AI/Stability/StabilityTextToImagePromptHandler.cs +++ b/BotNet.CommandHandlers/AI/Stability/StabilityTextToImagePromptHandler.cs @@ -1,6 +1,7 @@ using BotNet.Commands; using BotNet.Commands.AI.Stability; using BotNet.Commands.BotUpdate.Message; +using BotNet.Commands.CommandPrioritization; using BotNet.Services.Stability.Models; using BotNet.Services.Stability.Skills; using Telegram.Bot; @@ -11,11 +12,13 @@ namespace BotNet.CommandHandlers.AI.Stability { public sealed class StabilityTextToImagePromptHandler( ITelegramBotClient telegramBotClient, ImageGenerationBot imageGenerationBot, - ITelegramMessageCache telegramMessageCache + ITelegramMessageCache telegramMessageCache, + CommandPriorityCategorizer commandPriorityCategorizer ) : ICommandHandler { private readonly ITelegramBotClient _telegramBotClient = telegramBotClient; private readonly ImageGenerationBot _imageGenerationBot = imageGenerationBot; private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache; + private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer; public Task Handle(StabilityTextToImagePrompt command, CancellationToken cancellationToken) { // Fire and forget @@ -69,7 +72,7 @@ await _telegramBotClient.DeleteMessageAsync( // Track thread _telegramMessageCache.Add( - NormalMessage.FromMessage(responseMessage) + NormalMessage.FromMessage(responseMessage, _commandPriorityCategorizer) ); } catch (OperationCanceledException) { // Terminate gracefully diff --git a/BotNet.CommandHandlers/BotUpdate/Message/MessageUpdateHandler.cs b/BotNet.CommandHandlers/BotUpdate/Message/MessageUpdateHandler.cs index e837ab0..bd40e68 100644 --- a/BotNet.CommandHandlers/BotUpdate/Message/MessageUpdateHandler.cs +++ b/BotNet.CommandHandlers/BotUpdate/Message/MessageUpdateHandler.cs @@ -1,5 +1,6 @@ using BotNet.Commands; using BotNet.Commands.BotUpdate.Message; +using BotNet.Commands.CommandPrioritization; using BotNet.Services.BotProfile; using BotNet.Services.SocialLink; using RG.Ninja; @@ -11,12 +12,14 @@ public sealed class MessageUpdateHandler( ITelegramBotClient telegramBotClient, ICommandQueue commandQueue, ITelegramMessageCache telegramMessageCache, - BotProfileAccessor botProfileAccessor + BotProfileAccessor botProfileAccessor, + CommandPriorityCategorizer commandPriorityCategorizer ) : ICommandHandler { private readonly ITelegramBotClient _telegramBotClient = telegramBotClient; private readonly ICommandQueue _commandQueue = commandQueue; private readonly ITelegramMessageCache _telegramMessageCache = telegramMessageCache; private readonly BotProfileAccessor _botProfileAccessor = botProfileAccessor; + private readonly CommandPriorityCategorizer _commandPriorityCategorizer = commandPriorityCategorizer; public async Task Handle(MessageUpdate update, CancellationToken cancellationToken) { // Handle slash commands @@ -27,6 +30,7 @@ public async Task Handle(MessageUpdate update, CancellationToken cancellationTok if (SlashCommand.TryCreate( message: update.Message, botUsername: (await _botProfileAccessor.GetBotProfileAsync(cancellationToken)).Username!, + commandPriorityCategorizer: _commandPriorityCategorizer, out SlashCommand? slashCommand )) { await _commandQueue.DispatchAsync( @@ -108,6 +112,7 @@ await _telegramBotClient.SendTextMessageAsync( // Handle AI calls if (AICallCommand.TryCreate( message: update.Message!, + commandPriorityCategorizer: _commandPriorityCategorizer, out AICallCommand? aiCallCommand )) { // Cache both message and reply to message @@ -140,6 +145,7 @@ await _commandQueue.DispatchAsync( messageId: new(replyToMessageId), chatId: new(chatId) ), + commandPriorityCategorizer: _commandPriorityCategorizer, out AIFollowUpMessage? aiFollowUpMessage )) { return; diff --git a/BotNet.Commands/BotUpdate/Message/AICallCommand.cs b/BotNet.Commands/BotUpdate/Message/AICallCommand.cs index 7824f49..0f97d58 100644 --- a/BotNet.Commands/BotUpdate/Message/AICallCommand.cs +++ b/BotNet.Commands/BotUpdate/Message/AICallCommand.cs @@ -1,6 +1,7 @@ using System.Collections.Immutable; using System.Diagnostics.CodeAnalysis; using BotNet.Commands.ChatAggregate; +using BotNet.Commands.CommandPrioritization; using BotNet.Commands.SenderAggregate; namespace BotNet.Commands.BotUpdate.Message { @@ -36,17 +37,18 @@ string callSign public static bool TryCreate( Telegram.Bot.Types.Message message, + CommandPriorityCategorizer commandPriorityCategorizer, [NotNullWhen(true)] out AICallCommand? aiCallCommand ) { // Chat must be private or group - if (!ChatBase.TryCreate(message.Chat, out ChatBase? chat)) { + if (!ChatBase.TryCreate(message.Chat, commandPriorityCategorizer, out ChatBase? chat)) { aiCallCommand = null; return false; } // Sender must be a user if (message.From is not { } from - || !HumanSender.TryCreate(from, out HumanSender? sender)) { + || !HumanSender.TryCreate(from, commandPriorityCategorizer, out HumanSender? sender)) { aiCallCommand = null; return false; } @@ -72,7 +74,7 @@ public static bool TryCreate( ?? message.ReplyToMessage?.Sticker?.FileId, replyToMessage: message.ReplyToMessage is null ? null - : NormalMessage.FromMessage(message.ReplyToMessage), + : NormalMessage.FromMessage(message.ReplyToMessage, commandPriorityCategorizer), callSign: callSign ); return true; diff --git a/BotNet.Commands/BotUpdate/Message/AIFollowUpMessage.cs b/BotNet.Commands/BotUpdate/Message/AIFollowUpMessage.cs index 5f41566..01765e0 100644 --- a/BotNet.Commands/BotUpdate/Message/AIFollowUpMessage.cs +++ b/BotNet.Commands/BotUpdate/Message/AIFollowUpMessage.cs @@ -1,5 +1,6 @@ using System.Diagnostics.CodeAnalysis; using BotNet.Commands.ChatAggregate; +using BotNet.Commands.CommandPrioritization; using BotNet.Commands.SenderAggregate; namespace BotNet.Commands.BotUpdate.Message { @@ -26,17 +27,18 @@ AIResponseMessage replyToMessage public static bool TryCreate( Telegram.Bot.Types.Message message, IEnumerable thread, + CommandPriorityCategorizer commandPriorityCategorizer, [NotNullWhen(true)] out AIFollowUpMessage? aiFollowUpMessage ) { // Chat must be private or group - if (!ChatBase.TryCreate(message.Chat, out ChatBase? chat)) { + if (!ChatBase.TryCreate(message.Chat, commandPriorityCategorizer, out ChatBase? chat)) { aiFollowUpMessage = null; return false; } // Sender must be a user if (message.From is not { } from - || !HumanSender.TryCreate(from, out HumanSender? sender)) { + || !HumanSender.TryCreate(from, commandPriorityCategorizer, out HumanSender? sender)) { aiFollowUpMessage = null; return false; } diff --git a/BotNet.Commands/BotUpdate/Message/AIResponseMessage.cs b/BotNet.Commands/BotUpdate/Message/AIResponseMessage.cs index 606a20f..5c8bd8b 100644 --- a/BotNet.Commands/BotUpdate/Message/AIResponseMessage.cs +++ b/BotNet.Commands/BotUpdate/Message/AIResponseMessage.cs @@ -1,4 +1,5 @@ using BotNet.Commands.ChatAggregate; +using BotNet.Commands.CommandPrioritization; using BotNet.Commands.SenderAggregate; namespace BotNet.Commands.BotUpdate.Message { @@ -27,10 +28,11 @@ string callSign public static AIResponseMessage FromMessage( Telegram.Bot.Types.Message message, HumanMessageBase replyToMessage, - string callSign + string callSign, + CommandPriorityCategorizer commandPriorityCategorizer ) { // Chat must be private or group - if (!ChatBase.TryCreate(message.Chat, out ChatBase? chat)) { + if (!ChatBase.TryCreate(message.Chat, commandPriorityCategorizer, out ChatBase? chat)) { throw new ArgumentException("Chat must be private or group.", nameof(message)); } diff --git a/BotNet.Commands/BotUpdate/Message/NormalMessage.cs b/BotNet.Commands/BotUpdate/Message/NormalMessage.cs index 49beaf2..8f53bf1 100644 --- a/BotNet.Commands/BotUpdate/Message/NormalMessage.cs +++ b/BotNet.Commands/BotUpdate/Message/NormalMessage.cs @@ -1,4 +1,5 @@ using BotNet.Commands.ChatAggregate; +using BotNet.Commands.CommandPrioritization; using BotNet.Commands.SenderAggregate; namespace BotNet.Commands.BotUpdate.Message { @@ -19,9 +20,9 @@ private NormalMessage( replyToMessage: replyToMessage ) { } - public static NormalMessage FromMessage(Telegram.Bot.Types.Message message) { + public static NormalMessage FromMessage(Telegram.Bot.Types.Message message, CommandPriorityCategorizer commandPriorityCategorizer) { // Chat must be private or group - if (!ChatBase.TryCreate(message.Chat, out ChatBase? chat)) { + if (!ChatBase.TryCreate(message.Chat, commandPriorityCategorizer, out ChatBase? chat)) { throw new ArgumentException("Chat must be private or group.", nameof(message)); } @@ -33,7 +34,7 @@ public static NormalMessage FromMessage(Telegram.Bot.Types.Message message) { return new( messageId: new(message.MessageId), chat: chat, - sender: HumanSender.TryCreate(from, out HumanSender? humanSender) + sender: HumanSender.TryCreate(from, commandPriorityCategorizer, out HumanSender? humanSender) ? humanSender : BotSender.TryCreate(from, out BotSender? botSender) ? botSender @@ -42,7 +43,7 @@ public static NormalMessage FromMessage(Telegram.Bot.Types.Message message) { imageFileId: message.Photo?.LastOrDefault()?.FileId ?? message.Sticker?.FileId, replyToMessage: message.ReplyToMessage is null ? null - : NormalMessage.FromMessage(message.ReplyToMessage) + : NormalMessage.FromMessage(message.ReplyToMessage, commandPriorityCategorizer) ); } } diff --git a/BotNet.Commands/BotUpdate/Message/SlashCommand.cs b/BotNet.Commands/BotUpdate/Message/SlashCommand.cs index d43be36..08ea4f0 100644 --- a/BotNet.Commands/BotUpdate/Message/SlashCommand.cs +++ b/BotNet.Commands/BotUpdate/Message/SlashCommand.cs @@ -1,5 +1,6 @@ using System.Diagnostics.CodeAnalysis; using BotNet.Commands.ChatAggregate; +using BotNet.Commands.CommandPrioritization; using BotNet.Commands.SenderAggregate; using Telegram.Bot.Types.Enums; @@ -33,6 +34,7 @@ string command public static bool TryCreate( Telegram.Bot.Types.Message message, string botUsername, + CommandPriorityCategorizer commandPriorityCategorizer, [NotNullWhen(true)] out SlashCommand? slashCommand ) { // Message must start with a slash command @@ -46,14 +48,14 @@ public static bool TryCreate( } // Chat must be private or group - if (!ChatBase.TryCreate(message.Chat, out ChatBase? chat)) { + if (!ChatBase.TryCreate(message.Chat, commandPriorityCategorizer, out ChatBase? chat)) { slashCommand = null; return false; } // Sender must be a user if (message.From is not { } from - || !HumanSender.TryCreate(from, out HumanSender? sender)) { + || !HumanSender.TryCreate(from, commandPriorityCategorizer, out HumanSender? sender)) { slashCommand = null; return false; } @@ -88,7 +90,7 @@ public static bool TryCreate( imageFileId: message.Photo?.LastOrDefault()?.FileId, replyToMessage: message.ReplyToMessage is null ? null - : NormalMessage.FromMessage(message.ReplyToMessage), + : NormalMessage.FromMessage(message.ReplyToMessage, commandPriorityCategorizer), command: commandText ); return true; diff --git a/BotNet.Commands/ChatAggregate/Chat.cs b/BotNet.Commands/ChatAggregate/Chat.cs index 0f2bfe6..b90e259 100644 --- a/BotNet.Commands/ChatAggregate/Chat.cs +++ b/BotNet.Commands/ChatAggregate/Chat.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using BotNet.Commands.CommandPrioritization; using Telegram.Bot.Types.Enums; namespace BotNet.Commands.ChatAggregate { @@ -16,11 +17,14 @@ protected ChatBase( public static bool TryCreate( Telegram.Bot.Types.Chat telegramChat, + CommandPriorityCategorizer priorityCategorizer, [NotNullWhen(true)] out ChatBase? chat ) { chat = telegramChat switch { Telegram.Bot.Types.Chat { Type: ChatType.Private } => PrivateChat.FromTelegramChat(telegramChat), - Telegram.Bot.Types.Chat { Type: ChatType.Group or ChatType.Supergroup } => GroupChat.FromTelegramChat(telegramChat), + Telegram.Bot.Types.Chat { Type: ChatType.Group or ChatType.Supergroup } => priorityCategorizer.IsHomeGroup(telegramChat.Id) + ? HomeGroupChat.FromTelegramChat(telegramChat) + : GroupChat.FromTelegramChat(telegramChat), _ => null }; return chat is not null; diff --git a/BotNet.Commands/CommandPrioritization/CommandPriority.cs b/BotNet.Commands/CommandPrioritization/CommandPriority.cs deleted file mode 100644 index c88c85d..0000000 --- a/BotNet.Commands/CommandPrioritization/CommandPriority.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace BotNet.Commands.CommandPrioritization { - public enum CommandPriority { - Void, - PrivateChat, - GroupChat, - HomeGroupChat, - VIPChat - } -} diff --git a/BotNet.Commands/CommandPrioritization/CommandPriorityCategorizer.cs b/BotNet.Commands/CommandPrioritization/CommandPriorityCategorizer.cs index eb97e1d..d1f6c3e 100644 --- a/BotNet.Commands/CommandPrioritization/CommandPriorityCategorizer.cs +++ b/BotNet.Commands/CommandPrioritization/CommandPriorityCategorizer.cs @@ -1,7 +1,5 @@ using System.Collections.Immutable; using Microsoft.Extensions.Options; -using Telegram.Bot.Types; -using Telegram.Bot.Types.Enums; namespace BotNet.Commands.CommandPrioritization { public sealed class CommandPriorityCategorizer { @@ -28,37 +26,12 @@ IOptions optionsAccessor .ToImmutableHashSet(); } - public CommandPriority Categorize(Message message) { - if (message.From is null) { - return CommandPriority.Void; - } - - if (message.From.IsBot) { - return CommandPriority.Void; - } - - if (_vipUserIds.Contains(message.From.Id)) { - return CommandPriority.VIPChat; - } - - if (_homeGroupChatIds.Contains(message.Chat.Id)) { - return CommandPriority.HomeGroupChat; - } - - switch (message.Chat.Type) { - case ChatType.Private: - return CommandPriority.PrivateChat; - case ChatType.Group: - case ChatType.Channel: - case ChatType.Supergroup: - return CommandPriority.GroupChat; - default: - return CommandPriority.Void; - } - } - public bool IsHomeGroup(long ChatId) { return _homeGroupChatIds.Contains(ChatId); } + + public bool IsVIPUser(long UserId) { + return _vipUserIds.Contains(UserId); + } } } diff --git a/BotNet.Commands/SenderAggregate/Sender.cs b/BotNet.Commands/SenderAggregate/Sender.cs index 5495f7e..b6a9a83 100644 --- a/BotNet.Commands/SenderAggregate/Sender.cs +++ b/BotNet.Commands/SenderAggregate/Sender.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using BotNet.Commands.CommandPrioritization; namespace BotNet.Commands.SenderAggregate { public abstract record SenderBase( @@ -16,23 +17,32 @@ string Name public static bool TryCreate( Telegram.Bot.Types.User user, + CommandPriorityCategorizer commandPriorityCategorizer, [NotNullWhen(true)] out HumanSender? humanSender ) { - if (user is { + if (user is not { IsBot: false, Id: long senderId, FirstName: string senderFirstName, LastName: var senderLastName }) { - humanSender = new HumanSender( + humanSender = null; + return false; + } + + if (commandPriorityCategorizer.IsVIPUser(senderId)) { + humanSender = new VIPSender( Id: senderId, Name: senderLastName is { } ? $"{senderFirstName} {senderLastName}" : senderFirstName ); return true; } - humanSender = null; - return false; + humanSender = new HumanSender( + Id: senderId, + Name: senderLastName is { } ? $"{senderFirstName} {senderLastName}" : senderFirstName + ); + return true; } }