forked from FOSS/ASF-Control-Bot
refactor(bot): modularize bot code
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import asyncio
|
||||
|
||||
from bot.api.asf import get_2fa_token, get_confirmations
|
||||
from bot.state import twofa_tasks
|
||||
from bot.ui.keyboards import twofa_keyboard
|
||||
|
||||
|
||||
async def stop_twofa_task(message_id: int) -> None:
|
||||
task = twofa_tasks.pop(message_id, None)
|
||||
if task:
|
||||
task.cancel()
|
||||
try:
|
||||
await task
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
async def auto_update_2fa(message, bot_name: str) -> None:
|
||||
last_code = None
|
||||
message_id = message.message_id
|
||||
try:
|
||||
while True:
|
||||
code = await get_2fa_token(bot_name)
|
||||
if code != last_code:
|
||||
confirmations = await get_confirmations(bot_name)
|
||||
text = f"🔐 {bot_name}\n\n<code>{code}</code>"
|
||||
try:
|
||||
await message.edit_text(
|
||||
text,
|
||||
reply_markup=twofa_keyboard(bot_name, bool(confirmations)),
|
||||
)
|
||||
last_code = code
|
||||
except Exception:
|
||||
break
|
||||
await asyncio.sleep(15)
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
except Exception as error:
|
||||
print(f"2FA updater error: {error}")
|
||||
finally:
|
||||
twofa_tasks.pop(message_id, None)
|
||||
Reference in New Issue
Block a user