chore: cleanup

This commit is contained in:
Pavel-Savely Savianok 2025-07-12 02:02:03 +03:00
parent a526dbf6c6
commit 6e16c5831e
7 changed files with 19 additions and 16 deletions

View File

@ -12,9 +12,6 @@ public class EulaAgreementButtonCommand(PrepareUserState prepareUserState) : IAu
{ {
public async Task ExecuteAsync(Update update, GetUserResponse user, CancellationToken ct) 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); await prepareUserState.AcceptEula(update, ct);
} }
} }

View File

@ -1,7 +1,7 @@
using Telegram.Bot; using Telegram.Bot;
using Telegram.Bot.Types; using Telegram.Bot.Types;
namespace JOBot.TClient.Commands; namespace JOBot.TClient.Commands.Commands;
public class InfoCommand(ITelegramBotClient bot) : ITelegramCommand public class InfoCommand(ITelegramBotClient bot) : ITelegramCommand
{ {

View File

@ -2,7 +2,7 @@ using JOBot.Proto;
using JOBot.TClient.Services; using JOBot.TClient.Services;
using Telegram.Bot.Types; using Telegram.Bot.Types;
namespace JOBot.TClient.Commands; namespace JOBot.TClient.Commands.Commands;
public class MenuCommand(MenuService menuService) : IAuthorizedTelegramCommand public class MenuCommand(MenuService menuService) : IAuthorizedTelegramCommand
{ {

View File

@ -1,9 +1,7 @@
using JOBot.TClient.Commands.Buttons;
using JOBot.TClient.Statements; using JOBot.TClient.Statements;
using Telegram.Bot.Types; using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace JOBot.TClient.Commands; namespace JOBot.TClient.Commands.Commands;
public class StartCommand(PrepareUserState prepareUserState) : ITelegramCommand public class StartCommand(PrepareUserState prepareUserState) : ITelegramCommand
{ {

View File

@ -8,6 +8,8 @@ public interface IAuthorizedTelegramCommand : ITelegramCommand
{ {
public Task ExecuteAsync(Update update, GetUserResponse user, CancellationToken ct); public Task ExecuteAsync(Update update, GetUserResponse user, CancellationToken ct);
/// <exception cref="UnauthorizedAccessException">Throws if you try to use ITelegramCommand.ExecuteAsync
/// instead of IAuthorizedTelegramCommand.ExecuteAsync</exception>
Task ITelegramCommand.ExecuteAsync(Update update, CancellationToken ct) Task ITelegramCommand.ExecuteAsync(Update update, CancellationToken ct)
{ {
throw new UnauthorizedAccessException("You do not have permission to access this command."); throw new UnauthorizedAccessException("You do not have permission to access this command.");

View File

@ -1,5 +1,6 @@
using JOBot.TClient.Commands; using JOBot.TClient.Commands;
using JOBot.TClient.Commands.Buttons; using JOBot.TClient.Commands.Buttons;
using JOBot.TClient.Commands.Commands;
using JOBot.TClient.Infrastructure.Attributes.Authorization; using JOBot.TClient.Infrastructure.Attributes.Authorization;
using JOBot.TClient.Infrastructure.Extensions; using JOBot.TClient.Infrastructure.Extensions;
using JOBot.TClient.Services; using JOBot.TClient.Services;
@ -37,15 +38,15 @@ public sealed class BotBackgroundService(
["/start"] = scope.ServiceProvider.GetRequiredService<StartCommand>(), ["/start"] = scope.ServiceProvider.GetRequiredService<StartCommand>(),
["/menu"] = scope.ServiceProvider.GetRequiredService<MenuCommand>(), ["/menu"] = scope.ServiceProvider.GetRequiredService<MenuCommand>(),
["/info"] = scope.ServiceProvider.GetRequiredService<InfoCommand>(), ["/info"] = scope.ServiceProvider.GetRequiredService<InfoCommand>(),
//Buttons //Buttons
[ButtonResource.EULAAgrement] = scope.ServiceProvider.GetRequiredService<EulaAgreementButtonCommand>(), [ButtonResource.EULAAgrement] = scope.ServiceProvider.GetRequiredService<EulaAgreementButtonCommand>(),
}; };
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) if (user == null)
{ {
await commands["/start"].ExecuteAsync(update, ct); await commands["/start"].ExecuteAsync(update, ct);
@ -56,18 +57,23 @@ public sealed class BotBackgroundService(
{ {
if (command is IAuthorizedTelegramCommand authorizedTelegramCommand) 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) if (!user.IsPrepared() && attribute is not AcceptNotPreparedAttribute)
{ {
await commands["/start"].ExecuteAsync(update, ct); //заставляем пользователя завершить регистрацию await commands["/start"].ExecuteAsync(update, ct);
//заставляем пользователя завершить регистрацию
return; return;
} }
await authorizedTelegramCommand.ExecuteAsync(update, user, ct); await authorizedTelegramCommand.ExecuteAsync(update, user, ct);
} }
else await command.ExecuteAsync(update, ct); else await command.ExecuteAsync(update, ct);
return; return;
} }
await bot.SendMessage(update.Message.From.Id, TextResource.CommandNotFound, cancellationToken: ct); await bot.SendMessage(update.Message.From.Id, TextResource.CommandNotFound, cancellationToken: ct);
} }
} }

View File

@ -1,8 +1,8 @@
using Grpc.Core; using Grpc.Core;
using Grpc.Net.Client; using Grpc.Net.Client;
using JOBot.Proto; using JOBot.Proto;
using JOBot.TClient.Commands;
using JOBot.TClient.Commands.Buttons; using JOBot.TClient.Commands.Buttons;
using JOBot.TClient.Commands.Commands;
using JOBot.TClient.Core.HostedServices; using JOBot.TClient.Core.HostedServices;
using JOBot.TClient.Services; using JOBot.TClient.Services;
using JOBot.TClient.Statements; using JOBot.TClient.Statements;