Professor / plugins /broadcast.py
azils3's picture
Upload 47 files
bc96bf6 verified
raw
history blame
8.97 kB
import datetime, time, os, asyncio,logging
from pyrogram.errors import InputUserDeactivated, UserNotParticipant, FloodWait, UserIsBlocked, PeerIdInvalid
from pyrogram.errors.exceptions.bad_request_400 import MessageTooLong, PeerIdInvalid
from pyrogram.types import Message, InlineKeyboardButton
from pyrogram import Client, filters, enums
from database.users_chats_db import db
from info import ADMINS
@Client.on_message(filters.command("broadcast") & filters.user(ADMINS) & filters.reply)
async def broadcast(bot, message):
users = await db.get_all_users()
b_msg = message.reply_to_message
sts = await message.reply_text('Bʀᴏᴀᴅᴄᴀsᴛɪɴɢ Yᴏᴜʀ Mᴇssᴀɢᴇs...')
start_time = time.time()
total_users = await db.total_users_count()
done = 0
blocked = 0
deleted = 0
failed =0
success = 0
async for user in users:
pti, sh = await broadcast_messages(int(user['id']), b_msg)
if pti:
success += 1
elif pti == False:
if sh == "Blocked":
blocked+=1
elif sh == "Deleted":
deleted += 1
elif sh == "Error":
failed += 1
done += 1
if not done % 20:
await sts.edit(f"Bʀᴏᴀᴅᴄᴀsᴛ Iɴ Pʀᴏɢʀᴇss:\n\nTᴏᴛᴀʟ Uꜱᴇʀꜱ {total_users}\nCᴏᴍᴩʟᴇᴛᴇᴅ: {done} / {total_users}\nSᴜᴄᴄᴇꜱꜱ: {success}\nBʟᴏᴄᴋᴇᴅ: {blocked}\nDᴇʟᴇᴛᴇᴅ: {deleted}")
time_taken = datetime.timedelta(seconds=int(time.time()-start_time))
await sts.delete()
await bot.send_message(message.chat.id, f"Bʀᴏᴀᴅᴄᴀsᴛ Coᴍᴩʟᴇᴛᴇᴅ:\nTɪᴍᴇ Tᴀᴋᴇᴅ{time_taken} Sᴇᴄ\n\nTᴏᴛᴀʟ Uꜱᴇʀꜱ: {total_users}\nCᴏᴍᴩʟᴇᴛᴇᴅ: {done} / {total_users}\nSucᴄᴇꜱꜱ: {success}\nBʟᴏᴄᴋᴇᴅ: {blocked}\nDᴇʟᴇᴛᴇᴅ: {deleted}")
@Client.on_message(filters.command("clear_junk") & filters.user(ADMINS))
async def remove_junkuser__db(bot, message):
users = await db.get_all_users()
b_msg = message
sts = await message.reply_text('IN PROGRESS.......')
start_time = time.time()
total_users = await db.total_users_count()
blocked = 0
deleted = 0
failed = 0
done = 0
async for user in users:
pti, sh = await clear_junk(int(user['id']), b_msg)
if pti == False:
if sh == "Blocked":
blocked+=1
elif sh == "Deleted":
deleted += 1
elif sh == "Error":
failed += 1
done += 1
if not done % 20:
await sts.edit(f"In Progress:\n\nTotal Users {total_users}\nCompleted: {done} / {total_users}\nBlocked: {blocked}\nDeleted: {deleted}")
time_taken = datetime.timedelta(seconds=int(time.time()-start_time))
await sts.delete()
await bot.send_message(message.chat.id, f"Completed:\nCompleted in {time_taken} seconds.\n\nTotal Users {total_users}\nCompleted: {done} / {total_users}\nBlocked: {blocked}\nDeleted: {deleted}")
@Client.on_message(filters.command("group_broadcast") & filters.user(ADMINS) & filters.reply)
async def broadcast_group(bot, message):
groups = await db.get_all_chats()
b_msg = message.reply_to_message
sts = await message.reply_text(text='Broadcasting your messages To Groups...')
start_time = time.time()
total_groups = await db.total_chat_count()
done = 0
failed = ""
success = 0
deleted = 0
async for group in groups:
pti, sh, ex = await broadcast_messages_group(int(group['id']), b_msg)
if pti == True:
if sh == "Succes":
success += 1
elif pti == False:
if sh == "deleted":
deleted+=1
failed += ex
try:
await bot.leave_chat(int(group['id']))
except Exception as e:
print(f"{e} > {group['id']}")
done += 1
if not done % 20:
await sts.edit(f"Broadcast in progress:\n\nTotal Groups {total_groups}\nCompleted: {done} / {total_groups}\nSuccess: {success}\nDeleted: {deleted}")
time_taken = datetime.timedelta(seconds=int(time.time()-start_time))
await sts.delete()
try:
await message.reply_text(f"Broadcast Completed:\nCompleted in {time_taken} seconds.\n\nTotal Groups {total_groups}\nCompleted: {done} / {total_groups}\nSuccess: {success}\nDeleted: {deleted}\n\nFiled Reson:- {failed}")
except MessageTooLong:
with open('reason.txt', 'w+') as outfile:
outfile.write(failed)
await message.reply_document('reason.txt', caption=f"Completed:\nCompleted in {time_taken} seconds.\n\nTotal Groups {total_groups}\nCompleted: {done} / {total_groups}\nSuccess: {success}\nDeleted: {deleted}")
os.remove("reason.txt")
@Client.on_message(filters.command(["junk_group", "clear_junk_group"]) & filters.user(ADMINS))
async def junk_clear_group(bot, message):
groups = await db.get_all_chats()
b_msg = message
sts = await message.reply_text(text='..............')
start_time = time.time()
total_groups = await db.total_chat_count()
done = 0
failed = ""
deleted = 0
async for group in groups:
pti, sh, ex = await junk_group(int(group['id']), b_msg)
if pti == False:
if sh == "deleted":
deleted+=1
failed += ex
try:
await bot.leave_chat(int(group['id']))
except Exception as e:
print(f"{e} > {group['id']}")
done += 1
if not done % 20:
await sts.edit(f"in progress:\n\nTotal Groups {total_groups}\nCompleted: {done} / {total_groups}\nDeleted: {deleted}")
time_taken = datetime.timedelta(seconds=int(time.time()-start_time))
await sts.delete()
try:
await bot.send_message(message.chat.id, f"Completed:\nCompleted in {time_taken} seconds.\n\nTotal Groups {total_groups}\nCompleted: {done} / {total_groups}\nDeleted: {deleted}\n\nFiled Reson:- {failed}")
except MessageTooLong:
with open('junk.txt', 'w+') as outfile:
outfile.write(failed)
await message.reply_document('junk.txt', caption=f"Completed:\nCompleted in {time_taken} seconds.\n\nTotal Groups {total_groups}\nCompleted: {done} / {total_groups}\nDeleted: {deleted}")
os.remove("junk.txt")
async def broadcast_messages_group(chat_id, message):
try:
await message.copy(chat_id=chat_id)
return True, "Succes", 'mm'
except FloodWait as e:
await asyncio.sleep(e.value)
return await broadcast_messages_group(chat_id, message)
except Exception as e:
await db.delete_chat(int(chat_id))
logging.info(f"{chat_id} - PeerIdInvalid")
return False, "deleted", f'{e}\n\n'
async def junk_group(chat_id, message):
try:
kk = await message.copy(chat_id=chat_id)
await kk.delete(True)
return True, "Succes", 'mm'
except FloodWait as e:
await asyncio.sleep(e.value)
return await junk_group(chat_id, message)
except Exception as e:
await db.delete_chat(int(chat_id))
logging.info(f"{chat_id} - PeerIdInvalid")
return False, "deleted", f'{e}\n\n'
async def clear_junk(user_id, message):
try:
key = await message.copy(chat_id=user_id)
await key.delete(True)
return True, "Success"
except FloodWait as e:
await asyncio.sleep(e.value)
return await clear_junk(user_id, message)
except InputUserDeactivated:
await db.delete_user(int(user_id))
logging.info(f"{user_id}-Removed from Database, since deleted account.")
return False, "Deleted"
except UserIsBlocked:
logging.info(f"{user_id} -Blocked the bot.")
return False, "Blocked"
except PeerIdInvalid:
await db.delete_user(int(user_id))
logging.info(f"{user_id} - PeerIdInvalid")
return False, "Error"
except Exception as e:
return False, "Error"
async def broadcast_messages(user_id, message):
try:
await message.copy(chat_id=user_id)
return True, "Success"
except FloodWait as e:
await asyncio.sleep(e.value)
return await broadcast_messages(user_id, message)
except InputUserDeactivated:
await db.delete_user(int(user_id))
logging.info(f"{user_id}-Removed from Database, since deleted account.")
return False, "Deleted"
except UserIsBlocked:
logging.info(f"{user_id} -Blocked the bot.")
return False, "Blocked"
except PeerIdInvalid:
await db.delete_user(int(user_id))
logging.info(f"{user_id} - PeerIdInvalid")
return False, "Error"
except Exception as e:
return False, "Error"