460 lines
16 KiB
Python
460 lines
16 KiB
Python
from __future__ import annotations
|
|
|
|
from aiogram import F, Router
|
|
from aiogram.filters import Command
|
|
from aiogram.fsm.context import FSMContext
|
|
from aiogram.types import (
|
|
CallbackQuery,
|
|
FSInputFile,
|
|
InlineKeyboardButton,
|
|
InlineKeyboardMarkup,
|
|
Message,
|
|
)
|
|
|
|
from config import ADMIN_ID, ROBSEC_FILE
|
|
from db.storage import load_config
|
|
from db.users import get
|
|
from keyboards.admin import admin_menu_kb
|
|
from keyboards.user import back_kb
|
|
from services.admin import (
|
|
add_balance,
|
|
add_treasury,
|
|
ban_user,
|
|
broadcast,
|
|
clear_robsec,
|
|
find_user,
|
|
is_admin,
|
|
robsec_info,
|
|
sub_balance,
|
|
toggle_bot,
|
|
toggle_shop,
|
|
toggle_user_admin,
|
|
update_min_withdraw,
|
|
update_rate,
|
|
)
|
|
from services.withdrawals import check_treasury_invoice, create_treasury_invoice
|
|
from states.admin import Form
|
|
from runtime import get_bot
|
|
from utils.logging import safe_edit
|
|
|
|
router = Router(name="admin")
|
|
|
|
|
|
def _admin(user_id: int) -> bool:
|
|
return user_id == ADMIN_ID or is_admin(user_id)
|
|
|
|
|
|
def _user_card(uid: int) -> tuple[str, InlineKeyboardMarkup] | None:
|
|
profile = get(uid)
|
|
if not profile:
|
|
return None
|
|
text = (
|
|
"<b>User</b>\n\n"
|
|
f"ID: <code>{uid}</code>\n"
|
|
f"Username: @{profile.username or '-'}\n"
|
|
f"Registered: {profile.registered or '-'}\n"
|
|
f"Balance: {profile.balance:.2f} RUB\n"
|
|
f"Total earned: {profile.total_earned:.2f} RUB\n"
|
|
f"Cookies: {profile.cookies_loaded}\n"
|
|
f"Referrals: {len(profile.referrals)}\n"
|
|
f"Admin: {'yes' if profile.is_admin else 'no'}\n"
|
|
f"Banned: {'yes: ' + profile.ban_reason if profile.is_banned else 'no'}"
|
|
)
|
|
keyboard = InlineKeyboardMarkup(
|
|
inline_keyboard=[
|
|
[
|
|
InlineKeyboardButton(text="Add balance", callback_data=f"u_bal_add_{uid}"),
|
|
InlineKeyboardButton(text="Subtract balance", callback_data=f"u_bal_sub_{uid}"),
|
|
],
|
|
[InlineKeyboardButton(
|
|
text="Unban" if profile.is_banned else "Ban",
|
|
callback_data=f"u_ban_{uid}",
|
|
)],
|
|
[InlineKeyboardButton(
|
|
text="Remove admin" if profile.is_admin else "Grant admin",
|
|
callback_data=f"u_admin_{uid}",
|
|
)],
|
|
[InlineKeyboardButton(text="Back", callback_data="admin_back")],
|
|
]
|
|
)
|
|
return text, keyboard
|
|
|
|
|
|
@router.message(Command("admin"))
|
|
async def cmd_admin(message: Message, state: FSMContext):
|
|
if not _admin(message.from_user.id):
|
|
return
|
|
await state.clear()
|
|
await message.answer("<b>Admin panel</b>", parse_mode="HTML", reply_markup=admin_menu_kb())
|
|
|
|
|
|
@router.callback_query(F.data == "admin_close")
|
|
async def admin_close(callback: CallbackQuery, state: FSMContext):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
await state.clear()
|
|
try:
|
|
await callback.message.delete()
|
|
except Exception:
|
|
pass
|
|
await callback.answer()
|
|
|
|
|
|
@router.callback_query(F.data == "admin_back")
|
|
async def admin_back(callback: CallbackQuery, state: FSMContext):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
await state.clear()
|
|
await safe_edit(callback, "<b>Admin panel</b>", reply_markup=admin_menu_kb())
|
|
await callback.answer()
|
|
|
|
|
|
@router.callback_query(F.data == "admin_toggle_shop")
|
|
async def admin_toggle_shop(callback: CallbackQuery):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
enabled = toggle_shop()
|
|
await callback.message.edit_reply_markup(reply_markup=admin_menu_kb())
|
|
await callback.answer(f"Shop: {'ON' if enabled else 'OFF'}")
|
|
|
|
|
|
@router.callback_query(F.data == "admin_toggle_bot")
|
|
async def admin_toggle_bot(callback: CallbackQuery):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
enabled = toggle_bot()
|
|
await callback.message.edit_reply_markup(reply_markup=admin_menu_kb())
|
|
await callback.answer(f"Bot: {'ON' if enabled else 'OFF'}")
|
|
|
|
|
|
@router.callback_query(F.data == "admin_robsec_get")
|
|
async def admin_robsec_get(callback: CallbackQuery):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
lines, size = robsec_info()
|
|
if not size:
|
|
await callback.answer("robsec.txt is empty", show_alert=True)
|
|
return
|
|
try:
|
|
bot = get_bot() or callback.message.bot
|
|
await bot.send_document(
|
|
callback.from_user.id,
|
|
FSInputFile(ROBSEC_FILE),
|
|
caption=f"robsec.txt\nLines: {lines}\nSize: {size} bytes",
|
|
)
|
|
await callback.answer("Sent")
|
|
except Exception as exc:
|
|
await callback.answer(f"Error: {exc}", show_alert=True)
|
|
|
|
|
|
@router.callback_query(F.data == "admin_robsec_clear")
|
|
async def admin_robsec_clear_prompt(callback: CallbackQuery):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
lines, _ = robsec_info()
|
|
keyboard = InlineKeyboardMarkup(
|
|
inline_keyboard=[
|
|
[InlineKeyboardButton(text="Yes, clear", callback_data="admin_robsec_clear_yes")],
|
|
[InlineKeyboardButton(text="Cancel", callback_data="admin_back")],
|
|
]
|
|
)
|
|
await safe_edit(callback, f"Clear robsec.txt? Current lines: <b>{lines}</b>", reply_markup=keyboard)
|
|
|
|
|
|
@router.callback_query(F.data == "admin_robsec_clear_yes")
|
|
async def admin_robsec_clear_yes(callback: CallbackQuery):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
clear_robsec()
|
|
await safe_edit(callback, "<b>robsec.txt cleared</b>", reply_markup=admin_menu_kb())
|
|
await callback.answer("Cleared")
|
|
|
|
|
|
@router.callback_query(F.data == "admin_rates")
|
|
async def admin_rates(callback: CallbackQuery):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
cfg = load_config()
|
|
rows = [
|
|
[InlineKeyboardButton(text=f"{rate.name}: {rate.price:.2f} RUB", callback_data=f"admin_rate_{key}")]
|
|
for key, rate in cfg.rates.items()
|
|
]
|
|
rows.append([InlineKeyboardButton(text="Back", callback_data="admin_back")])
|
|
await safe_edit(
|
|
callback,
|
|
"<b>Rate management</b>\nSelect a rate:",
|
|
reply_markup=InlineKeyboardMarkup(inline_keyboard=rows),
|
|
)
|
|
await callback.answer()
|
|
|
|
|
|
@router.callback_query(F.data.startswith("admin_rate_"))
|
|
async def admin_rate_select(callback: CallbackQuery, state: FSMContext):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
key = callback.data.removeprefix("admin_rate_")
|
|
cfg = load_config()
|
|
rate = cfg.rates.get(key)
|
|
if not rate:
|
|
await callback.answer("Rate not found", show_alert=True)
|
|
return
|
|
await state.update_data(rate_key=key)
|
|
await state.set_state(Form.admin_edit_rate)
|
|
await safe_edit(
|
|
callback,
|
|
f"Rate: <b>{rate.name}</b>\nCurrent price: {rate.price:.2f} RUB\n\nSend a new price:",
|
|
reply_markup=back_kb("admin_back"),
|
|
)
|
|
|
|
|
|
@router.message(Form.admin_edit_rate)
|
|
async def admin_edit_rate_save(message: Message, state: FSMContext):
|
|
if not _admin(message.from_user.id):
|
|
return
|
|
try:
|
|
price = float((message.text or "").replace(",", "."))
|
|
if price < 0:
|
|
raise ValueError
|
|
except ValueError:
|
|
await message.answer("Send a non-negative number.")
|
|
return
|
|
data = await state.get_data()
|
|
key = data.get("rate_key")
|
|
cfg = load_config()
|
|
if key not in cfg.rates:
|
|
await state.clear()
|
|
return
|
|
update_rate(key, price)
|
|
await state.clear()
|
|
await message.answer(f"Rate updated: {cfg.rates[key].name} = {price:.2f} RUB", reply_markup=admin_menu_kb())
|
|
|
|
|
|
@router.callback_query(F.data == "admin_min_withdraw")
|
|
async def admin_min_withdraw(callback: CallbackQuery, state: FSMContext):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
await state.set_state(Form.admin_min_withdraw)
|
|
await safe_edit(
|
|
callback,
|
|
f"Current minimum withdrawal: {load_config().min_withdraw:.2f} RUB\nSend a new value:",
|
|
reply_markup=back_kb("admin_back"),
|
|
)
|
|
|
|
|
|
@router.message(Form.admin_min_withdraw)
|
|
async def admin_min_withdraw_save(message: Message, state: FSMContext):
|
|
if not _admin(message.from_user.id):
|
|
return
|
|
try:
|
|
value = float((message.text or "").replace(",", "."))
|
|
if value <= 0:
|
|
raise ValueError
|
|
except ValueError:
|
|
await message.answer("Send a positive number.")
|
|
return
|
|
update_min_withdraw(value)
|
|
await state.clear()
|
|
await message.answer(f"Minimum withdrawal updated to {value:.2f} RUB", reply_markup=admin_menu_kb())
|
|
|
|
|
|
@router.callback_query(F.data == "admin_treasury")
|
|
async def admin_treasury(callback: CallbackQuery, state: FSMContext):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
await state.set_state(Form.admin_treasury_amount)
|
|
await safe_edit(callback, "How much RUB should be added to the treasury?", reply_markup=back_kb("admin_back"))
|
|
|
|
|
|
@router.message(Form.admin_treasury_amount)
|
|
async def admin_treasury_amount(message: Message, state: FSMContext):
|
|
if not _admin(message.from_user.id):
|
|
return
|
|
try:
|
|
amount = float((message.text or "").replace(",", "."))
|
|
if amount <= 0:
|
|
raise ValueError
|
|
except ValueError:
|
|
await message.answer("Send a positive number.")
|
|
return
|
|
await state.clear()
|
|
invoice = await create_treasury_invoice(amount)
|
|
if not invoice:
|
|
await message.answer("CryptoBot invoice creation failed.", reply_markup=admin_menu_kb())
|
|
return
|
|
pay_url, invoice_id, amount_usdt = invoice
|
|
keyboard = InlineKeyboardMarkup(
|
|
inline_keyboard=[
|
|
[InlineKeyboardButton(text="Pay", url=pay_url)],
|
|
[InlineKeyboardButton(text="Check payment", callback_data=f"treasury_check_{invoice_id}_{amount}")],
|
|
]
|
|
)
|
|
await message.answer(
|
|
f"Treasury invoice: <b>{amount:.2f} RUB</b> (~{amount_usdt} USDT)\nPay it and check the status.",
|
|
parse_mode="HTML",
|
|
reply_markup=keyboard,
|
|
)
|
|
|
|
|
|
@router.callback_query(F.data.startswith("treasury_check_"))
|
|
async def treasury_check(callback: CallbackQuery):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
_, _, invoice_id, amount = callback.data.split("_", 3)
|
|
try:
|
|
paid = await check_treasury_invoice(int(invoice_id))
|
|
amount_rub = float(amount)
|
|
except (ValueError, TypeError):
|
|
paid = False
|
|
amount_rub = 0.0
|
|
if not paid:
|
|
await callback.answer("Payment has not arrived yet", show_alert=True)
|
|
return
|
|
balance = add_treasury(amount_rub)
|
|
await safe_edit(callback, f"Treasury topped up by {amount_rub:.2f} RUB. Total: {balance:.2f} RUB", reply_markup=admin_menu_kb())
|
|
await callback.answer("Paid")
|
|
|
|
|
|
@router.callback_query(F.data == "admin_broadcast")
|
|
async def admin_broadcast_prompt(callback: CallbackQuery, state: FSMContext):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
await state.set_state(Form.admin_broadcast)
|
|
await safe_edit(callback, "Send the broadcast text or a photo with caption.", reply_markup=back_kb("admin_back"))
|
|
|
|
|
|
@router.message(Form.admin_broadcast)
|
|
async def admin_broadcast_send(message: Message, state: FSMContext):
|
|
if not _admin(message.from_user.id):
|
|
return
|
|
await state.clear()
|
|
sent, failed = await broadcast(
|
|
message.bot,
|
|
text=message.text,
|
|
photo_id=message.photo[-1].file_id if message.photo else None,
|
|
caption=message.caption,
|
|
)
|
|
await message.answer(f"Broadcast complete. Sent: {sent}, failed: {failed}.", reply_markup=admin_menu_kb())
|
|
|
|
|
|
@router.callback_query(F.data == "admin_users")
|
|
async def admin_users(callback: CallbackQuery, state: FSMContext):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
await state.set_state(Form.admin_find_user)
|
|
await safe_edit(callback, "Send a user ID or @username.", reply_markup=back_kb("admin_back"))
|
|
|
|
|
|
@router.message(Form.admin_find_user)
|
|
async def admin_find_user(message: Message, state: FSMContext):
|
|
if not _admin(message.from_user.id):
|
|
return
|
|
uid = find_user((message.text or "").strip())
|
|
await state.clear()
|
|
if uid is None:
|
|
await message.answer("User not found.", reply_markup=admin_menu_kb())
|
|
return
|
|
card = _user_card(uid)
|
|
if card:
|
|
await message.answer(card[0], parse_mode="HTML", reply_markup=card[1])
|
|
|
|
|
|
@router.callback_query(F.data.startswith("u_bal_add_"))
|
|
async def u_bal_add(callback: CallbackQuery, state: FSMContext):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
uid = int(callback.data.removeprefix("u_bal_add_"))
|
|
await state.update_data(target_uid=uid)
|
|
await state.set_state(Form.admin_user_balance_add)
|
|
await safe_edit(callback, "Send the amount to add:", reply_markup=back_kb("admin_back"))
|
|
|
|
|
|
@router.message(Form.admin_user_balance_add)
|
|
async def u_bal_add_save(message: Message, state: FSMContext):
|
|
if not _admin(message.from_user.id):
|
|
return
|
|
try:
|
|
amount = float((message.text or "").replace(",", "."))
|
|
if amount <= 0:
|
|
raise ValueError
|
|
except ValueError:
|
|
await message.answer("Send a positive number.")
|
|
return
|
|
uid = (await state.get_data()).get("target_uid")
|
|
add_balance(uid, amount)
|
|
await state.clear()
|
|
await message.answer(f"Added {amount:.2f} RUB to {uid}.", reply_markup=admin_menu_kb())
|
|
|
|
|
|
@router.callback_query(F.data.startswith("u_bal_sub_"))
|
|
async def u_bal_sub(callback: CallbackQuery, state: FSMContext):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
uid = int(callback.data.removeprefix("u_bal_sub_"))
|
|
await state.update_data(target_uid=uid)
|
|
await state.set_state(Form.admin_user_balance_sub)
|
|
await safe_edit(callback, "Send the amount to subtract:", reply_markup=back_kb("admin_back"))
|
|
|
|
|
|
@router.message(Form.admin_user_balance_sub)
|
|
async def u_bal_sub_save(message: Message, state: FSMContext):
|
|
if not _admin(message.from_user.id):
|
|
return
|
|
try:
|
|
amount = float((message.text or "").replace(",", "."))
|
|
if amount <= 0:
|
|
raise ValueError
|
|
except ValueError:
|
|
await message.answer("Send a positive number.")
|
|
return
|
|
uid = (await state.get_data()).get("target_uid")
|
|
sub_balance(uid, amount)
|
|
await state.clear()
|
|
await message.answer(f"Subtracted {amount:.2f} RUB from {uid}.", reply_markup=admin_menu_kb())
|
|
|
|
|
|
@router.callback_query(F.data.startswith("u_ban_"))
|
|
async def u_ban(callback: CallbackQuery, state: FSMContext):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
uid = int(callback.data.removeprefix("u_ban_"))
|
|
profile = get(uid)
|
|
if not profile:
|
|
return
|
|
if profile.is_banned:
|
|
from services.admin import unban_user
|
|
|
|
unban_user(uid)
|
|
await callback.answer("Unbanned")
|
|
else:
|
|
await state.update_data(target_uid=uid)
|
|
await state.set_state(Form.admin_user_ban_reason)
|
|
await safe_edit(callback, "Send the ban reason:", reply_markup=back_kb("admin_back"))
|
|
return
|
|
card = _user_card(uid)
|
|
if card:
|
|
await safe_edit(callback, card[0], reply_markup=card[1])
|
|
|
|
|
|
@router.message(Form.admin_user_ban_reason)
|
|
async def u_ban_reason(message: Message, state: FSMContext):
|
|
if not _admin(message.from_user.id):
|
|
return
|
|
uid = (await state.get_data()).get("target_uid")
|
|
ban_user(uid, (message.text or "No reason").strip())
|
|
await state.clear()
|
|
card = _user_card(uid)
|
|
if card:
|
|
await message.answer(card[0], parse_mode="HTML", reply_markup=card[1])
|
|
|
|
|
|
@router.callback_query(F.data.startswith("u_admin_"))
|
|
async def u_admin(callback: CallbackQuery):
|
|
if not _admin(callback.from_user.id):
|
|
return
|
|
uid = int(callback.data.removeprefix("u_admin_"))
|
|
enabled = toggle_user_admin(uid)
|
|
await callback.answer(f"Admin: {'ON' if enabled else 'OFF'}")
|
|
card = _user_card(uid)
|
|
if card:
|
|
await safe_edit(callback, card[0], reply_markup=card[1])
|