Captain Ezio commited on
Commit
6e5ac37
·
1 Parent(s): 3c3977c

Updated few things

Browse files
Files changed (2) hide show
  1. Powers/plugins/info.py +64 -41
  2. Powers/utils/caching.py +7 -9
Powers/plugins/info.py CHANGED
@@ -11,6 +11,9 @@ from Powers.utils.custom_filters import command
11
  from Powers.utils.extract_user import extract_user
12
  from Powers import (
13
  LOGGER, DEV_USERS, SUDO_USERS, SUPPORT_STAFF, WHITELIST_USERS)
 
 
 
14
 
15
 
16
  gban_db = GBan()
@@ -51,7 +54,8 @@ async def count(c: Gojo, chat):
51
  total_bot = (
52
  total_admin
53
  ) = bot_admin = total_banned = "Can't fetch due to some error."
54
- return total_bot, total_admin, bot_admin, total_banned
 
55
 
56
 
57
  async def user_info(c: Gojo, user, already=False):
@@ -197,9 +201,7 @@ async def info_func(c: Gojo, message: Message):
197
 
198
  try:
199
  info_caption, photo_id = await user_info(c, user)
200
- LOGGER.info(
201
- f"{message.from_user.id} tried to fetch user info of user {message.from_user.id} in {message.chat.id}"
202
- )
203
  except Exception as e:
204
  LOGGER.error(e)
205
  LOGGER.error(format_exc())
@@ -213,61 +215,82 @@ async def info_func(c: Gojo, message: Message):
213
 
214
  await m.delete()
215
  await sleep(2)
