Beispiel #1
0
def enforce_gmute(update: Update, context: CallbackContext):
    # 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]
        gmute_alert = sql.get_gmute_alert(chat.id)
        new_members = update.effective_message.new_chat_members

        if user and not is_user_admin(chat, user.id):
            check_and_mute(update, context, user.id)
            
        if msg.text:
            if user and not is_user_admin(chat, user.id):
                check_and_mute(update, context, user.id)
                
                if gmute_alert:
                    gmute_notification(update, context, user.id,
                                  should_message=True)
                
        elif msg.new_chat_members:
            for mem in new_members:
                welcome_gmute(update, context, mem.id)
                if gmute_alert:
                    gmute_notification(update, context, mem.id,
                                  should_message=True)
                    
        elif new_members:
            for mem in new_members:
                welcome_gmute(update, context, mem.id)
                if gmute_alert:
                    gmute_notification(update, context, user.id,
                                  should_message=True)
Beispiel #2
0
def gmutestat(update: Update, context: CallbackContext):
    chat = update.effective_chat
    args = update.effective_message.text.split(" ")
    args_option = ""
    
    if len(args) > 1:
        args_option = args[1].lower()
        
    if len(args) >= 2:
        if args_option != "" and args_option in ["on", "yes"]:
            sql.enable_gmutes(update.effective_chat.id)
            update.effective_message.reply_text("I've enabled gmutes for {}. This will help protect you "
                                                "from spammers, unsavoury characters, and "
                                                "the biggest trolls.".format(chat.title))
        elif args_option != "" and args_option in ["off", "no"]:
            sql.disable_gmutes(update.effective_chat.id)
            update.effective_message.reply_text("I've disabled gmutes for {}. Global mute wont affect your users "
                                                "anymore. You'll be less protected from any trolls and spammers "
                                                "though!".format(chat.title))
    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)))
