Unavailable store item message (#31)

This commit is contained in:
2023-05-17 23:12:15 +03:00
committed by GitHub
parent aef13b1883
commit d63113f58c
+40 -23
View File
@@ -106,38 +106,55 @@ public class Store : BotEventHandler
return; return;
} }
try
{
await PerformPurchase(mediaService, messageId);
}
catch (Exception e)
{
answer = "Товар недоступен";
await Bot.EditMessageTextAsync(Chat.Id,
messageId,
$"<b>{From.ToHtml()} попытался купить \"{storeItem.Name}\", но товара в наличии не оказалось.");
await AnswerQuery(query.Id, answer);
return;
}
reputation = await _reputation.SetUserReputationAsync(Chat, From, reputation - storeItem.Price); reputation = await _reputation.SetUserReputationAsync(Chat, From, reputation - storeItem.Price);
await Bot.EditMessageTextAsync(Chat.Id, await Bot.EditMessageTextAsync(Chat.Id,
messageId, messageId,
$"<b>{From.ToHtml()} ({reputation})</b> купил \"{storeItem.Name}\".", $"<b>{From.ToHtml()} ({reputation})</b> купил \"{storeItem.Name}\".",
ParseMode.Html); ParseMode.Html);
var randomMedia = await mediaService!.GetRandomMediaAsync();
var file = randomMedia.File;
switch (randomMedia.Type)
{
case InputMediaType.Photo:
await Bot.SendPhotoAsync(Chat.Id, file,
caption: randomMedia.Caption,
replyToMessageId: messageId,
parseMode: ParseMode.Html,
hasSpoiler: randomMedia.Nsfw
);
break;
case InputMediaType.Video:
await Bot.SendVideoAsync(Chat.Id, file,
caption: randomMedia.Caption,
replyToMessageId: messageId,
parseMode: ParseMode.Html,
hasSpoiler: randomMedia.Nsfw
);
break;
}
} }
await AnswerQuery(query.Id, answer); await AnswerQuery(query.Id, answer);
} }
private async Task PerformPurchase(IRandomMediaService mediaService, int messageId)
{
var randomMedia = await mediaService!.GetRandomMediaAsync();
var file = randomMedia.File;
switch (randomMedia.Type)
{
case InputMediaType.Photo:
await Bot.SendPhotoAsync(Chat.Id, file,
caption: randomMedia.Caption,
replyToMessageId: messageId,
parseMode: ParseMode.Html,
hasSpoiler: randomMedia.Nsfw
);
break;
case InputMediaType.Video:
await Bot.SendVideoAsync(Chat.Id, file,
caption: randomMedia.Caption,
replyToMessageId: messageId,
parseMode: ParseMode.Html,
hasSpoiler: randomMedia.Nsfw
);
break;
}
}
private bool TryGetMediaService(string name, out IRandomMediaService? mediaService) private bool TryGetMediaService(string name, out IRandomMediaService? mediaService)
{ {