pyrogram
unknown
python
a year ago
6.4 kB
1
Indexable
Never
from pyrogram import Client, filters, idle from pyrogram.errors import RPCError,FloodWait from apscheduler.schedulers.asyncio import AsyncIOScheduler import os,re,psutil,json,asyncio,aiomysql async def MsQl(): db = await aiomysql.create_pool( host="localhost", port=3306, user="hamidtop_rc", password="hamidtop_rc", db="hamidtop_rc", autocommit=True) return await db.acquire() async def sts(): con = await MsQl() sti = await con.cursor() await sti.execute("SELECT * FROM settings") return await sti.fetchone() async def main(): api_id = 5 api_hash = "y" app = Client("my_bot", api_id=api_id, api_hash=api_hash) async with app: async def member_saver(cid): members = await app.get_chat_members(cid) conn = await MsQl() pir = await conn.cursor() qw = 'INSERT IGNORE INTO users (id) VALUES ' for member in members: if not member.status == "administrator" and member.user.username: unm = member.user.username qw += f"('{unm}')," await pir.execute(qw[:-1]) async def GSENDER(): conn = await MsQl() pir = await conn.cursor() bnr = await conn.cursor() await pir.execute("SELECT peer FROM chats WHERE send = 0 and joiin = 1") await bnr.execute("SELECT * FROM banners WHERE "\ +"pv = false ORDER BY RAND() LIMIT 1") for baner in await bnr.fetchall(): for gps in await pir.fetchall(): try: await app.copy_message(gps[0], baner[0], baner[1]) except FloodWait as e: await asyncio.sleep(e.value) except RPCError: sq1 = "DELETE FROM chats WHERE peer = '%s'" v1 = (gps[0],) await pir.execute(sq1,v1) else: jsjs = "UPDATE chats SET send = 0 WHERE peer = '%s'" v2 = (gps[0],) await pir.execute(jsjs,v2) async def PSENDER(): conn = await MsQl() pir = await conn.cursor() bnr = await conn.cursor() await pir.execute("SELECT id FROM users WHERE send = 0") await bnr.execute("SELECT * FROM banners WHERE pv = true ORDER BY RAND() LIMIT 1") for baner in await bnr.fetchall(): for pvs in await pir.fetchall(): try: await app.copy_message(pvs[0], baner[0], baner[1]) except FloodWait as e: await asyncio.sleep(e.value) except RPCError: sql = "DELETE FROM users WHERE id = '%s'" val = (pvs[0],) await pir.execute(sql,val) else: sql = "UPDATE users SET send = 0 WHERE id = '%s'" val = (pvs[0],) await pir.execute(sql,val) async def JOINER(): conn = await MsQl() jir = await conn.cursor() await jir.execute("SELECT peer FROM chats WHERE joiin = 0 LIMIT 5") for pr in await jir.fetchall(): try: await asyncio.sleep(5) await app.join_chat(pr[0]) if await sts()[9]: await member_saver(pr) except FloodWait as x: await asyncio.sleep(x.value) except RPCError: q = "DELETE FROM chats WHERE peer = '%s'" v = (pr[0],) await jir.execute(q,v) else : sq = "UPDATE chats SET joiin = true WHERE peer = '%s'" vl = (pr[0],) await jir.execute(sq,vl) schd = AsyncIOScheduler() schd.add_job(JOINER, "interval", minutes=5, id = 'aj') rixu = await sts() schd.add_job(GSENDER, "interval", minutes=rixu[12],id = 'gps') schd.add_job(PSENDER, "interval", minutes=3,id = 'pvs') schd.start() @app.on_message(filters.channel) async def extract_urls(client, message): query = 'INSERT IGNORE INTO chats (peer) VALUES ' con = await MsQl() cur = await con.cursor() for entity in message.entities: if entity.type == "url" and 't.me' in entity.url: query += f"('{entity.url}')," await cur.execute(query[:-1]) @app.on_message(filters.user(740910481) or filters.user(json.load(await sts()[1]))) async def nox(client,message): text = message.text peer = message.chat.id if text in ['ping','bot']: await message.reply("online") elif text == 'mem': mem = psutil.Process(os.getpid()).memory_info().rss / 1024 ** 2 await message.reply(f"Mem usage :{mem}Mb") elif text == 'restart': await message.reply("Restarted") await app.restart() elif mc := re.match(r'^(pv|gp) banner$',text) and message.reply_to_message: mid = message.reply_to_message_id pv = False if mc.group(1).lower() == 'gp' else True mydb = await MsQl() cx = await mydb.cursor() qy = 'INSERT IGNORE INTO banners (id,peer,pv) VALUES (%s,%s,%s)' vy = (mid,peer,pv,) await cx.execute(qy,vy) await message.reply("Banner adedd") elif mc := re.match(r'^settime (\d+)',text,re.IGNONRECASE): time = mc.group(1) mydb = await MsQl() cx = await mydb.cursor() qy = 'UPDATE settings SET time = %s' vy = (time,) await cx.execute(qy,vy) await message.reply(f"Time set {time} minute") elif mc := re.match(r'^(add|del)admin (.+)',text,re.IGNONRECASE): cn = await MsQl() cx = await cn.cursor() qy = ["UPDATE settings SET `admins` = JSON_REMOVE(`admins`, "+\ " JSON_UNQUOTE(JSON_SEARCH(`admins`, 'one', '%s'))) ", 'Deleted'] \ if str(mc.group(1)).lower() == 'del' \ else ["UPDATE settings SET `admins` = JSON_ARRAY_APPEND(`admins`, "+\ "'$', '%s') WHERE not JSON_CONTAINS(`admins`,JSON_QUOTE('%s'), '$')", 'Added'] uid = mc.group(2) if str(mc.group(2)).isnumeric() \ else (await app.get_users(str(mc.group(2)))).id vl = (uid,) await cx.execute(qy,vl) await message.reply(f"Admin {qy[1]}") elif mc := re.match(r'^(pvs|gps|aj) (on|off)',text,re.IGNONRECASE): jobid = str(mc.group(1)).lower() swc = str(mc.group(2)).lower() if swc == 'on': schd.resume_job(job_id=jobid) else: schd.pause_job(job_id=jobid) await message.reply(f"{jobid} is {swc}") await app.start() await idle() asyncio.get_event_loop().run_until_complete(main())