.js
· 1.9 KiB · JavaScript
Raw
bot.callbackQuery(/^credit_confirm_(\d+)_(\d+)$/, async (ctx) => {
const chatInstance = ctx.update.callback_query.chat_instance;
const amount = parseInt(ctx.match[1], 10);
const targetUserId = parseInt(ctx.match[2], 10);
const userId = ctx.from.id;
const firstName = ctx.from.first_name;
if (userId !== targetUserId) {
return ctx.answerCallbackQuery({
text: "Это не твой кредит! Создай свой запрос.",
show_alert: true
});
}
if (!chatInstance) return ctx.answerCallbackQuery({ text: "Error: No chat instance." });
const db = getDb();
const user = getChatUser(db, chatInstance, userId, firstName);
if (!user.credit) user.credit = 0;
if (!user.credit_taken_today) user.credit_taken_today = 0;
const info = getCreditInfo(user);
if (info.taken + amount > 50) {
saveDb(db);
return ctx.answerCallbackQuery({
text: `Лимит превышен! Ты уже взял ${info.taken} см сегодня. Остаток: ${50 - info.taken} см.`,
show_alert: true
});
}
user.size += amount;
user.credit += amount;
user.credit_taken_today = (user.credit_taken_today || 0) + amount;
user.credit_last_reset = user.credit_last_reset || Date.now();
saveDb(db);
try {
await ctx.editMessageText(
`🏦 *${user.name}* взял кредит!\n` +
`💰 Сумма: *${amount} см*\n` +
`📉 Долг: *${user.credit} см*\n` +
`📏 Размер сейчас: *${user.size} см*\n` +
`⚠️ _Вернуть придется с процентами!_`,
{ parse_mode: "Markdown" }
);
} catch (e) {
console.error(e);
}
await ctx.answerCallbackQuery({ text: `Кредит ${amount} см выдан!` });
});
| 1 | bot.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 | }); |