216
- await message.reply_photo(photo, caption=info_caption, quote=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
217
  os.remove(photo)
218
- LOGGER.info(
219
- f"{message.from_user.id} fetched user info of user {user_name} in {m.chat.id}"
220
- )
221
 
 
222
 
223
  @Gojo.on_message(command(["chinfo", "chatinfo", "chat_info"]))
224
  async def chat_info_func(c: Gojo, message: Message):
225
  splited = message.text.split()
226
- try:
227
- if len(splited) == 1:
228
- chat = message.chat.id
229
 
230
- else:
231
- chat = splited[1]
232
 
233
- try:
234
- chat = int(chat)
235
- except (ValueError, Exception) as ef:
236
- if "invalid literal for int() with base 10:" in str(ef):
237
- chat = str(chat)
238
- else:
239
- return await message.reply_text(
240
- f"Got and exception {e}\n**Usage:**/chinfo [USERNAME|ID]"
241
- )
242
-
243
- m = await message.reply_text(
244
- f"Fetching chat info of chat **{message.chat.title}**....."
245
- )
246
 
247
- info_caption, photo_id = await chat_info(c, chat=chat)
248
- if not photo_id:
249
- await m.delete()
250
- await sleep(2)
251
- return await message.reply_text(info_caption, disable_web_page_preview=True)
252
 
253
- photo = await c.download_media(photo_id)
 
254
  await m.delete()
255
  await sleep(2)
256
- if len(info_caption) >= 1024:
257
- x = await message.reply_photo(photo)
258
- await x.reply_text(info_caption)
259
- else:
260
- await message.reply_photo(photo, caption=info_caption, quote=False)
261
- LOGGER.info(
262
- f"{message.from_user.id} fetched chat info of chat {chat} in {message.chat.id}"
263
- )
264
 
265
- os.remove(photo)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266
  except Exception as e:
267
  await message.reply_text(text=e)
268
  LOGGER.error(e)
269
  LOGGER.error(format_exc())
270
 
 
 
 
 
271
 
272
  __PLUGIN__ = "info"
273
  __alt_name__ = [
 
11
  from Powers.utils.extract_user import extract_user
12
  from Powers import (
13
  LOGGER, DEV_USERS, SUDO_USERS, SUPPORT_STAFF, WHITELIST_USERS)
14
+ from pyrogram.errors import (
15
+ RPCError, MessageTooLong, EntityBoundsInvalid, MediaCaptionTooLong)
16
+
17
 
18
 
19
  gban_db = GBan()
 
54
  total_bot = (
55
  total_admin
56
  ) = bot_admin = total_banned = "Can't fetch due to some error."
57
+
58
+ return total_bot, total_admin, bot_admin, total_banned
59
 
60
 
61
  async def user_info(c: Gojo, user, already=False):
 
201
 
202
  try:
203
  info_caption, photo_id = await user_info(c, user)
204
+
 
 
205
  except Exception as e:
206
  LOGGER.error(e)
207
  LOGGER.error(format_exc())
 
215
 
216
  await m.delete()
217
  await sleep(2)
218
+ try:
219
+ await message.reply_photo(photo, caption=info_caption, quote=False)
220
+ except MediaCaptionTooLong:
221
+ x = await message.reply_photo(photo)
222
+ try:
223
+ await x.reply_text(info_caption)
224
+ except EntityBoundsInvalid:
225
+ await x.delete()
226
+ await message.reply_text(info_caption)
227
+ except RPCError as rpc:
228
+ await message.reply_text(rpc)
229
+ LOGGER.error(rpc)
230
+ LOGGER.error(format_exc())
231
+ except Exception as e:
232
+ await message.reply_text(text=e)
233
+ LOGGER.error(e)
234
+ LOGGER.error(format_exc())
235
+
236
  os.remove(photo)
 
 
 
237
 
238
+ return
239
 
240
  @Gojo.on_message(command(["chinfo", "chatinfo", "chat_info"]))
241
  async def chat_info_func(c: Gojo, message: Message):
242
  splited = message.text.split()
243
+ if len(splited) == 1:
244
+ chat = message.chat.id
 
245
 
246
+ else:
247
+ chat = splited[1]
248
 
249
+ try:
250
+ chat = int(chat)
251
+ except (ValueError, Exception) as ef:
252
+ if "invalid literal for int() with base 10:" in str(ef):
253
+ chat = str(chat)
254
+ else:
255
+ return await message.reply_text(
256
+ f"Got and exception {e}\n**Usage:**/chinfo [USERNAME|ID]"
257
+ )
 
 
 
 
258
 
259
+ m = await message.reply_text(
260
+ f"Fetching chat info of chat **{message.chat.title}**....."
261
+ )
 
 
262
 
263
+ info_caption, photo_id = await chat_info(c, chat=chat)
264
+ if not photo_id:
265
  await m.delete()
266
  await sleep(2)
267
+ return await message.reply_text(info_caption, disable_web_page_preview=True)
 
 
 
 
 
 
 
268
 
269
+ photo = await c.download_media(photo_id)
270
+ await m.delete()
271
+ await sleep(2)
272
+ try:
273
+ await message.reply_photo(photo, caption=info_caption, quote=False)
274
+ except MediaCaptionTooLong:
275
+ x = await message.reply_photo(photo)
276
+ try:
277
+ await x.reply_text(info_caption)
278
+ except EntityBoundsInvalid:
279
+ await x.delete()
280
+ await message.reply_text(info_caption)
281
+ except RPCError as rpc:
282
+ await message.reply_text(rpc)
283
+ LOGGER.error(e)
284
+ LOGGER.error(format_exc())
285
  except Exception as e:
286
  await message.reply_text(text=e)
287
  LOGGER.error(e)
288
  LOGGER.error(format_exc())
289
 
290
+ os.remove(photo)
291
+
292
+ return
293
+
294
 
295
  __PLUGIN__ = "info"
296
  __alt_name__ = [
Powers/utils/caching.py CHANGED
@@ -1,6 +1,6 @@
1
  from typing import List
2
  from Powers import LOGGER
3
- from pyrogram import enums
4
  from threading import RLock
5
  from cachetools import TTLCache
6
  from time import time, perf_counter
@@ -18,18 +18,16 @@ TEMP_ADMIN_CACHE_BLOCK = TTLCache(maxsize=512, ttl=(60 * 10), timer=perf_counter
18
 
19
  async def admin_cache_reload(m: Message or CallbackQuery, status=None) -> List[int]:
20
  start = time()
21
- with THREAD_LOCK:
22
 
23
  if isinstance(m, CallbackQuery):
24
  m = m.message
25
-
26
- global ADMIN_CACHE, TEMP_ADMIN_CACHE_BLOCK
27
  if status is not None:
28
  TEMP_ADMIN_CACHE_BLOCK[m.chat.id] = status
29
 
30
  try:
31
  if TEMP_ADMIN_CACHE_BLOCK[m.chat.id] in ("autoblock", "manualblock"):
32
- return
33
  except KeyError:
34
  # Because it might be first time when admn_list is being reloaded
35
  pass
@@ -37,13 +35,13 @@ async def admin_cache_reload(m: Message or CallbackQuery, status=None) -> List[i
37
  admin_list = [
38
  (
39
  z.user.id,
40
- (("@" + z.user.username) if z.user.username else z.user.first_name),
41
- )
42
- async for z in m.chat.get_members(
43
- filter=enums.ChatMembersFilter.ADMINISTRATORS
44
  )
 
45
  if not z.user.is_deleted
46
  ]
 
47
  ADMIN_CACHE[m.chat.id] = admin_list
48
  LOGGER.info(
49
  f"Loaded admins for chat {m.chat.id} in {round((time() - start), 3)}s due to '{status}'",
 
1
  from typing import List
2
  from Powers import LOGGER
3
+ from pyrogram.enums import ChatMembersFilter
4
  from threading import RLock
5
  from cachetools import TTLCache
6
  from time import time, perf_counter
 
18
 
19
  async def admin_cache_reload(m: Message or CallbackQuery, status=None) -> List[int]:
20
  start = time()
21
+ async with THREAD_LOCK:
22
 
23
  if isinstance(m, CallbackQuery):
24
  m = m.message
 
 
25
  if status is not None:
26
  TEMP_ADMIN_CACHE_BLOCK[m.chat.id] = status
27
 
28
  try:
29
  if TEMP_ADMIN_CACHE_BLOCK[m.chat.id] in ("autoblock", "manualblock"):
30
+ return []
31
  except KeyError:
32
  # Because it might be first time when admn_list is being reloaded
33
  pass
 
35
  admin_list = [
36
  (
37
  z.user.id,
38
+ f"@{z.user.username}" if z.user.username else z.user.first_name,
39
+ z.is_anonymous,
 
 
40
  )
41
+ async for z in m.chat.get_members(filter=ChatMembersFilter.ADMINISTRATORS)
42
  if not z.user.is_deleted
43
  ]
44
+
45
  ADMIN_CACHE[m.chat.id] = admin_list
46
  LOGGER.info(
47
  f"Loaded admins for chat {m.chat.id} in {round((time() - start), 3)}s due to '{status}'",