Как реализовать систему браков телеграмм боте?
Уже третий день пытаюсь сделать рабочий отрывок кода, в котором реализована система браков, но никак нормально не получается. В чём моя ошибка?
from aiogram import Bot, Dispatcher, types
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.utils import executor
API_TOKEN = 'YOUR_API_TOKEN' # Укажите ваш токен
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
marriages = {} # Словарь для хранения информации о браках
@dp.message_handler(commands=['брак'])
async def propose_marriage(message: types.Message):
user_id = message.from_user.id
chat_id = message.chat.id
marriages[chat_id] = {'proposal_from': user_id, 'proposed_to': None}
keyboard = InlineKeyboardMarkup()
keyboard.add(InlineKeyboardButton(text="Да", callback_data="marry_yes"))
keyboard.add(InlineKeyboardButton(text="Нет", callback_data="marry_no"))
await bot.send_message(chat_id, "Вы предложены в брак. Принимаете?", reply_markup=keyboard)
@dp.callback_query_handler(lambda query: query.data == 'marry_yes')
async def accept_marriage(query: types.CallbackQuery):
chat_id = query.message.chat.id
user_id = query.from_user.id
if chat_id in marriages and marriages[chat_id]['proposed_to'] is None:
marriages[chat_id]['proposed_to'] = user_id
await bot.send_message(chat_id, f"Поздравляем! Вы вступили в брак.")
else:
await bot.send_message(chat_id, "Извините, предложение уже принято.")
@dp.callback_query_handler(lambda query: query.data == 'marry_no')
async def reject_marriage(query: types.CallbackQuery):
chat_id = query.message.chat.id
user_id = query.from_user.id
if chat_id in marriages and marriages[chat_id]['proposed_to'] == user_id:
del marriages[chat_id]
await bot.send_message(chat_id, "Вы отказались от брака.")
else:
await bot.send_message(chat_id, "Извините, предложение не для вас.")
@dp.message_handler(commands=['развод'])
async def divorce(message: types.Message):
chat_id = message.chat.id
user_id = message.from_user.id
if chat_id in marriages and marriages[chat_id]['proposal_from'] == user_id:
del marriages[chat_id]
await bot.send_message(chat_id, "Брак расторгнут.")
else:
await bot.send_message(chat_id, "Вы не можете подать на развод.")
@dp.message_handler(commands=['мой_брак'])
async def my_marriage(message: types.Message):
chat_id = message.chat.id
if chat_id in marriages:
proposal_from = marriages[chat_id]['proposal_from']
proposed_to = marriages[chat_id]['proposed_to']
await bot.send_message(chat_id, f"Предложение от: {proposal_from}\nПринято: {proposed_to}")
else:
await bot.send_message(chat_id, "У вас нет брака.")
@dp.message_handler(commands=['браки'])
async def list_marriages(message: types.Message):
chat_id = message.chat.id
if marriages:
for key, value in marriages.items():
proposal_from = value['proposal_from']
proposed_to = value['proposed_to']
await bot.send_message(chat_id, f"Брак между {proposal_from} и {proposed_to}")
else:
await bot.send_message(chat_id, "В группе нет браков.")
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)