def get_chat_member(client: Client, cid: int, uid: int) -> Union[bool, ChatMember, None]: # Get information about one member of a chat result = None try: result = client.get_chat_member(chat_id=cid, user_id=uid) except FloodWait as e: raise e except (ChannelInvalid, ChannelPrivate, PeerIdInvalid, UserNotParticipant): result = False except Exception as e: logger.warning(f"Get chat member {uid} in {cid} error: {e}", exc_info=True) return result
def get_super_groups(app: Client) -> Generator[Chat, None, None]: self_id = app.get_me().id for dialog in app.iter_dialogs(): dialog: Dialog chat = dialog.chat if chat.type != "supergroup": continue try: member = app.get_chat_member(chat.id, self_id) if not member.can_restrict_members: continue except Exception as e: print("#%s (%s): %s" % (chat.title, chat.id, repr(e))) continue yield chat
async def paste_code(_client: Client, message: Message): await message.delete() if commander_status in ('creator', 'administrator' ) or original_message_sender_id == commander_id: original_message_sender_id = message.reply_to_message.from_user.id commander_id = message.from_user.id commander_status = await (_client.get_chat_member( message.chat.id, commander_id)).status await message.reply_to_message.delete() await (message.reply_to_message.reply(await detect_exception( message.reply_to_message.text)))
async def banHammer(client: Client, message: Message): # /ban # /unban <username> global adminsIdList user = client.get_chat_member(message.chat.id, message.from_user.id) if user.can_restrict_members is False: await message.delete(revoke=True) return command = message.command.pop(0) if command == "unban" user = await client.get_users(message.command.pop(0)) await message.chat.unban_member(user.id) elif message.reply_to_message is not None and message.reply_to_message.from_user.id not in adminsIdList: user = message.reply_to_message.from_user await message.chat.kick_member(user.id) await message.reply_text("I have {}ned @{}.".format(command, user.username), quote=True) logger.info("I have {}ned @{}.".format(command, user.username))
def get_chat_member(client: Client, cid: int, uid: int) -> Union[bool, ChatMember, None]: # Get information about one member of a chat result = None try: flood_wait = True while flood_wait: flood_wait = False try: result = client.get_chat_member(chat_id=cid, user_id=uid) except FloodWait as e: flood_wait = True wait_flood(e) except UserNotParticipant: result = False except Exception as e: logger.warning(f"Get chat member {uid} in {cid} error: {e}", exc_info=True) return result
def on_user_join(client: Client, msg: Message) -> None: for user in msg.new_chat_members: if client.get_chat_member(msg.chat.id, user.id).can_send_messages == False: client.kick_chat_member(msg.chat.id, user.id, until_date=int(time.time()) + 36000) # 60*60*10 == 36000 == 10 hours msg.reply(f"Banned user {user.first_name} because they rejoined.") return client.restrict_chat_member( msg.chat.id, user.id, ChatPermissions( can_send_messages=False, can_change_info=False, can_invite_users=False, can_pin_messages=False, ), ) client.send_chat_action(msg.chat.id, "upload_photo") captchaSolution, captchaBytesIO = get_captcha() captchaBytesIO.name = "captcha.jpeg" correctEmoji = getattr(emoji, captchaSolution) msg.reply_photo( captchaBytesIO, caption= f"Welcome {user.first_name}\nYou must complete a captcha to chat.\nChoose the correct emoji.", reply_markup=get_keyboard( correctEmoji, user.id, ), )
chatID = input(Fore.CYAN + "[chat lookup]: " + Style.RESET_ALL) choice = input( Fore.CYAN + "[1] => Bulk search\n[2] => single user lookup\n[<]: " + Style.RESET_ALL) if choice == "1": limit = input(Fore.CYAN + "[# of users]: " + Style.RESET_ALL) members = app.get_chat_members(chat_id=int(chatID), limit=int(limit)) chatMembersInfoPrint(members) elif choice == "2": userInput = input( Fore.CYAN + "[user-id (id)/username (u)/phone number (pn)]: " + Style.RESET_ALL) members = app.get_chat_member(chat_id=int(chatID), user_id=userID(userInput)) chatMembersInfoPrint(members, total=False) elif choice == "2": userInput = input( Fore.CYAN + "[user-id (id)/username (u)/phone number (pn)]: " + Style.RESET_ALL) lookupResult = app.get_users([userID(userInput)]) singleUserLookup({'user': lookupResult[0]}) elif choice == "3": dialogs = app.get_dialogs() userInput = input( Fore.CYAN + "[user-id (id)/username (u)/phone number (pn)]: " + Style.RESET_ALL) for d in dialogs: if int(d['chat']['id']) < 0: try:
print(" ") print(" ! Given value leads to invalid chat " + str(chat)) continue except (InviteHashExpired): print(" ") print(" ! Given chat invite has expired " + str(chat)) continue print(" ") print(" - Deleting from chat " + str(chat)) print(" -- Chat ID: " + str(chatobj.id)) print(" -- Chat name: " + chatobj.title) # Check that we have delete perms try: memberobj = app.get_chat_member(chatobj.id, "self") assert memberobj.can_delete_messages is True except (UserNotParticipant): print(" ") print(" !! The user is not a participant in this chat") continue except (AssertionError): print(" ") print(" !! The user does not have delete perms in this chat") continue # Create empty array and variables messages = [] messagecount = 0 deleted = 0
if len(chats) == 0: print("No channels to monitor") exit(0) questions = [ inquirer.List( "chat", message="Select a channel to read from", choices=chats, ), ] answers = inquirer.prompt(questions) from_chat_id = answers["chat"] me = app.get_me() chats = [c for c in chats if c[1] != from_chat_id] for chat in chats: member = app.get_chat_member(chat[1], me.id) if member.status in ["creator", "administrator", "member"]: continue if member.status == "restricted" and member.can_send_messages: continue chats.remove(chat) if len(chats) == 0: print("No channels to write messages") exit(0) questions = [ inquirer.List( "chat", message="Select a channel to forward to", choices=chats, ),
from pyrogram import Client # Bot will leave all of non admin right group. app = Client("silencert") app.start() dialogs = app.iter_dialogs() me = app.get_me() for d in dialogs: try: member = app.get_chat_member(d.chat.id, me.id) if member.status != "administrator": print("LEAVE", d.chat.title) app.leave_chat(d.chat.id) else: print('STAY', d.chat.title) except Exception as e: print('ERROR', d.chat.title) app.stop()