27 lines
1.1 KiB
Python
27 lines
1.1 KiB
Python
from telegram import Update
|
|
from telegram.ext import CommandHandler, ContextTypes
|
|
from db import AsyncSessionLocal, log_action
|
|
from models import Button
|
|
|
|
async def del_button(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
args = context.args
|
|
if not args:
|
|
if update.message:
|
|
await update.message.reply_text('Используйте: /del_button <название>')
|
|
return
|
|
name = args[0]
|
|
from sqlalchemy import select
|
|
async with AsyncSessionLocal() as session:
|
|
result = await session.execute(select(Button).where(Button.name == name))
|
|
button = result.scalar_one_or_none()
|
|
if not button:
|
|
if update.message:
|
|
await update.message.reply_text('Кнопка не найдена.')
|
|
return
|
|
await session.delete(button)
|
|
await session.commit()
|
|
user_id = update.effective_user.id if update.effective_user else None
|
|
await log_action(user_id, "del_button", f"name={name}")
|
|
if update.message:
|
|
await update.message.reply_text(f'Кнопка \"{name}\" удалена.')
|