Spaces:
Paused
Paused
Captain Ezio
commited on
Commit
·
105fa08
1
Parent(s):
ab6c63b
Create extract_user.py
Browse files- Powers/utils/extract_user.py +102 -0
Powers/utils/extract_user.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from traceback import format_exc
|
| 2 |
+
from typing import Tuple
|
| 3 |
+
|
| 4 |
+
from pyrogram.types.messages_and_media.message import Message
|
| 5 |
+
|
| 6 |
+
from Powers import LOGGER
|
| 7 |
+
from Powers.bot_class import Gojo
|
| 8 |
+
from Powers.database.users_db import Users
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
async def extract_user(c: Gojo, m: Message) -> Tuple[int, str, str]:
|
| 12 |
+
"""Extract the user from the provided message."""
|
| 13 |
+
user_id = None
|
| 14 |
+
user_first_name = None
|
| 15 |
+
user_name = None
|
| 16 |
+
|
| 17 |
+
if m.reply_to_message and m.reply_to_message.from_user:
|
| 18 |
+
user_id = m.reply_to_message.from_user.id
|
| 19 |
+
user_first_name = m.reply_to_message.from_user.first_name
|
| 20 |
+
user_name = m.reply_to_message.from_user.username
|
| 21 |
+
|
| 22 |
+
elif len(m.text.split()) > 1:
|
| 23 |
+
if len(m.entities) > 1:
|
| 24 |
+
required_entity = m.entities[1]
|
| 25 |
+
if required_entity.type == "text_mention":
|
| 26 |
+
user_id = required_entity.user.id
|
| 27 |
+
user_first_name = required_entity.user.first_name
|
| 28 |
+
user_name = required_entity.user.username
|
| 29 |
+
elif required_entity.type in ("mention", "phone_number"):
|
| 30 |
+
# new long user ids are identified as phone_number
|
| 31 |
+
user_found = m.text[
|
| 32 |
+
required_entity.offset : (
|
| 33 |
+
required_entity.offset + required_entity.length
|
| 34 |
+
)
|
| 35 |
+
]
|
| 36 |
+
|
| 37 |
+
try:
|
| 38 |
+
user_found = int(user_found)
|
| 39 |
+
except (ValueError, Exception) as ef:
|
| 40 |
+
if "invalid literal for int() with base 10:" in str(ef):
|
| 41 |
+
user_found = str(user_found)
|
| 42 |
+
else:
|
| 43 |
+
LOGGER.error(ef)
|
| 44 |
+
LOGGER.error(format_exc())
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
user = Users.get_user_info(user_found)
|
| 48 |
+
user_id = user["_id"]
|
| 49 |
+
user_first_name = user["name"]
|
| 50 |
+
user_name = user["username"]
|
| 51 |
+
except KeyError:
|
| 52 |
+
# If user not in database
|
| 53 |
+
try:
|
| 54 |
+
user = await c.get_users(user_found)
|
| 55 |
+
except Exception as ef:
|
| 56 |
+
return await m.reply_text(f"User not found ! Error: {ef}")
|
| 57 |
+
user_id = user.id
|
| 58 |
+
user_first_name = user.first_name
|
| 59 |
+
user_name = user.username
|
| 60 |
+
except Exception as ef:
|
| 61 |
+
user_id = user_found
|
| 62 |
+
user_first_name = user_found
|
| 63 |
+
user_name = ""
|
| 64 |
+
LOGGER.error(ef)
|
| 65 |
+
LOGGER.error(format_exc())
|
| 66 |
+
|
| 67 |
+
else:
|
| 68 |
+
try:
|
| 69 |
+
user_id = int(m.text.split()[1])
|
| 70 |
+
except (ValueError, Exception) as ef:
|
| 71 |
+
if "invalid literal for int() with base 10:" in str(ef):
|
| 72 |
+
user_id = (
|
| 73 |
+
str(m.text.split()[1])
|
| 74 |
+
if (m.text.split()[1]).startswith("@")
|
| 75 |
+
else None
|
| 76 |
+
)
|
| 77 |
+
else:
|
| 78 |
+
user_id = m.text.split()[1]
|
| 79 |
+
LOGGER.error(ef)
|
| 80 |
+
LOGGER.error(format_exc())
|
| 81 |
+
|
| 82 |
+
if user_id is not None:
|
| 83 |
+
try:
|
| 84 |
+
user = Users.get_user_info(user_id)
|
| 85 |
+
user_first_name = user["name"]
|
| 86 |
+
user_name = user["username"]
|
| 87 |
+
except Exception as ef:
|
| 88 |
+
try:
|
| 89 |
+
user = await c.get_users(user_id)
|
| 90 |
+
except Exception as ef:
|
| 91 |
+
return await m.reply_text(f"User not found ! Error: {ef}")
|
| 92 |
+
user_first_name = user.first_name
|
| 93 |
+
user_name = user.username
|
| 94 |
+
LOGGER.error(ef)
|
| 95 |
+
LOGGER.error(format_exc())
|
| 96 |
+
|
| 97 |
+
else:
|
| 98 |
+
user_id = m.from_user.id
|
| 99 |
+
user_first_name = m.from_user.first_name
|
| 100 |
+
user_name = m.from_user.username
|
| 101 |
+
|
| 102 |
+
return user_id, user_first_name, user_name
|