2023-09-11 20:21:55 +00:00
|
|
|
|
from storage import Profile, scan
|
|
|
|
|
from api import get_member, send_message, get_chat_administrators
|
|
|
|
|
from utils.mention import userdata_extract
|
2023-09-18 07:02:15 +00:00
|
|
|
|
import json
|
2023-04-28 12:24:14 +00:00
|
|
|
|
|
2023-04-23 16:54:58 +00:00
|
|
|
|
|
2023-09-11 20:04:53 +00:00
|
|
|
|
async def construct_unlink_buttons(actor):
|
2023-04-23 16:54:58 +00:00
|
|
|
|
buttons = []
|
2023-09-11 20:04:53 +00:00
|
|
|
|
for vouch in actor["children"]:
|
|
|
|
|
for chat_id in actor["chats"]:
|
|
|
|
|
r = await get_member(chat_id, vouch)
|
|
|
|
|
member = r["result"]["user"]
|
|
|
|
|
_uid, identity, username = userdata_extract(member)
|
|
|
|
|
buttons.append(
|
|
|
|
|
{"text": f"{identity} {username}", "callback_data": "unlink" + vouch}
|
|
|
|
|
)
|
2023-09-11 17:02:29 +00:00
|
|
|
|
return buttons
|
2023-04-23 16:54:58 +00:00
|
|
|
|
|
2023-04-28 12:24:14 +00:00
|
|
|
|
|
2023-09-11 20:04:53 +00:00
|
|
|
|
async def handle_command_my(msg, state):
|
|
|
|
|
print("handle my command")
|
|
|
|
|
from_id = str(msg["from"]["id"])
|
2023-04-23 16:54:58 +00:00
|
|
|
|
sender = Profile.get(from_id, msg)
|
2023-05-01 16:31:17 +00:00
|
|
|
|
|
2023-09-11 20:04:53 +00:00
|
|
|
|
await handle_command_owner_my(msg)
|
2023-05-01 16:31:17 +00:00
|
|
|
|
|
2023-04-23 16:54:58 +00:00
|
|
|
|
# генерируем кнопки для всех, за кого поручились
|
2023-09-11 20:04:53 +00:00
|
|
|
|
buttons = await construct_unlink_buttons(sender)
|
|
|
|
|
reply_markup = {
|
|
|
|
|
"inline_keyboard": [
|
|
|
|
|
buttons,
|
|
|
|
|
]
|
|
|
|
|
}
|
2023-09-11 17:02:29 +00:00
|
|
|
|
if len(buttons) == 0:
|
2023-09-11 20:04:53 +00:00
|
|
|
|
if msg["from"].get("language_code", "ru") == "ru":
|
|
|
|
|
body = "Вас ещё никто не узнал? Напишите, я передам нашему кругу"
|
2023-09-11 17:02:29 +00:00
|
|
|
|
else:
|
2023-09-11 20:04:53 +00:00
|
|
|
|
body = (
|
|
|
|
|
"Nobody recognized you? Speak, I will pass your message to the circle"
|
|
|
|
|
)
|
|
|
|
|
r = await send_message(from_id, body)
|
2023-09-11 17:02:29 +00:00
|
|
|
|
print(r)
|
2023-09-11 20:04:53 +00:00
|
|
|
|
chat_id = msg["chat"]["id"]
|
2023-09-11 17:02:29 +00:00
|
|
|
|
state.make_talking(from_id, chat_id)
|
2023-04-23 16:54:58 +00:00
|
|
|
|
else:
|
2023-09-11 20:04:53 +00:00
|
|
|
|
if msg["from"].get("language_code", "ru") == "ru":
|
|
|
|
|
body = "Нажмите кнопки ниже, чтобы удалить ваши связи"
|
2023-09-11 17:02:29 +00:00
|
|
|
|
else:
|
2023-09-11 20:04:53 +00:00
|
|
|
|
body = "Unlink your connections pressing the buttons below"
|
2023-04-23 16:54:58 +00:00
|
|
|
|
|
2023-09-11 20:04:53 +00:00
|
|
|
|
r = await send_message(from_id, body, reply_markup=reply_markup)
|
2023-05-01 16:31:17 +00:00
|
|
|
|
print(r)
|
|
|
|
|
|
|
|
|
|
|
2023-09-11 20:04:53 +00:00
|
|
|
|
async def handle_command_owner_my(msg):
|
|
|
|
|
chat_id = msg["chat"]["id"]
|
2023-09-18 07:02:15 +00:00
|
|
|
|
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)
|