sukuinote/sukuinote/__main__.py

46 lines
1.6 KiB
Python
Raw Permalink Normal View History

2021-03-29 08:15:41 +00:00
import html
2020-10-16 06:12:56 +00:00
import asyncio
2021-03-29 08:15:41 +00:00
import logging
import traceback
2020-10-16 06:12:56 +00:00
from pyrogram import idle
2021-03-10 08:27:11 +00:00
from pyrogram.errors.exceptions.flood_420 import FloodWait
from . import loop, apps, slave, app_user_ids, session, log_ring, config
2020-10-16 06:12:56 +00:00
async def main():
async def _start_app(app):
await app.start()
asyncio.create_task(_get_me_loop(app))
async def _get_me_loop(app):
while True:
try:
me = await app.get_me()
app_user_ids[me.id] = me
2021-03-10 08:27:11 +00:00
except BaseException:
2020-10-16 06:12:56 +00:00
pass
await asyncio.sleep(60)
2021-03-10 08:27:11 +00:00
async def log_ring_worker():
while True:
await asyncio.sleep(1)
try:
text = log_ring.popleft()
except IndexError:
pass
2021-03-10 08:43:49 +00:00
else:
while True:
try:
await slave.send_message(config['config']['log_chat'], text, disable_web_page_preview=True)
except FloodWait as ex:
await asyncio.sleep(ex.x + 1)
2021-03-29 08:15:41 +00:00
except BaseException:
logging.exception('Exception occured while sending message to log chat')
log_ring.append(f'Exception occured while sending message to log chat\n\n{html.escape(traceback.format_exc())}')
2021-03-10 08:43:49 +00:00
else:
break
2021-03-10 08:27:11 +00:00
asyncio.create_task(log_ring_worker())
2020-10-16 06:12:56 +00:00
await asyncio.gather(*(_start_app(app) for app in apps), slave.start())
await idle()
await asyncio.gather(*(app.stop() for app in apps), slave.stop())
await session.close()
loop.run_until_complete(main())