def enforce_gmute(update, context): # Not using @restrict handler to avoid spamming - just ignore if cant gmute. if sql.does_chat_gmute( update.effective_chat.id) and update.effective_chat.get_member( context.bot.id).can_restrict_members: user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] msg = update.effective_message # type: Optional[Message] if user and not is_user_admin(chat, user.id): check_and_mute(update, user.id, should_message=True) if msg.new_chat_members: new_members = update.effective_message.new_chat_members for mem in new_members: check_and_mute(update, mem.id, should_message=True) if msg.reply_to_message: user = msg.reply_to_message.from_user # type: Optional[User] if user and not is_user_admin(chat, user.id): check_and_mute(update, user.id, should_message=True)
def gmutestat(update, context): args = context.args if len(args) > 0: if args[0].lower() in ["on", "yes"]: sql.enable_gmutes(update.effective_chat.id) update.effective_message.reply_text( "I've enabled gmutes in this group. This will help protect you " "from spammers, unsavoury characters, and Anirudh.") elif args[0].lower() in ["off", "no"]: sql.disable_gmutes(update.effective_chat.id) update.effective_message.reply_text( "I've disabled gmutes in this group. GMutes wont affect your users " "anymore. You'll be less protected from Anirudh though!") else: update.effective_message.reply_text( "Give me some arguments to choose a setting! on/off, yes/no!\n\n" "Your current setting is: {}\n" "When True, any gmutes that happen will also happen in your group. " "When False, they won't, leaving you at the possible mercy of " "spammers.".format(sql.does_chat_gmute(update.effective_chat.id)))
def gmute(update, context): message = update.effective_message # type: Optional[Message] chat = update.effective_chat args = context.args user_id, reason = extract_user_and_text(message, args) if not user_id: message.reply_text("You don't seem to be referring to a user.") return if int(user_id) in OFFICERS: message.reply_text("I Can't Gmute My Sudo Users .") return if user_id == context.bot.id: message.reply_text("I can't gmute myself.") return if not reason: message.reply_text( "Please give a reason why are you want to gmute this user!") return try: user_chat = context.bot.get_chat(user_id) except BadRequest as excp: message.reply_text(excp.message) return if user_chat.type != 'private': message.reply_text("That's not a user!") return if sql.is_user_gmuted(user_id): if not reason: message.reply_text( "This user is already gmuted; I'd change the reason, but you haven't given me one..." ) return success = sql.update_gmute_reason( user_id, user_chat.username or user_chat.first_name, reason) if success: message.reply_text( "This user is already gmuted; I've gone and updated the gmute reason though!" ) else: message.reply_text("I thought this person was gmuted.") return message.reply_text("Gets duct tape ready 😉") muter = update.effective_user # type: Optional[User] sql.gmute_user(user_id, user_chat.username or user_chat.first_name, reason) chats = get_all_chats() for chat in chats: chat_id = chat.chat_id # Check if this group has disabled gmutes if not sql.does_chat_gmute(chat_id): continue try: context.bot.restrict_chat_member( chat_id, user_id, permissions=ChatPermissions(can_send_messages=False)) except BadRequest as excp: if excp.message == "User is an administrator of the chat": pass elif excp.message == "Chat not found": pass elif excp.message == "Not enough rights to restrict/unrestrict chat member": pass elif excp.message == "User_not_participant": pass elif excp.message == "Peer_id_invalid": # Suspect this happens when a group is suspended by telegram. pass elif excp.message == "Group chat was deactivated": pass elif excp.message == "Need to be inviter of a user to kick it from a basic group": pass elif excp.message == "Chat_admin_required": pass elif excp.message == "Only the creator of a basic group can kick group administrators": pass elif excp.message == "Method is available only for supergroups": pass elif excp.message == "Can't demote chat creator": pass else: message.reply_text("Unexpected Error!") context.bot.send_message( ERROR_DUMP, "Could not gmute due to: {}".format(excp.message)) sql.ungmute_user(user_id) return except TelegramError: pass message.reply_text("They won't be talking again anytime soon.")
def ungmute(update, context): message = update.effective_message # type: Optional[Message] bot = context.bot args = context.args user_id = extract_user(message, args) if not user_id: message.reply_text("You don't seem to be referring to a user.") return user_chat = bot.get_chat(user_id) if user_chat.type != 'private': message.reply_text("That's not a user!") return if not sql.is_user_gmuted(user_id): message.reply_text("This user is not gmuted!") return muter = update.effective_user # type: Optional[User] message.reply_text("I'll let {} speak again, globally.".format( user_chat.first_name)) chats = get_all_chats() for chat in chats: chat_id = chat.chat_id # Check if this group has disabled gmutes if not sql.does_chat_gmute(chat_id): continue try: member = context.bot.get_chat_member(chat_id, user_id) if member.status == 'restricted': context.bot.restrict_chat_member( chat_id, int(user_id), permissions=ChatPermissions( can_send_messages=True, can_invite_users=True, can_pin_messages=True, can_send_polls=True, can_change_info=True, can_send_media_messages=True, can_send_other_messages=True, can_add_web_page_previews=True, )) except BadRequest as excp: if excp.message == "User is an administrator of the chat": pass elif excp.message == "Chat not found": pass elif excp.message == "Not enough rights to restrict/unrestrict chat member": pass elif excp.message == "User_not_participant": pass elif excp.message == "Method is available for supergroup and channel chats only": pass elif excp.message == "Not in the chat": pass elif excp.message == "Channel_private": pass elif excp.message == "Chat_admin_required": pass else: message.reply_text("Unexpected Error!") bot.send_message( ERROR_DUMP, "Could not un-gmute due to: {}".format(excp.message)) return except TelegramError: pass sql.ungmute_user(user_id) message.reply_text("Person has been un-gmuted.")