chore(userbot): apply ruff check --fix and ruff format
- ruff check --fix: 210 auto-fixed errors (import sorting, trailing whitespace, unused imports, f-string fixups, deprecated annotations) - ruff format: 104 files reformatted to consistent style - 268 non-auto-fixable issues remain (S113 requests timeout, etc.)
This commit is contained in:
+84
-104
@@ -14,12 +14,12 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
from collections.abc import AsyncGenerator
|
||||
from time import perf_counter
|
||||
from typing import AsyncGenerator, List, Optional, Union
|
||||
from typing import Optional
|
||||
|
||||
from pyrogram import Client, enums, filters, raw, types, utils
|
||||
from pyrogram.types.object import Object
|
||||
|
||||
from utils.misc import modules_help, prefix
|
||||
from utils.scripts import format_exc
|
||||
|
||||
@@ -28,7 +28,7 @@ class Chat(Object):
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
client: "Client" = None,
|
||||
client: 'Client' = None,
|
||||
id: id,
|
||||
type: type,
|
||||
is_verified: bool = None,
|
||||
@@ -41,7 +41,7 @@ class Chat(Object):
|
||||
username: str = None,
|
||||
first_name: str = None,
|
||||
last_name: str = None,
|
||||
photo: "types.ChatPhoto" = None,
|
||||
photo: 'types.ChatPhoto' = None,
|
||||
bio: str = None,
|
||||
description: str = None,
|
||||
dc_id: int = None,
|
||||
@@ -51,12 +51,12 @@ class Chat(Object):
|
||||
sticker_set_name: str = None,
|
||||
can_set_sticker_set: bool = None,
|
||||
members_count: int = None,
|
||||
restrictions: List["types.Restriction"] = None,
|
||||
permissions: "types.ChatPermissions" = None,
|
||||
restrictions: list['types.Restriction'] = None,
|
||||
permissions: 'types.ChatPermissions' = None,
|
||||
distance: int = None,
|
||||
linked_chat: "types.Chat" = None,
|
||||
send_as_chat: "types.Chat" = None,
|
||||
available_reactions: Optional["types.ChatReactions"] = None,
|
||||
linked_chat: 'types.Chat' = None,
|
||||
send_as_chat: 'types.Chat' = None,
|
||||
available_reactions: Optional['types.ChatReactions'] = None,
|
||||
is_admin: bool = False,
|
||||
deactivated: bool = False,
|
||||
):
|
||||
@@ -94,98 +94,82 @@ class Chat(Object):
|
||||
self.deactivated = deactivated
|
||||
|
||||
@staticmethod
|
||||
def _parse_user_chat(client, user: raw.types.User) -> "Chat":
|
||||
def _parse_user_chat(client, user: raw.types.User) -> 'Chat':
|
||||
peer_id = user.id
|
||||
|
||||
return Chat(
|
||||
id=peer_id,
|
||||
type=enums.ChatType.BOT if user.bot else enums.ChatType.PRIVATE,
|
||||
is_verified=getattr(user, "verified", None),
|
||||
is_restricted=getattr(user, "restricted", None),
|
||||
is_scam=getattr(user, "scam", None),
|
||||
is_fake=getattr(user, "fake", None),
|
||||
is_support=getattr(user, "support", None),
|
||||
is_verified=getattr(user, 'verified', None),
|
||||
is_restricted=getattr(user, 'restricted', None),
|
||||
is_scam=getattr(user, 'scam', None),
|
||||
is_fake=getattr(user, 'fake', None),
|
||||
is_support=getattr(user, 'support', None),
|
||||
username=user.username,
|
||||
first_name=user.first_name,
|
||||
last_name=user.last_name,
|
||||
photo=types.ChatPhoto._parse(client, user.photo, peer_id, user.access_hash),
|
||||
restrictions=types.List(
|
||||
[types.Restriction._parse(r) for r in user.restriction_reason]
|
||||
)
|
||||
or None,
|
||||
dc_id=getattr(getattr(user, "photo", None), "dc_id", None),
|
||||
restrictions=types.List([types.Restriction._parse(r) for r in user.restriction_reason]) or None,
|
||||
dc_id=getattr(getattr(user, 'photo', None), 'dc_id', None),
|
||||
client=client,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _parse_chat_chat(client, chat: raw.types.Chat) -> "Chat":
|
||||
def _parse_chat_chat(client, chat: raw.types.Chat) -> 'Chat':
|
||||
peer_id = -chat.id
|
||||
return Chat(
|
||||
id=peer_id,
|
||||
type=enums.ChatType.GROUP,
|
||||
title=chat.title,
|
||||
is_creator=getattr(chat, "creator", None),
|
||||
photo=types.ChatPhoto._parse(
|
||||
client, getattr(chat, "photo", None), peer_id, 0
|
||||
),
|
||||
permissions=types.ChatPermissions._parse(
|
||||
getattr(chat, "default_banned_rights", None)
|
||||
),
|
||||
members_count=getattr(chat, "participants_count", None),
|
||||
dc_id=getattr(getattr(chat, "photo", None), "dc_id", None),
|
||||
has_protected_content=getattr(chat, "noforwards", None),
|
||||
is_creator=getattr(chat, 'creator', None),
|
||||
photo=types.ChatPhoto._parse(client, getattr(chat, 'photo', None), peer_id, 0),
|
||||
permissions=types.ChatPermissions._parse(getattr(chat, 'default_banned_rights', None)),
|
||||
members_count=getattr(chat, 'participants_count', None),
|
||||
dc_id=getattr(getattr(chat, 'photo', None), 'dc_id', None),
|
||||
has_protected_content=getattr(chat, 'noforwards', None),
|
||||
client=client,
|
||||
is_admin=bool(getattr(chat, "admin_rights", False)),
|
||||
is_admin=bool(getattr(chat, 'admin_rights', False)),
|
||||
deactivated=chat.deactivated,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _parse_channel_chat(client, channel: raw.types.Channel) -> "Chat":
|
||||
def _parse_channel_chat(client, channel: raw.types.Channel) -> 'Chat':
|
||||
peer_id = utils.get_channel_id(channel.id)
|
||||
restriction_reason = getattr(channel, "restriction_reason", [])
|
||||
restriction_reason = getattr(channel, 'restriction_reason', [])
|
||||
|
||||
return Chat(
|
||||
id=peer_id,
|
||||
type=(
|
||||
enums.ChatType.SUPERGROUP
|
||||
if getattr(channel, "megagroup", None)
|
||||
else enums.ChatType.CHANNEL
|
||||
),
|
||||
is_verified=getattr(channel, "verified", None),
|
||||
is_restricted=getattr(channel, "restricted", None),
|
||||
is_creator=getattr(channel, "creator", None),
|
||||
is_scam=getattr(channel, "scam", None),
|
||||
is_fake=getattr(channel, "fake", None),
|
||||
type=(enums.ChatType.SUPERGROUP if getattr(channel, 'megagroup', None) else enums.ChatType.CHANNEL),
|
||||
is_verified=getattr(channel, 'verified', None),
|
||||
is_restricted=getattr(channel, 'restricted', None),
|
||||
is_creator=getattr(channel, 'creator', None),
|
||||
is_scam=getattr(channel, 'scam', None),
|
||||
is_fake=getattr(channel, 'fake', None),
|
||||
title=channel.title,
|
||||
username=getattr(channel, "username", None),
|
||||
username=getattr(channel, 'username', None),
|
||||
photo=types.ChatPhoto._parse(
|
||||
client,
|
||||
getattr(channel, "photo", None),
|
||||
getattr(channel, 'photo', None),
|
||||
peer_id,
|
||||
getattr(channel, "access_hash", 0),
|
||||
getattr(channel, 'access_hash', 0),
|
||||
),
|
||||
restrictions=types.List(
|
||||
[types.Restriction._parse(r) for r in restriction_reason]
|
||||
)
|
||||
or None,
|
||||
permissions=types.ChatPermissions._parse(
|
||||
getattr(channel, "default_banned_rights", None)
|
||||
),
|
||||
members_count=getattr(channel, "participants_count", None),
|
||||
dc_id=getattr(getattr(channel, "photo", None), "dc_id", None),
|
||||
has_protected_content=getattr(channel, "noforwards", None),
|
||||
is_admin=bool(getattr(channel, "admin_rights", False)),
|
||||
restrictions=types.List([types.Restriction._parse(r) for r in restriction_reason]) or None,
|
||||
permissions=types.ChatPermissions._parse(getattr(channel, 'default_banned_rights', None)),
|
||||
members_count=getattr(channel, 'participants_count', None),
|
||||
dc_id=getattr(getattr(channel, 'photo', None), 'dc_id', None),
|
||||
has_protected_content=getattr(channel, 'noforwards', None),
|
||||
is_admin=bool(getattr(channel, 'admin_rights', False)),
|
||||
client=client,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _parse(
|
||||
client,
|
||||
message: Union[raw.types.Message, raw.types.MessageService],
|
||||
message: raw.types.Message | raw.types.MessageService,
|
||||
users: dict,
|
||||
chats: dict,
|
||||
is_chat: bool,
|
||||
) -> "Chat":
|
||||
) -> 'Chat':
|
||||
from_id = utils.get_raw_peer_id(message.from_id)
|
||||
peer_id = utils.get_raw_peer_id(message.peer_id)
|
||||
chat_id = (peer_id or from_id) if is_chat else (from_id or peer_id)
|
||||
@@ -211,9 +195,9 @@ class Dialog(Object):
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
client: "Client" = None,
|
||||
chat: "types.Chat",
|
||||
top_message: "types.Message",
|
||||
client: 'Client' = None,
|
||||
chat: 'types.Chat',
|
||||
top_message: 'types.Message',
|
||||
unread_messages_count: int,
|
||||
unread_mentions_count: int,
|
||||
unread_mark: bool,
|
||||
@@ -229,7 +213,7 @@ class Dialog(Object):
|
||||
self.is_pinned = is_pinned
|
||||
|
||||
@staticmethod
|
||||
def _parse(client, dialog: "raw.types.Dialog", messages, users, chats) -> "Dialog":
|
||||
def _parse(client, dialog: 'raw.types.Dialog', messages, users, chats) -> 'Dialog':
|
||||
return Dialog(
|
||||
chat=Chat._parse_dialog(client, dialog.peer, users, chats),
|
||||
top_message=messages.get(utils.get_peer_id(dialog.peer)),
|
||||
@@ -241,9 +225,7 @@ class Dialog(Object):
|
||||
)
|
||||
|
||||
|
||||
async def get_dialogs(
|
||||
self: "Client", limit: int = 0
|
||||
) -> Optional[AsyncGenerator["types.Dialog", None]]:
|
||||
async def get_dialogs(self: 'Client', limit: int = 0) -> AsyncGenerator['types.Dialog', None] | None:
|
||||
current = 0
|
||||
total = limit or (1 << 31) - 1
|
||||
limit = min(100, total)
|
||||
@@ -287,7 +269,7 @@ async def get_dialogs(
|
||||
return
|
||||
|
||||
|
||||
@Client.on_message(filters.command("admlist", prefix) & filters.me)
|
||||
@Client.on_message(filters.command('admlist', prefix) & filters.me)
|
||||
async def admlist(client: Client, message: types.Message):
|
||||
await message.edit("<b>Retrieving information... (it'll take some time)</b>")
|
||||
|
||||
@@ -298,48 +280,46 @@ async def admlist(client: Client, message: types.Message):
|
||||
owned_usernamed_chats = []
|
||||
async for dialog in get_dialogs(client):
|
||||
chat = dialog.chat
|
||||
if getattr(chat, "deactivated", False):
|
||||
if getattr(chat, 'deactivated', False):
|
||||
continue
|
||||
if getattr(chat, "is_creator", False) and getattr(chat, "username", None):
|
||||
if getattr(chat, 'is_creator', False) and getattr(chat, 'username', None):
|
||||
owned_usernamed_chats.append(chat)
|
||||
elif getattr(chat, "is_creator", False):
|
||||
elif getattr(chat, 'is_creator', False):
|
||||
owned_chats.append(chat)
|
||||
elif getattr(chat, "is_admin", False):
|
||||
elif getattr(chat, 'is_admin', False):
|
||||
adminned_chats.append(chat)
|
||||
|
||||
text = "<b>Adminned chats:</b>\n"
|
||||
text = '<b>Adminned chats:</b>\n'
|
||||
for index, chat in enumerate(adminned_chats):
|
||||
cid = str(chat.id).replace("-100", "")
|
||||
text += f"{index + 1}. <a href=https://t.me/c/{cid}/1>{chat.title}</a>\n"
|
||||
cid = str(chat.id).replace('-100', '')
|
||||
text += f'{index + 1}. <a href=https://t.me/c/{cid}/1>{chat.title}</a>\n'
|
||||
|
||||
text += "\n<b>Owned chats:</b>\n"
|
||||
text += '\n<b>Owned chats:</b>\n'
|
||||
for index, chat in enumerate(owned_chats):
|
||||
cid = str(chat.id).replace("-100", "")
|
||||
text += f"{index + 1}. <a href=https://t.me/c/{cid}/1>{chat.title}</a>\n"
|
||||
cid = str(chat.id).replace('-100', '')
|
||||
text += f'{index + 1}. <a href=https://t.me/c/{cid}/1>{chat.title}</a>\n'
|
||||
|
||||
text += "\n<b>Owned chats with username:</b>\n"
|
||||
text += '\n<b>Owned chats with username:</b>\n'
|
||||
for index, chat in enumerate(owned_usernamed_chats):
|
||||
cid = str(chat.id).replace("-100", "")
|
||||
text += f"{index + 1}. <a href=https://t.me/c/{cid}/1>{chat.title}</a>\n"
|
||||
cid = str(chat.id).replace('-100', '')
|
||||
text += f'{index + 1}. <a href=https://t.me/c/{cid}/1>{chat.title}</a>\n'
|
||||
|
||||
stop = perf_counter()
|
||||
total_count = (
|
||||
len(adminned_chats) + len(owned_chats) + len(owned_usernamed_chats)
|
||||
)
|
||||
total_count = len(adminned_chats) + len(owned_chats) + len(owned_usernamed_chats)
|
||||
await message.edit(
|
||||
text + "\n"
|
||||
f"<b><u>Total:</u></b> {total_count}"
|
||||
f"\n<b><u>Adminned chats:</u></b> {len(adminned_chats)}\n"
|
||||
f"<b><u>Owned chats:</u></b> {len(owned_chats)}\n"
|
||||
f"<b><u>Owned chats with username:</u></b> {len(owned_usernamed_chats)}\n\n"
|
||||
f"Done in {round(stop - start, 3)} seconds.",
|
||||
text + '\n'
|
||||
f'<b><u>Total:</u></b> {total_count}'
|
||||
f'\n<b><u>Adminned chats:</u></b> {len(adminned_chats)}\n'
|
||||
f'<b><u>Owned chats:</u></b> {len(owned_chats)}\n'
|
||||
f'<b><u>Owned chats with username:</u></b> {len(owned_usernamed_chats)}\n\n'
|
||||
f'Done in {round(stop - start, 3)} seconds.',
|
||||
)
|
||||
except Exception as e:
|
||||
await message.edit(format_exc(e))
|
||||
return
|
||||
|
||||
|
||||
@Client.on_message(filters.command("admcount", prefix) & filters.me)
|
||||
@Client.on_message(filters.command('admcount', prefix) & filters.me)
|
||||
async def admcount(client: Client, message: types.Message):
|
||||
await message.edit("<b>Retrieving information... (it'll take some time)</b>")
|
||||
|
||||
@@ -350,31 +330,31 @@ async def admcount(client: Client, message: types.Message):
|
||||
owned_usernamed_chats = 0
|
||||
async for dialog in get_dialogs(client):
|
||||
chat = dialog.chat
|
||||
if getattr(chat, "deactivated", False):
|
||||
if getattr(chat, 'deactivated', False):
|
||||
continue
|
||||
if getattr(chat, "is_creator", False) and getattr(chat, "username", None):
|
||||
if getattr(chat, 'is_creator', False) and getattr(chat, 'username', None):
|
||||
owned_usernamed_chats += 1
|
||||
elif getattr(chat, "is_creator", False):
|
||||
elif getattr(chat, 'is_creator', False):
|
||||
owned_chats += 1
|
||||
elif getattr(chat, "is_admin", False):
|
||||
elif getattr(chat, 'is_admin', False):
|
||||
adminned_chats += 1
|
||||
|
||||
stop = perf_counter()
|
||||
total_count = adminned_chats + owned_chats + owned_usernamed_chats
|
||||
await message.edit(
|
||||
f"<b><u>Total:</u></b> {total_count}"
|
||||
f"\n<b><u>Adminned chats:</u></b> {adminned_chats}\n"
|
||||
f"<b><u>Owned chats:</u></b> {owned_chats}\n"
|
||||
f"<b><u>Owned chats with username:</u></b> {owned_usernamed_chats}\n\n"
|
||||
f"Done in {round(stop - start, 3)} seconds.\n\n"
|
||||
f"<b>Get full list: </b><code>{prefix}admlist</code>",
|
||||
f'<b><u>Total:</u></b> {total_count}'
|
||||
f'\n<b><u>Adminned chats:</u></b> {adminned_chats}\n'
|
||||
f'<b><u>Owned chats:</u></b> {owned_chats}\n'
|
||||
f'<b><u>Owned chats with username:</u></b> {owned_usernamed_chats}\n\n'
|
||||
f'Done in {round(stop - start, 3)} seconds.\n\n'
|
||||
f'<b>Get full list: </b><code>{prefix}admlist</code>',
|
||||
)
|
||||
except Exception as e:
|
||||
await message.edit(format_exc(e))
|
||||
return
|
||||
|
||||
|
||||
modules_help["admlist"] = {
|
||||
"admcount": "Get count of adminned and owned chats",
|
||||
"admlist": "Get list of adminned and owned chats",
|
||||
modules_help['admlist'] = {
|
||||
'admcount': 'Get count of adminned and owned chats',
|
||||
'admlist': 'Get list of adminned and owned chats',
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user