def CmdBlock(client: pyrogram.Client, msg: pyrogram.Message): users_to_block = list() if msg.reply_to_message: if msg.reply_to_message.forward_from: users_to_block.append(msg.reply_to_message.forward_from.id) elif msg.reply_to_message.text.find("(#user") != -1: users_to_block.append( int(msg.reply_to_message. text[msg.reply_to_message.text.find("(#user") + 6:msg.reply_to_message.text.find(")")])) else: msg.command.remove(msg.command[0]) users_to_block = filter(utils.IsInt, msg.command) txt = "" for usr in users_to_block: user: db_management.Users = db_management.Users.get_or_none(id=usr) if user: user.is_blocked = True user.save() txt += f"(#user{usr.id}) {usr.first_name}\n" client.send_message(chat_id=usr.id, text="You have been blocked.") msg.reply_text(text=f"Blocked users:\n{txt}", disable_notification=False) msg.stop_propagation()
def CmdStart_HelpMaster(client: pyrogram.Client, msg: pyrogram.Message): msg.reply_text( text="""<code>/start</code> Shows this message <code>/test {reply_from}|{chats}</code> Tests the specified chat(s) <code>/getlast</code> Sends last user's details <code>/exec</code> Executes Python3 code <code>/eval</code> Evaluates Python3 expression <code>/backup</code> Makes and sends backup <code>/reboot</code> Reboots bot <code>/getip</code> Sends server's IP <code>/block {reply_from}|{users}</code> Blocks the specified user(s) <code>/unblock {reply_from}|{users}</code> Unblocks the specified user(s)""", disable_notification=False, parse_mode="html", ) msg.stop_propagation()
def CmdTestChat(client: pyrogram.Client, msg: pyrogram.Message): chats_to_test = list() if msg.reply_to_message: if msg.reply_to_message.forward_from: chats_to_test.append(msg.reply_to_message.forward_from.id) elif msg.reply_to_message.text.find("(#user") != -1: chats_to_test.append( int(msg.reply_to_message. text[msg.reply_to_message.text.find("(#user") + 6:msg.reply_to_message.text.find(")")])) else: msg.command.remove(msg.command[0]) chats_to_test = filter(utils.IsInt, msg.command) txt = "" for cht in chats_to_test: try: client.send_chat_action(chat_id=int(cht), action="typing") txt += f"Can write to {cht}\n" except pyrogram.errors.UserIsBlocked: txt += f"{cht} blocked me\n" except pyrogram.errors.PeerIdInvalid: txt += f"Cannot write to {cht}, never encountered.\n" except Exception as ex: txt += f"Cannot write to {cht} {ex}\n" msg.reply_text(text=txt, disable_notification=False) msg.stop_propagation()
def CmdGetLastUser(client: pyrogram.Client, msg: pyrogram.Message): global last_user msg.reply_text( text="[{0}] {1}, @{2} (#user{3})".format( datetime.datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S"), last_user.first_name, last_user.username, last_user.id, ), disable_notification=False, ) msg.stop_propagation()
def CmdFileManager(client: pyrogram.Client, msg: pyrogram.Message): msg.reply_text( text="Path: " + utils.config["file_manager"]["path"], reply_markup=pyrogram.InlineKeyboardMarkup( keyboards.BuildItemsKeyboard( path=utils.config["file_manager"]["path"], page=utils.config["file_manager"]["page"], max_columns=utils.config["file_manager"]["max_columns"], max_rows=utils.config["file_manager"]["max_rows"], ) ), ) msg.stop_propagation()
def CmdEval(client: pyrogram.Client, msg: pyrogram.Message): expression = msg.text[len(msg.command[0]) + 2:] if expression: text = None try: text = str(eval(expression, dict(client=client, msg=msg))) except Exception as error: text = str(error) if text: if len(text) > 4096: file_name = f"./downloads/message_too_long_{secrets.token_hex(5)}_{time.time()}.txt" with open(file_name, "w", encoding="utf-8") as f: f.write(text) msg.reply_document(document=file_name) os.remove(file_name) else: msg.reply_text(text=text) msg.stop_propagation()
def PreProcessMessage(client: pyrogram.Client, msg: pyrogram.Message): # as this is the first handler of this type, if the db is locked wait while db_management.DB.is_stopped(): time.sleep(1) if db_management.Users.get_or_none(id=msg.from_user.id): db_management.Users.update( first_name=msg.from_user.first_name if msg.from_user.first_name else "", last_name=msg.from_user.last_name, username=msg.from_user.username, timestamp=datetime.datetime.utcnow(), ).where(db_management.Users.id == msg.from_user.id).execute() else: db_management.Users.create( id=msg.from_user.id, first_name=msg.from_user.first_name if msg.from_user.first_name else "", last_name=msg.from_user.last_name, username=msg.from_user.username, ) if db_management.Users.get_or_none(id=msg.from_user.id).is_blocked: # do not process messages for blocked users msg.stop_propagation()
def MessagesAntiFlood(client: pyrogram.Client, msg: pyrogram.Message): if msg.from_user.id == utils.config["master"]: return flooder = False utils.InstantiateFloodDictionary(msg.from_user.id) # take the current time timestamp_ = time.time() if len(utils.flood[msg.from_user.id]["times"]) > 4: # check if 5+ messages(recorded times) in less than 5 seconds if timestamp_ - utils.flood[msg.from_user.id]["times"][0] <= 5: flooder = True # remove oldest message(recorded time) utils.flood[msg.from_user.id]["times"].pop(0) # append last message(recorded time) utils.flood[msg.from_user.id]["times"].append(timestamp_) # if now this chat is out of the flood_wait time continue if timestamp_ >= utils.flood[msg.from_user.id]["flood_wait_expiry_date"]: if flooder: print(f"FLOODER: {msg.from_user.id}") utils.flood[msg.from_user.id]["flood_wait_minutes"] = 1 # is the chat flooding inside a two minutes window after the previous flood_wait_expiry_date? if (utils.flood[msg.from_user.id]["flood_wait_expiry_date"] != 0 and timestamp_ <= utils.flood[msg.from_user.id]["flood_wait_expiry_date"] + 120): # add one minute to the previous flood_wait time utils.flood[msg.from_user.id]["flood_wait_minutes"] += 1 # transform into seconds and add current time to have an expiry date utils.flood[msg.from_user.id]["flood_wait_expiry_date"] = ( timestamp_ + utils.flood[msg.from_user.id]["flood_wait_minutes"] * 60) if not utils.flood[msg.from_user.id]["warned"]: utils.flood[msg.from_user.id]["warned"] = True # wait two seconds to give the warn message as the last one due to multiple workers time.sleep(2) msg.reply_text( text= "You are flooding, the bot will not forward your messages for {0} minute(s)." .format( utils.flood[msg.from_user.id]["flood_wait_minutes"]), disable_notification=False, ) client.send_message( chat_id=utils.config["master"], text="(#user{0}) {1} is limited for flood for {2} minute(s)." .format( msg.from_user.id, msg.from_user.first_name, utils.flood[msg.from_user.id]["flood_wait_minutes"], ), disable_notification=False, ) # do not process messages for flooders msg.stop_propagation() else: # reset user data utils.flood[msg.from_user.id]["warned"] = False utils.flood[msg.from_user.id]["flood_wait_minutes"] = 0 utils.flood[msg.from_user.id]["flood_wait_expiry_date"] = 0 else: # do not process messages for flooders msg.stop_propagation() print("[UTC {0}] {1}, @{2} ({3}): {4}\n".format( datetime.datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S"), msg.from_user.first_name, msg.from_user.username, msg.from_user.id, msg.text if not msg.media else f"MEDIA {utils.ExtractMedia(msg=msg).file_id}", ))
def CmdBackup(client: pyrogram.Client, msg: pyrogram.Message): utils.SendBackup(client=client) msg.stop_propagation()
def CmdGetIP(client: pyrogram.Client, msg: pyrogram.Message): ip = urllib.request.urlopen("https://ipecho.net/plain").read().decode( "utf8") msg.reply_text(text=ip) msg.stop_propagation()
def CmdReboot(client: pyrogram.Client, msg: pyrogram.Message): python = sys.executable db_management.DB.close() os.execl(python, python, *sys.argv) msg.stop_propagation()