Andrexxelles

Untitled

Apr 11th, 2021
408
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import datetime
  2.  
  3. from aiogram import types
  4. from aiogram.dispatcher.storage import FSMContext
  5. from aiogram.types import ReplyKeyboardRemove
  6. from sqlalchemy import delete
  7. from sqlalchemy.dialects.postgresql import insert
  8. from sqlalchemy.future import select
  9.  
  10. from button import actions_encryption
  11. from config import MAX_VALUE_IN_KEY
  12. from db import async_session, Key_table, Key_value
  13. from func import create_key
  14. from func import filtr_view_key_with_
  15. from language import language
  16. from loader import dp, bot
  17. from middleware import anti_flood
  18. from states import CreateKey
  19.  
  20. # the main mechanism
  21. '''@anti_flood(1)
  22. @dp.message_handler(content_types='any')
  23. async def hi_tag(message):
  24.    # Переводим любой медиафайл в вид json
  25.    copy = message.to_python()
  26.    await message.answer(f'cope = message.to_python()' + f'\n{copy}')
  27.    # Переводим json в объект message
  28.    copy = types.Message.to_object(copy)
  29.    # С помощью метода copy_to, переводим объект в медифайл
  30.    await copy.copy_to(chat_id=message.chat.id)'''
  31.  
  32. number_list = {1: '1️⃣', 2: '2️⃣', 3: '3️⃣', 4: '4️⃣', 5: '5️⃣',
  33.                6: '6️⃣', 7: '7️⃣', 8: '8️⃣', 9: '9️⃣', 10: '🔟'}
  34.  
  35.  
  36. # command cancel process,del key
  37. @anti_flood(3)
  38. @dp.message_handler(state=CreateKey.media_upload, text=['🚫Cancel', '🚫Отмена'])
  39. async def cancel(message: types.Message, state: FSMContext, lang):
  40.     await state.reset_state(with_data=False)
  41.  
  42.     data = await state.get_data()
  43.  
  44.     if 'message_tag_count' in data:
  45.         await bot.delete_message(
  46.             chat_id=message.from_user.id,
  47.             message_id=data['message_tag_count']
  48.         )
  49.  
  50.     if 'message_tag_finish' not in data:
  51.         message_tag_finish = await message.answer(language['message_tag_cancel'][lang])
  52.         message_tag_finish = message_tag_finish.message_id
  53.     else:
  54.         message_tag_finish = data['message_tag_finish']
  55.  
  56.     if 'message_null' not in data:
  57.         message_null = await message.answer('⚙️',
  58.                                             reply_markup=ReplyKeyboardRemove())
  59.         message_null = message_null.message_id
  60.  
  61.         await bot.delete_message(
  62.             chat_id=message.from_user.id,
  63.             message_id=message_null
  64.         )
  65.  
  66.     async with async_session() as session:
  67.         try:
  68.             await session.execute(delete(Key_table).where(
  69.                 Key_table.key == data['key']
  70.                 and Key_table.user_id == message.from_user.id
  71.             ))
  72.             await session.commit()
  73.         except Exception as e:
  74.             await session.rollback()
  75.             await bot.edit_message_text(
  76.                 chat_id=message.from_user.id,
  77.                 message_id=message_tag_finish,
  78.                 text='Error in cancel db\n'
  79.                      f'{e}')
  80.  
  81.     await state.reset_data()
  82.  
  83.  
  84. # command create key,create values
  85. # noinspection PyArgumentList
  86. @anti_flood(3)
  87. @dp.message_handler(state=CreateKey.media_upload, text=['🔑Create key', '🔑Создать ключ'])
  88. async def completed(message: types.Message, state: FSMContext, lang):
  89.     await state.reset_state(with_data=False)
  90.  
  91.     user_id = message.from_user.id
  92.     data = await state.get_data()
  93.  
  94.     await bot.delete_message(
  95.         chat_id=message.from_user.id,
  96.         message_id=data['message_tag_count']
  97.     )
  98.  
  99.     async with state.proxy() as data:
  100.         del data['message_tag_count']
  101.  
  102.     if 'value1' not in data:
  103.         message_tag_finish = await message.answer(language['message_tag_data'][lang])
  104.         message_tag_finish = message_tag_finish.message_id
  105.  
  106.         async with state.proxy() as data:
  107.             data['message_tag_finish'] = message_tag_finish
  108.  
  109.         await cancel(message, state, lang)
  110.         return
  111.  
  112.     message_tag_finish = await message.answer(language['message_tag_key'][lang])
  113.     message_tag_finish = message_tag_finish.message_id
  114.  
  115.     async with state.proxy() as data:
  116.         data['message_tag_finish'] = message_tag_finish
  117.  
  118.     message_null = await message.answer('⚙️',
  119.                                         reply_markup=ReplyKeyboardRemove())
  120.     message_null = message_null.message_id
  121.  
  122.     await bot.delete_message(
  123.         chat_id=user_id,
  124.         message_id=message_null
  125.     )
  126.  
  127.     async with state.proxy() as data:
  128.         data['message_null'] = True
  129.  
  130.     async with async_session() as session:
  131.         try:
  132.             for count_value in range(1, data['count'] + 1):
  133.                 value = data[f"value{count_value}"]
  134.                 await session.execute(insert(Key_value).values(
  135.                     key=data['key'],
  136.                     value=value,
  137.                     by_order=count_value
  138.                 ))
  139.             await session.commit()
  140.         except Exception as e:
  141.             await session.rollback()
  142.             await bot.edit_message_text(
  143.                 chat_id=message.from_user.id,
  144.                 message_id=message_tag_finish,
  145.                 text='Error in value\n'
  146.                      f'{e}')
  147.             await cancel(message, state, lang)
  148.             return
  149.  
  150.     await bot.edit_message_text(
  151.         message_id=message_tag_finish,
  152.         chat_id=user_id,
  153.         text=f"🔑{await filtr_view_key_with_(data['key'])}"
  154.     )
  155.  
  156.     await state.reset_data()
  157.  
  158.  
  159. # noinspection PyTypeChecker,PyBroadException
  160. # state start,create key
  161. @anti_flood(6, key='key')
  162. @dp.message_handler(commands=['key'])
  163. async def encryption(message: types.Message, state: FSMContext, lang):
  164.     await message.answer(language['encryption'][lang],
  165.                          reply_markup=actions_encryption[lang])
  166.  
  167.     message_tag_count = await message.answer(f"{language['count_media'][lang]}\n0️⃣◀️🔟")
  168.     message_tag_count = message_tag_count.message_id
  169.  
  170.     async with state.proxy() as data:
  171.         data['message_tag_count'] = message_tag_count
  172.         data['count'] = 0
  173.  
  174.     count = message.text.split()[-1]
  175.  
  176.     try:
  177.         count = int(count)
  178.     except ValueError:
  179.         count = 1
  180.  
  181.     if count > 100:
  182.         count = 100
  183.     elif count < 1:
  184.         count = 1
  185.  
  186.     async with async_session() as session:
  187.         try:
  188.             while True:
  189.  
  190.                 # Генерируем ключ
  191.                 key = await create_key()
  192.  
  193.                 # Ищем ключ в базе
  194.                 result = await session.execute(select(Key_table.key).
  195.                                                where(Key_table.key == key))
  196.  
  197.                 # .scalar() возвращает None или первую запись с таким ключом
  198.                 result = result.scalar()
  199.  
  200.                 # Если ключ найден в базе,запускаем цикл с начала
  201.                 if result is not None:
  202.                     continue
  203.  
  204.                 # Если ключ не найден,покидаем цикл
  205.                 else:
  206.                     break
  207.  
  208.             await session.execute(insert(Key_table).values(
  209.                 user_id=message.from_user.id,
  210.                 key=key,
  211.                 count=count,
  212.                 status=True,
  213.                 date=datetime.datetime.utcnow()
  214.             ))
  215.             await session.commit()
  216.         except Exception as e:
  217.             await session.rollback()
  218.             await message.answer('Error in key create\n'
  219.                                  f'{e}')
  220.  
  221.             await bot.delete_message(
  222.                 message_id=message_tag_count
  223.             )
  224.             await state.finish()
  225.             return
  226.  
  227.     async with state.proxy() as data:
  228.         data['key'] = key
  229.  
  230.     await CreateKey.media_upload.set()
  231.  
  232.  
  233. @anti_flood(0)
  234. @dp.message_handler(state=CreateKey.media_upload,
  235.                     content_types=types.ContentTypes.ANY)
  236. async def media_upload(message: types.Message, state: FSMContext, lang):
  237.     data = await state.get_data()
  238.     message_tag_count = data['message_tag_count']
  239.     count = message.message_id - message_tag_count
  240.  
  241.     if message.message_id - message_tag_count > 10:
  242.         return
  243.  
  244.     async with state.proxy() as data:
  245.         data[f"value{count}"] = str(message.to_python())
  246.         data['count'] = count
  247.  
  248.     print(f'{message.from_user.username} - {count}')
  249.     '''if count < MAX_VALUE_IN_KEY:
  250.        try:
  251.            await bot.edit_message_text(
  252.                chat_id=message.from_user.id,
  253.                message_id=message_tag_count,
  254.                text=f"{language['count_media'][lang]}\n"
  255.                     f'{number_list[count]}◀️🔟'
  256.            )
  257.        except Exception as e:
  258.            await message.answer('Error in media upload\n'
  259.                                 f'{e}')'''
  260.  
  261.     if count == MAX_VALUE_IN_KEY:
  262.         await state.reset_state(with_data=False)
  263.         await completed(message, state, lang)
  264.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×