Beispiel #3
0
def fmute(update: Update, context: CallbackContext):
    message = update.effective_message  # type: Optional[Message]
    user = update.effective_user  # type: Optional[User]
    args = message.text.split(" ")
    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 SUDO_USERS:
        message.reply_text("You're playing with fire! Sudo war is catastrophic.")
        return

    if int(user_id) in SUPPORT_USERS:
        message.reply_text("I can't silence a support user! Only my creator can!")
        return
    
    if int(user_id) in SUPER_ADMINS:
        message.reply_text("This is one of the super admin users appointed by the hierarchy. "
                           "Therefore, I can't touch this user!")
        return

    if user_id == context.bot.id:
        message.reply_text("Only my creator can decide the fate!")
        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:
            msg = "User {} is already globally muted; I'd change the reason, " \
                  "but you haven't given me one...".format(mention_html(user_chat.id, user_chat.first_name 
                                                                        or "Deleted Account"))

            message.reply_text(msg, parse_mode=ParseMode.HTML)
            return

        old_reason = sql.update_gmute_reason(user_id, user_chat.username or user_chat.first_name, reason)
        user_id, new_reason = extract_user_and_text(message, args)
        
        if old_reason == new_reason:
            same_reason = "User {} has already been globally muted, with the " \
                          "exact same reason.".format(mention_html(user_chat.id, user_chat.first_name or  
                                                                   "Deleted Account"))
            
            message.reply_text(same_reason, parse_mode=ParseMode.HTML)
            return
        
        if old_reason:
            muter = update.effective_user  # type: Optional[User]
            send_gmute = "<b>Emendation of Global mute</b>" \
                        "\n#GMUTE" \
                        "\n<b>Status:</b> <code>Amended</code>" \
                        "\n<b>Name:</b> {}".format(mention_html(user_chat.id, user_chat.first_name or 
                                                                "Deleted Account"))
    
            if user_chat.last_name:
                send_gmute += "\n<b>Surname:</b> {}".format(mention_html(user_chat.id, user_chat.last_name))
            
            if user_chat.username:
                send_gmute += "\n<b>Username:</b> @{}".format(html.escape(user_chat.username))
            
            if  user_chat:
                send_gmute += "\n<b>ID:</b> <code>{}</code>".format(user_chat.id)
            
            if muter.id in SUDO_USERS:
                send_gmute += "\n<b>Sudo:</b> {}".format(mention_html(muter.id, muter.first_name))
            
            if muter.id in SUPPORT_USERS:
                send_gmute += "\n<b>Support:</b> {}".format(mention_html(muter.id, muter.first_name))
            
            if reason:
                send_gmute += "\n<b>Previous:</b> {}".format(old_reason)
                send_gmute += "\n<b>Amended:</b> {}".format(new_reason)
            
            context.bot.send_message(chat_id=GBAN_LOG, text=send_gmute, parse_mode=ParseMode.HTML)
            old_msg = "User {} is already globally muted, for the following reason:\n" \
                      "<code>{}</code>\n" \
                      "I've gone and updated it with your new reason!".format(mention_html(user_chat.id, 
                                                                              user_chat.first_name or "Deleted Account"), 
                                                                              old_reason)

            message.reply_text(old_msg, parse_mode=ParseMode.HTML)
        
        else:
            muter = update.effective_user  # type: Optional[User]
            send_gmute = "<b>Emendation of Global mute</b>" \
                        "\n#GMUTE" \
                        "\n<b>Status:</b> <code>New reason</code>" \
                        "\n<b>Name:</b> {}".format(mention_html(user_chat.id, user_chat.first_name or 
                                                                "Deleted Account"))
    
            if user_chat.last_name:
                send_gmute += "\n<b>Surname:</b> {}".format(mention_html(user_chat.id, user_chat.last_name))
            
            if user_chat.username:
                send_gmute += "\n<b>Username:</b> @{}".format(html.escape(user_chat.username))
            
            if  user_chat:
                send_gmute += "\n<b>ID:</b> <code>{}</code>".format(user_chat.id)
            
            if muter.id in SUDO_USERS:
                send_gmute += "\n<b>Sudo:</b> {}".format(mention_html(muter.id, muter.first_name))
            
            if muter.id in SUPPORT_USERS:
                send_gmute += "\n<b>Support:</b> {}".format(mention_html(muter.id, muter.first_name))
            
            if reason:
                send_gmute += "\n<b>Reason:</b> {}".format(new_reason)
            
            context.bot.send_message(chat_id=GBAN_LOG, text=send_gmute, parse_mode=ParseMode.HTML)
            msg = "User {} is already globally muted, but had no reason set; " \
                  "I've gone and updated it!".format(mention_html(user_chat.id, user_chat.first_name or "Deleted Account"))

            message.reply_text(msg, parse_mode=ParseMode.HTML)

        return

    starting = "Starting global mutes for {}...".format(mention_html(user_chat.id, user_chat.first_name or "Deleted Account"))

    message.reply_text(starting, parse_mode=ParseMode.HTML)
    
    muter = update.effective_user  # type: Optional[User]
    send_gmute = "<b>Global mute</b>" \
                "\n#GMUTE" \
                "\n<b>Status:</b> <code>Enforced</code>" \
                "\n<b>Name:</b> {}".format(mention_html(user_chat.id, user_chat.first_name or "Deleted Account"))
    
    if user_chat.last_name:
        send_gmute += "\n<b>Surname:</b> {}".format(mention_html(user_chat.id, user_chat.last_name))
    
    if user_chat.username:
        send_gmute += "\n<b>Username:</b> @{}".format(html.escape(user_chat.username))
    
    if  user_chat:
        send_gmute += "\n<b>ID:</b> <code>{}</code>".format(user_chat.id)
    
    if muter.id in SUDO_USERS:
        send_gmute += "\n<b>Sudo:</b> {}".format(mention_html(muter.id, muter.first_name))
    
    if muter.id in SUPPORT_USERS:
        send_gmute += "\n<b>Support:</b> {}".format(mention_html(muter.id, muter.first_name))
    
    if reason:
        send_gmute += "\n<b>Reason:</b> {}".format(reason)
             

    context.bot.send_message(chat_id=GBAN_LOG, text=send_gmute, parse_mode=ParseMode.HTML)
    sql.gmute_user(user_id, user_chat.username or user_chat.first_name, reason)
    
    fmute_id = update.effective_chat  # type: Optional[Chat]
    chats = get_users_by_chat(fmute_id.id, user_id)
    for chat in chats:
        chat_id = chat.chat

        # Check if this group has disabled gmutes
        if not sql.does_chat_gmute(chat_id):
            continue

        try:
            context.bot.restrict_chat_member(update.effective_chat.id, int(user_id), MUTE_PERMISSIONS)
        except BadRequest as excp:
            if excp.message in GMUTE_ERRORS:
                pass
            else:
                message.reply_text("Could not fmute due to: {}".format(excp.message))
                sql.ungmute_user(user_id)
                return
        except TelegramError:
            pass
    
    silence_t = "User {} has been successfully globally muted".format(mention_html(user_chat.id, user_chat.first_name or 
                                                               "Deleted Account"))

    context.bot.send_message(chat_id=GBAN_LOG, text=silence_t, parse_mode=ParseMode.HTML)
    message.reply_text("User {} has been globally muted!".format(mention_html(user_chat.id, user_chat.first_name 
                                                            or "Deleted Account")), parse_mode=ParseMode.HTML)
