diff --git a/JOBot.TClient/Commands/Buttons/EulaAgreementButtonCommand.cs b/JOBot.TClient/Commands/Buttons/EulaAgreementButtonCommand.cs index 4f1c6d3..87d6368 100644 --- a/JOBot.TClient/Commands/Buttons/EulaAgreementButtonCommand.cs +++ b/JOBot.TClient/Commands/Buttons/EulaAgreementButtonCommand.cs @@ -12,9 +12,6 @@ public class EulaAgreementButtonCommand(PrepareUserState prepareUserState) : IAu { public async Task ExecuteAsync(Update update, GetUserResponse user, CancellationToken ct) { - if (update.Type != UpdateType.Message || update.Message?.From == null) - return; - await prepareUserState.AcceptEula(update, ct); } } \ No newline at end of file diff --git a/JOBot.TClient/Commands/InfoCommand.cs b/JOBot.TClient/Commands/Commands/InfoCommand.cs similarity index 89% rename from JOBot.TClient/Commands/InfoCommand.cs rename to JOBot.TClient/Commands/Commands/InfoCommand.cs index 084fb2a..45bb655 100644 --- a/JOBot.TClient/Commands/InfoCommand.cs +++ b/JOBot.TClient/Commands/Commands/InfoCommand.cs @@ -1,7 +1,7 @@ using Telegram.Bot; using Telegram.Bot.Types; -namespace JOBot.TClient.Commands; +namespace JOBot.TClient.Commands.Commands; public class InfoCommand(ITelegramBotClient bot) : ITelegramCommand { diff --git a/JOBot.TClient/Commands/MenuCommand.cs b/JOBot.TClient/Commands/Commands/MenuCommand.cs similarity index 87% rename from JOBot.TClient/Commands/MenuCommand.cs rename to JOBot.TClient/Commands/Commands/MenuCommand.cs index b5b7393..acce938 100644 --- a/JOBot.TClient/Commands/MenuCommand.cs +++ b/JOBot.TClient/Commands/Commands/MenuCommand.cs @@ -2,7 +2,7 @@ using JOBot.Proto; using JOBot.TClient.Services; using Telegram.Bot.Types; -namespace JOBot.TClient.Commands; +namespace JOBot.TClient.Commands.Commands; public class MenuCommand(MenuService menuService) : IAuthorizedTelegramCommand { diff --git a/JOBot.TClient/Commands/StartCommand.cs b/JOBot.TClient/Commands/Commands/StartCommand.cs similarity index 73% rename from JOBot.TClient/Commands/StartCommand.cs rename to JOBot.TClient/Commands/Commands/StartCommand.cs index b2f5bb4..e8c11f8 100644 --- a/JOBot.TClient/Commands/StartCommand.cs +++ b/JOBot.TClient/Commands/Commands/StartCommand.cs @@ -1,9 +1,7 @@ -using JOBot.TClient.Commands.Buttons; using JOBot.TClient.Statements; using Telegram.Bot.Types; -using Telegram.Bot.Types.Enums; -namespace JOBot.TClient.Commands; +namespace JOBot.TClient.Commands.Commands; public class StartCommand(PrepareUserState prepareUserState) : ITelegramCommand { diff --git a/JOBot.TClient/Commands/IAuthorizedTelegramCommand.cs b/JOBot.TClient/Commands/IAuthorizedTelegramCommand.cs index 4d37881..7d0d035 100644 --- a/JOBot.TClient/Commands/IAuthorizedTelegramCommand.cs +++ b/JOBot.TClient/Commands/IAuthorizedTelegramCommand.cs @@ -8,6 +8,8 @@ public interface IAuthorizedTelegramCommand : ITelegramCommand { public Task ExecuteAsync(Update update, GetUserResponse user, CancellationToken ct); + /// Throws if you try to use ITelegramCommand.ExecuteAsync + /// instead of IAuthorizedTelegramCommand.ExecuteAsync Task ITelegramCommand.ExecuteAsync(Update update, CancellationToken ct) { throw new UnauthorizedAccessException("You do not have permission to access this command."); diff --git a/JOBot.TClient/Core/HostedServices/BotBackgroundService.cs b/JOBot.TClient/Core/HostedServices/BotBackgroundService.cs index 82264e8..87e63ff 100644 --- a/JOBot.TClient/Core/HostedServices/BotBackgroundService.cs +++ b/JOBot.TClient/Core/HostedServices/BotBackgroundService.cs @@ -1,5 +1,6 @@ using JOBot.TClient.Commands; using JOBot.TClient.Commands.Buttons; +using JOBot.TClient.Commands.Commands; using JOBot.TClient.Infrastructure.Attributes.Authorization; using JOBot.TClient.Infrastructure.Extensions; using JOBot.TClient.Services; @@ -37,15 +38,15 @@ public sealed class BotBackgroundService( ["/start"] = scope.ServiceProvider.GetRequiredService(), ["/menu"] = scope.ServiceProvider.GetRequiredService(), ["/info"] = scope.ServiceProvider.GetRequiredService(), - + //Buttons [ButtonResource.EULAAgrement] = scope.ServiceProvider.GetRequiredService(), }; - if (update.Message?.Text is { } text && update.Message?.From != null) + if (update.Message is { Text: { } text, From: not null }) { - var user = await userService.GetUser(update, ct); //Check user for existance - + var user = await userService.GetUser(update, ct); //Проверка существования пользователя + if (user == null) { await commands["/start"].ExecuteAsync(update, ct); @@ -56,18 +57,23 @@ public sealed class BotBackgroundService( { if (command is IAuthorizedTelegramCommand authorizedTelegramCommand) { - var attribute = Attribute.GetCustomAttribute(command.GetType(), typeof(AcceptNotPreparedAttribute)); + var attribute = Attribute.GetCustomAttribute( + command.GetType(), + typeof(AcceptNotPreparedAttribute)); if (!user.IsPrepared() && attribute is not AcceptNotPreparedAttribute) { - await commands["/start"].ExecuteAsync(update, ct); //заставляем пользователя завершить регистрацию + await commands["/start"].ExecuteAsync(update, ct); + //заставляем пользователя завершить регистрацию return; } + await authorizedTelegramCommand.ExecuteAsync(update, user, ct); } else await command.ExecuteAsync(update, ct); + return; } - + await bot.SendMessage(update.Message.From.Id, TextResource.CommandNotFound, cancellationToken: ct); } } diff --git a/JOBot.TClient/DependencyInjection.cs b/JOBot.TClient/DependencyInjection.cs index 71a7e93..153be1b 100644 --- a/JOBot.TClient/DependencyInjection.cs +++ b/JOBot.TClient/DependencyInjection.cs @@ -1,8 +1,8 @@ using Grpc.Core; using Grpc.Net.Client; using JOBot.Proto; -using JOBot.TClient.Commands; using JOBot.TClient.Commands.Buttons; +using JOBot.TClient.Commands.Commands; using JOBot.TClient.Core.HostedServices; using JOBot.TClient.Services; using JOBot.TClient.Statements;