Files
homelab/modules/misc/search.py
T
forust 7ba6bc44f2 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.)
2026-06-19 12:19:01 +02:00

71 lines
2.5 KiB
Python

from asyncio import sleep
from pyrogram import Client, enums, filters
from pyrogram.types import Message
# noinspection PyUnresolvedReferences
from utils.misc import modules_help, prefix
# noinspection PyUnresolvedReferences
from utils.scripts import format_exc
now = {}
@Client.on_message(filters.command(['search'], prefix) & filters.me)
async def search_cmd(client: Client, message: Message):
if now.get(message.chat.id):
return await message.edit(
f'<b>You already have a search in progress!\nType: <code>{prefix}scancel</code> to cancel it.</b>',
parse_mode=enums.ParseMode.HTML,
)
await message.edit('<b>Start searching...</b>', parse_mode=enums.ParseMode.HTML)
finished = False
local = False
try:
cmd = message.command[1]
word = message.command[2].lower()
timeout = float(message.command[3]) if len(message.command) > 3 else 2
except:
return await message.edit(
f'<b>Usage:</b> <code>{prefix}search [/cmd]* [search_word]* [timeout=2.0]</code>',
parse_mode=enums.ParseMode.HTML,
)
now[message.chat.id] = True
try:
await message.reply_text(quote=False, text=cmd, reply_to_message_id=None)
while not finished and now[message.chat.id]:
async for msg in client.get_chat_history(message.chat.id, limit=2):
if msg.from_user.id == message.from_user.id:
continue
elif word in msg.text.lower():
finished = True
local = True
if not local:
await sleep(timeout)
await message.reply_text(quote=False, text=cmd, reply_to_message_id=None)
else:
break
if now[message.chat.id]:
await message.reply_text('<b>Search finished!</b>', parse_mode=enums.ParseMode.HTML)
except Exception as ex:
await message.edit(format_exc(ex), parse_mode=enums.ParseMode.HTML)
now[message.chat.id] = False
@Client.on_message(filters.command(['scancel'], prefix) & filters.me)
async def scancel_cmd(_: Client, message: Message):
if not now.get(message.chat.id):
return await message.edit('<b>There is no search in progress!</b>', parse_mode=enums.ParseMode.HTML)
now[message.chat.id] = False
await message.edit('<b>Search cancelled!</b>', parse_mode=enums.ParseMode.HTML)
modules_help['search'] = {
'search [/cmd]* [search_word]* [timeout=2.0]': 'Search for a specific word in bot (while)',
'scancel': 'Cancel current search',
}