Beispiel #4
0
def __chat_settings__(chat_id, user_id):
    return "This chat is enforcing *gmutes*: `{}`.".format(sql.does_chat_gmute(chat_id))
Beispiel #5
0
def ungmute(update: Update, context: CallbackContext):
    message = update.effective_message  # type: Optional[Message]
    user = update.effective_user  # type: Optional[User]
    args = message.text.split(" ")

    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 = context.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):
        msg = "User {} is not globally muted!".format(mention_html(user_chat.id, user_chat.first_name 
                                                                         or "Deleted Account"))
        
        message.reply_text(msg, parse_mode=ParseMode.HTML)
        return

    muter = update.effective_user  # type: Optional[User]

    message.reply_text("Removing {} from global mutes.".format(user_chat.first_name or "Deleted Account"))

    send_gmute = "<b>Regression of Global mute</b>" \
                "\n#GMUTE" \
                "\n<b>Status:</b> <code>Ceased</code>" \
                "\n<b>Name:</b> {}".format(mention_html(user_chat.id, user_chat.first_name or "Deleted Account"))
    
    if user_chat.last_name:
        send_gmute += "\n<b>Surname:</b> {}".format(mention_html(user_chat.id, user_chat.last_name))
    
    if user_chat.username:
        send_gmute += "\n<b>Username:</b> @{}".format(html.escape(user_chat.username))
    
    if  user_chat:
        send_gmute += "\n<b>ID:</b> <code>{}</code>".format(user_chat.id)
    
    if muter.id in SUDO_USERS:
        send_gmute += "\n<b>Sudo:</b> {}".format(mention_html(muter.id, muter.first_name))
    
    if muter.id in SUPPORT_USERS:
        send_gmute += "\n<b>Support:</b> {}".format(mention_html(muter.id, muter.first_name))
            
   
    context.bot.send_message(chat_id=GBAN_LOG, text=send_gmute, parse_mode=ParseMode.HTML)
    
    unfmute_id = update.effective_chat  # type: Optional[Chat]
    chats = get_users_by_chat(unfmute_id.id, user_id)
    for chat in chats:
        chat_id = chat.chat

        # 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 == 'kicked':
                context.bot.restrict_chat_member(update.effective_chat.id, int(user_id), UNMUTE_PERMISSIONS)

        except BadRequest as excp:
            if excp.message in UNGMUTE_ERRORS:
                pass
            else:
                message.reply_text("Could not un-gmute due to: {}".format(excp.message))
                context.bot.send_message(OWNER_ID, "Could not un-gmute due to: {}".format(excp.message))
                return
        except TelegramError:
            pass

    sql.ungmute_user(user_id)
    
    silence_t = "User {} has been pardoned from global mutes.".format(mention_html(user_chat.id, user_chat.first_name or 
                                                                                     "Deleted Account"))
    
    context.bot.send_message(chat_id=GBAN_LOG, text=silence_t, parse_mode=ParseMode.HTML)
    message.reply_text("User {} has been pardoned from global mutes.".format(mention_html(user_chat.id, user_chat.first_name 
                                                               or "Deleted Account")), parse_mode=ParseMode.HTML)