welcomecenterbot/bot/handlers/command_my.py
2023-09-18 10:16:33 +03:00

76 lines
2.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from storage import Profile, scan
from api import get_member, send_message, get_chat_administrators
from utils.mention import userdata_extract
import json
async def construct_unlink_buttons(actor):
print(f"constructing unlink buttons for {actor['children']}")
buttons = []
for vouch in actor["children"]:
for chat_id in actor["chats"]:
r = await get_member(chat_id, vouch)
print(r)
member = r["result"]["user"]
_uid, identity, username = userdata_extract(member)
buttons.append(
{"text": f"{identity} {username}", "callback_data": "unlink" + vouch}
)
return buttons
async def handle_command_my(msg, state):
print("handle my command")
from_id = str(msg["from"]["id"])
sender = Profile.get(from_id, msg)
await handle_command_owner_my(msg)
# генерируем кнопки для всех, за кого поручились
buttons = await construct_unlink_buttons(sender)
reply_markup = {
"inline_keyboard": [
buttons,
]
}
if len(buttons) == 0:
if msg["from"].get("language_code", "ru") == "ru":
body = "Вас ещё никто не узнал? Напишите, я передам нашему кругу"
else:
body = (
"Nobody recognized you? Speak, I will pass your message to the circle"
)
r = await send_message(from_id, body)
print(r)
chat_id = msg["chat"]["id"]
state.make_talking(from_id, chat_id)
else:
if msg["from"].get("language_code", "ru") == "ru":
body = "Нажмите кнопки ниже, чтобы удалить ваши связи"
else:
body = "Unlink your connections pressing the buttons below"
r = await send_message(from_id, body, reply_markup=reply_markup)
print(r)
async def handle_command_owner_my(msg):
chat_id = msg["chat"]["id"]
if chat_id < 0: # is not private
r = await get_chat_administrators(chat_id)
print(r)
owner_id = ""
for admin in r["result"]:
if admin["status"] == "creator":
owner_id = str(admin["user"]["id"])
break
if owner_id:
owner = Profile.get(owner_id, msg)
_uids, members = scan()
for mdata in members:
m = json.loads(mdata.decode("utf-8"))
if owner_id in m["parents"]:
if str(m["id"]) not in owner["children"]:
owner["children"].append(str(m["id"]))
Profile.save(owner)