Dernière activité 1 month ago

Révision c0a47106d77c628f5a683a675475eea2c4a8e2ce

.js Brut
1bot.callbackQuery(/^credit_confirm_(\d+)_(\d+)$/, async (ctx) => {
2 const chatInstance = ctx.update.callback_query.chat_instance;
3 const amount = parseInt(ctx.match[1], 10);
4 const targetUserId = parseInt(ctx.match[2], 10);
5 const userId = ctx.from.id;
6 const firstName = ctx.from.first_name;
7
8 if (userId !== targetUserId) {
9 return ctx.answerCallbackQuery({
10 text: "Это не твой кредит! Создай свой запрос.",
11 show_alert: true
12 });
13 }
14
15 if (!chatInstance) return ctx.answerCallbackQuery({ text: "Error: No chat instance." });
16
17 const db = getDb();
18 const user = getChatUser(db, chatInstance, userId, firstName);
19
20 if (!user.credit) user.credit = 0;
21 if (!user.credit_taken_today) user.credit_taken_today = 0;
22
23 const info = getCreditInfo(user);
24
25 if (info.taken + amount > 50) {
26 saveDb(db);
27 return ctx.answerCallbackQuery({
28 text: `Лимит превышен! Ты уже взял ${info.taken} см сегодня. Остаток: ${50 - info.taken} см.`,
29 show_alert: true
30 });
31 }
32
33 user.size += amount;
34 user.credit += amount;
35 user.credit_taken_today = (user.credit_taken_today || 0) + amount;
36 user.credit_last_reset = user.credit_last_reset || Date.now();
37
38 saveDb(db);
39
40 try {
41 await ctx.editMessageText(
42 `🏦 *${user.name}* взял кредит!\n` +
43 `💰 Сумма: *${amount} см*\n` +
44 `📉 Долг: *${user.credit} см*\n` +
45 `📏 Размер сейчас: *${user.size} см*\n` +
46 `⚠️ _Вернуть придется с процентами!_`,
47 { parse_mode: "Markdown" }
48 );
49 } catch (e) {
50 console.error(e);
51 }
52
53 await ctx.answerCallbackQuery({ text: `Кредит ${amount} см выдан!` });
54});