fixed modern style for ensuring props are not null and extracting values.

This commit is contained in:
Ruberoid 2025-07-21 14:59:21 +03:00
parent 66910ae6fe
commit 65af3ded91
2 changed files with 13 additions and 22 deletions

View File

@ -28,24 +28,12 @@ public abstract class StartHandlerBase
public async Task Handle(Update update, CancellationToken cancellationToken = default)
{
if (update.Message?.From is null || update.CallbackQuery?.Message is null)
long telegramId = update.Type switch
{
_logger.LogError("Не удалось выделить пользовательский колбэк.\n{@Update}", update);
return;
}
long telegramId;
switch (update.Type)
{
case UpdateType.Message:
telegramId = update.Message.From.Id;
break;
case UpdateType.CallbackQuery:
telegramId = update.CallbackQuery.From.Id;
break;
default:
throw new ArgumentOutOfRangeException(nameof(update.Type), "Unsupported update type");
}
UpdateType.Message when update.Message?.From is { } from => from.Id,
UpdateType.CallbackQuery when update.CallbackQuery?.From is { } from => from.Id,
_ => throw new ArgumentOutOfRangeException(nameof(update.Type), "Unsupported update type or missing From.")
};
var message = new TextMessage(telegramId)
{
@ -54,16 +42,18 @@ public abstract class StartHandlerBase
ReplyMarkup = GetKeyboard()
};
if (update.Type == UpdateType.CallbackQuery)
if (update is { Type: UpdateType.CallbackQuery, CallbackQuery.Message: { } m })
{
var messageId = update.CallbackQuery.Message.MessageId;
if (update.CallbackQuery.Message.Type == MessageType.Text)
var messageId = m.MessageId;
if (m.Type == MessageType.Text)
{
await Bot.EditMessageTextAsync(messageId, message, cancellationToken);
return;
}
var chatId = update.CallbackQuery.Message.Chat.Id;
var chatId = m.Chat.Id;
try
{
await Bot.DeleteMessageAsync(chatId, messageId, cancellationToken);

View File

@ -28,7 +28,8 @@ public abstract class SubscribeHandlerBase : ViewSubscriptionHandlerBase
{
if (update.Message?.Text is null || update.Message?.From is null)
{
_logger.LogError("Не удалось выделить пользовательское сообщение.\n{@Update}", update);
_logger.LogError("Cannot parse update message or from/text user unknown. {@Update}", update.Id);
_logger.LogDebug(update.ToString());
return;
}