Advertisement
Andrexxelles

Untitled

Mar 8th, 2021
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. from aiogram import types
  2. from aiogram.dispatcher.storage import FSMContext
  3. from aiogram.types import ReplyKeyboardRemove
  4. from sqlalchemy.dialects.postgresql import insert
  5. from sqlalchemy.future import select
  6.  
  7. from button import actions_encryption
  8. from db import async_session, Key_table
  9. from func import create_key
  10. from loader import dp
  11. from middleware import get_lang
  12. from states import CreateKey
  13.  
  14. # the main mechanism
  15. '''@anti_flood(1)
  16. @dp_c.message_handler(content_types='any')
  17. async def hi_tag(message):
  18.    copy = message.to_python()
  19.    await sleep(10)
  20.    await message.answer(f'cope = message.to_python()' + f'\n{copy}')
  21.    copy = types.Message.to_object(copy)
  22.    await copy.copy_to(chat_id=message.chat.id)'''
  23.  
  24.  
  25. @dp.message_handler(commands=['key'])
  26. async def encryption(message: types.Message, state: FSMContext):
  27.     await message.answer('Создаем типо ключ,тест #99493843213 :',
  28.                          reply_markup=actions_encryption[await get_lang(message)])
  29.     count = message.text.split()[-1]
  30.  
  31.     try:
  32.  
  33.         count = int(count)
  34.     except:
  35.         count = 1
  36.  
  37.     await message.answer(f'{count}')
  38.     await message.answer(isinstance(count, int))
  39.  
  40.     if count > 100:
  41.         count = 100
  42.     elif count < 1:
  43.         count = 1
  44.  
  45.     while True:
  46.  
  47.         # Генерируем ключ
  48.         key = await create_key()
  49.  
  50.         # Ищем ключ в базе
  51.         async with async_session as session:
  52.             result = await session.execute(select(Key_table.key).where(Key_table.key == key))
  53.  
  54.             # .scalar() возвращает None или первую запись с таким ключом
  55.             result = result.scalar()
  56.  
  57.             # Если ключ найден,запускаем цикл с начала
  58.             if result is not None:
  59.                 continue
  60.  
  61.             # Если ключ не найден,заносим в базу данных
  62.             else:
  63.                 await message.answer('Tests')
  64.  
  65.                 new_key = await session.execute(insert(Key_table).values(
  66.                     user_id=message.from_user,
  67.                     key=key,
  68.                     count=count,
  69.                     status=True
  70.                 ))
  71.                 await session.commit()
  72.                 break
  73.     async with state.proxy() as data:
  74.         data['key'] = key
  75.     await message.answer(data['key'])
  76.     await CreateKey.media_upload.set()
  77.  
  78.  
  79. # noinspection PyTypeChecker
  80. @dp.message_handler(state=CreateKey.media_upload, text=['🚫Cancel', '🚫Отмена'])
  81. async def cancel(message: types.Message, state: FSMContext):
  82.     await state.finish()
  83.     await message.answer('Completed', reply_markup=ReplyKeyboardRemove())
  84.  
  85.  
  86. # state 2
  87. @dp.message_handler(state=CreateKey.media_upload)
  88. async def media_upload(message, state: FSMContext):
  89.     await message.answer('Слава великому Андрею,все работает!')
  90.     # async with state.proxy() as data:
  91.     # data['media_1'] = message.text
  92.     # data['key'] = await create_key()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement