-
Notifications
You must be signed in to change notification settings - Fork 3
/
chattools.py
199 lines (166 loc) · 8.09 KB
/
chattools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Sh1t-UB (telegram userbot by sh1tn3t)
# Copyright (C) 2021-2022 Sh1tN3t
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import io
import html
from pyrogram import Client, types, errors
from .. import loader, utils
@loader.module("ChatTools", "sh1tn3t")
class ChatToolsMod(loader.Module):
"""Инструменты для чата"""
async def who_cmd(self, app: Client, message: types.Message, args: str):
"""Вывести имя и айди пользователя/чата. Использование: who <@ или ID или реплай>"""
entity = message.from_user
reply = message.reply_to_message
if (args or reply):
try:
entity = await app.get_chat(
(int(args) if args.isdigit() else args) if args
else reply.from_user.id
)
except Exception:
pass
return await utils.answer(
message, f"Имя: <code>{html.escape(utils.get_display_name(entity))}</code>\n"
f"ID: <code>{entity.id}</code>"
)
async def invite_cmd(self, app: Client, message: types.Message, args: str):
"""Пригласить пользователя. Использование: invite <@ или ID или реплай>"""
chat = message.chat
if chat.type == "private":
return await utils.answer(
message, "<b>[ChatTools]</b> Это не чат!")
reply = message.reply_to_message
if not (args or reply):
return await utils.answer(
message, "<b>[ChatTools]</b> Нет аргументов или реплая")
try:
await chat.add_members(
args.split() if args else reply.from_user.id)
except errors.UserNotMutualContact:
return await utils.answer(
message, "<b>[ChatTools]</b> Невзаимный контакт")
except (errors.RPCError, Exception) as error:
return await utils.answer(
message, f"<b>[ChatTools]</b> Не удалось пригласить. Ошибка: {error}")
return await utils.answer(
message, "<b>[ChatTools]</b> Пользователь приглашен успешно")
async def users_cmd(self, app: Client, message: types.Message, args: str):
"""Показать список пользователей. Использование: users [имя] [!doc]>"""
chat = message.chat
if chat.type == "private":
return await utils.answer(
message, "<b>[ChatTools]</b> Это не чат!")
m = await utils.answer(
message, "<b>[ChatTools]</b> Загружаю...")
msg = [
(
"Удалённый аккаунт" if member.user.is_deleted
else member.user.mention
) + f" | <code>{member.user.id}</code>"
async for member in chat.iter_members(query=args.replace("!doc", ""))
]
text = (
f"<b>[ChatTools]</b> Пользователи в \"{chat.title}\"" + (
f" по запросу \"{args.replace('!doc', '')}\"" if args and args != "!doc"
else ""
) + f": {len(msg)}"
)
if "!doc" not in args:
msg = "\n".join(msg)
try:
return await utils.answer(
m, text + f"\n\n{msg}")
except errors.MessageTooLong:
m = await utils.answer(
m, "<b>[ChatTools]</b> Слишком много пользователей. Загружаю в файл...")
msg = "<br>\n\n".join(msg)
file = io.BytesIO(bytes(f"{text}<br><br>\n\n{msg}", "utf-8"))
file.name = f"users-{chat.id}.html"
file.seek(0)
await utils.answer(
message, file, doc=True, caption=text)
return await m[-1].delete()
async def admins_cmd(self, app: Client, message: types.Message, args: str):
"""Показать список админов. Использование: admins [имя] [!doc]"""
chat = message.chat
if chat.type == "private":
return await utils.answer(
message, "<b>[ChatTools]</b> Это не чат!")
m = await utils.answer(
message, "<b>[ChatTools]</b> Загружаю...")
msg = [
(
"Удалённый аккаунт" if member.user.is_deleted
else member.user.mention
) + f" | {member.title or 'admin'} | <code>{member.user.id}</code>"
async for member in chat.iter_members(
query=args.replace("!doc", ""), filter="administrators")
]
text = (
f"<b>[ChatTools]</b> Админы в \"{chat.title}\"" + (
f" по запросу \"{args.replace('!doc', '')}\"" if args and args != "!doc"
else ""
) + f": {len(msg)}"
)
if "!doc" not in args:
msg = "\n".join(msg)
try:
return await utils.answer(
m, text + f"\n\n{msg}")
except errors.MessageTooLong:
m = await utils.answer(
m, "<b>[ChatTools]</b> Слишком много пользователей. Загружаю в файл...")
msg = "<br>\n\n".join(msg)
file = io.BytesIO(bytes(f"{text}<br><br>\n\n{msg}", "utf-8"))
file.name = f"admins-{chat.id}.html"
file.seek(0)
await utils.answer(
message, file, doc=True, caption=text)
return await m[-1].delete()
async def bots_cmd(self, app: Client, message: types.Message, args: str):
"""Показать список ботов. Использование: bots [имя] [!doc]"""
chat = message.chat
if chat.type == "private":
return await utils.answer(
message, "<b>[ChatTools]</b> Это не чат!")
m = await utils.answer(
message, "<b>[ChatTools]</b> Загружаю...")
msg = [
(
"Удалённый аккаунт" if member.user.is_deleted
else member.user.mention
) + f" | <code>{member.user.id}</code>"
async for member in chat.iter_members(
query = args.replace("!doc", ""), filter = "bots")
]
text = (
f"<b>[ChatTools]</b> Боты в \"{chat.title}\"" + (
f" по запросу \"{args.replace('!doc', '')}\"" if args and args != "!doc"
else ""
) + f": {len(msg)}"
)
if "!doc" not in args:
msg = "\n".join(msg)
try:
return await utils.answer(
m, text + f"\n\n{msg}")
except errors.MessageTooLong:
m = await utils.answer(
m, "<b>[ChatTools]</b> Слишком много пользователей. Загружаю в файл...")
msg = "<br>\n\n".join(msg)
file = io.BytesIO(bytes(f"{text}<br><br>\n\n{msg}", "utf-8"))
file.name = f"bots-{chat.id}.html"
file.seek(0)
await utils.answer(
message, file, doc=True, caption=text)
return await m[-1].delete()