Exemple #1
0
def fed_chats(bot: Bot, update: Update, args: List[str]):
	chat = update.effective_chat  # type: Optional[Chat]
	user = update.effective_user  # type: Optional[User]
	fed_id = sql.get_fed_id(chat.id)
	info = sql.get_fed_info(fed_id)

	if not fed_id:
		update.effective_message.reply_text("This group is not a part of any federation!")
		return

	if is_user_fed_admin(fed_id, user.id) == False:
		update.effective_message.reply_text("Only federation admins can do this!")
		return

	getlist = sql.all_fed_chats(fed_id)
	if len(getlist) == 0:
		update.effective_message.reply_text("No users are fbanned from the federation {}".format(info['fname']), parse_mode=ParseMode.HTML)
		return

	text = "<b>New chat joined the federation {}:</b>\n".format(info['fname'])
	for chats in getlist:
		chat_name = sql.get_fed_name(chats)
		text += " • {} (<code>{}</code>)\n".format(chat_name, chats)

	try:
		update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)
	except:
		cleanr = re.compile('<.*?>')
		cleantext = re.sub(cleanr, '', text)
		with BytesIO(str.encode(cleantext)) as output:
			output.name = "fbanlist.txt"
			update.effective_message.reply_document(document=output, filename="fbanlist.txt",
													caption="Here is the list of all the chats in the federation {}.".format(info['fname']))
Exemple #2
0
def unfban(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]
    fed_id = sql.get_fed_id(chat.id)

    if is_user_fed_admin(fed_id, user.id) == False:
        update.effective_message.reply_text(
            tld(chat.id, "Only fed admins can do this!"))
        return

    user_id = extract_user(message, args)
    if not user_id:
        message.reply_text(
            tld(chat.id, "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(tld(chat.id, "That's not a user!"))
        return

    if sql.get_fban_user(fed_id, user_id) == False:
        message.reply_text(tld(chat.id, "This user is not fbanned!"))
        return

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

    message.reply_text(
        tld(chat.id,
            "I'll give {} a second chance in this federation.").format(
                user_chat.first_name))

    h = sql.all_fed_chats(fed_id)

    for O in h:
        try:
            member = bot.get_chat_member(O, user_id)
            if member.status == 'kicked':
                bot.unban_chat_member(O, user_id)

        except BadRequest as excp:

            if excp.message in UNFBAN_ERRORS:
                pass
            else:
                message.reply_text(
                    tld(chat.id,
                        "Could not un-fban due to: {}").format(excp.message))
                return

        except TelegramError:
            pass

        try:
            sql.un_fban_user(fed_id, user_id)
        except:
            pass

    message.reply_text(tld(chat.id, "Person has been un-fbanned."))
Exemple #3
0
def fed_broadcast(bot: Bot, update: Update, args: List[str]):
	msg = update.effective_message  # type: Optional[Message]
	user = update.effective_user  # type: Optional[User]
	if args:
		chat = update.effective_chat  # type: Optional[Chat]
		fed_id = sql.get_fed_id(chat.id)
		fedinfo = sql.get_fed_info(fed_id)
		text = "*New broadcast from the Federation {}*\n".format(fedinfo['fname'])
		# Parsing md
		raw_text = msg.text
		args = raw_text.split(None, 1)  # use python's maxsplit to separate cmd and args
		txt = args[1]
		offset = len(txt) - len(raw_text)  # set correct offset relative to command
		text_parser = markdown_parser(txt, entities=msg.parse_entities(), offset=offset)
		text += text_parser
		try:
			broadcaster = user.first_name
		except:
			broadcaster = user.first_name + " " + user.last_name
		text += "\n\n- {}".format(mention_markdown(user.id, broadcaster))
		chat_list = sql.all_fed_chats(fed_id)
		failed = 0
		for chat in chat_list:
			try:
				bot.sendMessage(chat, text, parse_mode="markdown")
			except TelegramError:
				failed += 1
				LOGGER.warning("Couldn't send broadcast to %s, group name %s", str(chat.chat_id), str(chat.chat_name))

		send_text = "The federation broadcast is complete!"
		if failed >= 1:
			send_text += "{} the group failed to receive the broadcast, probably because they left the federation.".format(failed)
		update.effective_message.reply_text(send_text)
Exemple #4
0
def fed_ban(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    fed_id = sql.get_fed_id(chat.id)

    if is_user_fed_admin(fed_id, user.id) == False:
        update.effective_message.reply_text("Only fed admins can do this!")
        return

    message = update.effective_message  # type: Optional[Message]

    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 user_id == bot.id:
        message.reply_text("You can't fban me, better hit your head against the wall, it's more fun.")
        return

    try:
        user_chat = 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

    
    message.reply_text("Start fbanning!")

    if reason == "":
        reason = "no reason"

    sql.fban_user(fed_id, user_id, reason)

    h = sql.all_fed_chats(fed_id)
    for O in h:
        try:
            bot.kick_chat_member(O, user_id)
            #text = tld(chat.id, "I should gban {}, but it's only test fban, right? So i let him live.").format(O)
            text = "Fbanning {}".format(user_id)
            #message.reply_text(text)
        except BadRequest as excp:
            if excp.message in FBAN_ERRORS:
                pass
            else:
                message.reply_text("Could not fban due to: {}").format(excp.message)
                return
        except TelegramError:
            pass
Exemple #5
0
def fed_info(bot: Bot, update: Update, args: List[str]):

    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    fed_id = sql.get_fed_id(chat.id)

    if not fed_id:
        update.effective_message.reply_text(
            tld(chat.id, "This group is not linked to any federation!"))
        return

    if is_user_fed_admin(fed_id, user.id) == False:
        update.effective_message.reply_text(
            tld(chat.id, "Only fed admins can do this!"))
        return

    print(fed_id)
    user = update.effective_user  # type: Optional[Chat]
    chat = update.effective_chat  # type: Optional[Chat]
    info = sql.get_fed_info(fed_id)

    text = "<b>Federation INFO:</b>"
    text += "\nName: <code>{}</code>".format(info.fed_name)
    text += "\nID: <code>{}</code>".format(fed_id)

    R = 0
    for O in sql.get_all_fban_users(fed_id):
        R = R + 1

    text += "\nBanned: <code>{}</code>".format(R)
    text += "\n\n<b>Chats:</b>"
    h = sql.all_fed_chats(fed_id)
    for O in h:
        cht = bot.get_chat(O)
        text += "\n• {} (<code>{}</code>)".format(cht.title, O)

    text += "\n\n<b>Admins:</b>"
    user = bot.get_chat(info.owner_id)
    text += "\n• {} - <code>{}</code> (Creator)".format(
        user.first_name, user.id)

    h = sql.all_fed_users(fed_id)
    for O in h:
        user = bot.get_chat(O)
        text += "\n• {} - <code>{}</code>".format(user.first_name, user.id, O)

    # Chance 1/5 to add this string to /fedinfo
    # You can remove this or reduce the percentage, but if you really like my work leave this.
    num = random.randint(1, 5)
    print("random ", num)
    if num == 3:
        text += "\n\nFederation by MrYacha for YanaBot"

    update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)
Exemple #6
0
def broadcast(bot: Bot, update: Update, args: List[str]):
    to_send = update.effective_message.text.split(None, 1)
    if len(to_send) >= 2:
        chat = update.effective_chat  # type: Optional[Chat]
        fed_id = sql.get_fed_id(chat.id)
        chats = sql.all_fed_chats(fed_id)
        failed = 0
        for Q in chats:
            try:
                bot.sendMessage(Q, to_send[1])
                sleep(0.1)
            except TelegramError:
                failed += 1
                LOGGER.warning("Couldn't send broadcast to %s, group name %s", str(chat.chat_id), str(chat.chat_name))

        update.effective_message.reply_text("Federations Broadcast complete. {} groups failed to receive the message, probably "
                                            "due to left federation.").format(failed)
Exemple #7
0
def fed_info(bot: Bot, update: Update, args: List[str]):

    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    fed_id = sql.get_fed_id(chat.id)
    info = sql.get_fed_info(fed_id)

    if not fed_id:
        update.effective_message.reply_text(
            tld(chat.id, "This group is not in any federation!"))
        return

    if is_user_fed_admin(fed_id, user.id) == False:
        update.effective_message.reply_text(
            tld(chat.id, "Only fed admins can do this!"))
        return

    OW = bot.get_chat(info.owner_id)
    HAHA = OW.id
    FEDADMIN = sql.all_fed_users(fed_id)
    FEDADMIN.append(int(HAHA))
    ACTUALADMIN = len(FEDADMIN)

    print(fed_id)
    user = update.effective_user  # type: Optional[Chat]
    chat = update.effective_chat  # type: Optional[Chat]
    info = sql.get_fed_info(fed_id)

    text = "<b>Fed info:</b>"
    text += "\nFedID: <code>{}</code>".format(fed_id)
    text += "\nName: {}".format(info.fed_name)
    text += "\nCreator: {}".format(mention_html(HAHA, "this guy"))
    text += "\nNumber of admins: <code>{}</code>".format(ACTUALADMIN)
    R = 0
    for O in sql.get_all_fban_users(fed_id):
        R = R + 1

    text += "\nNumber of bans: <code>{}</code>".format(R)
    h = sql.all_fed_chats(fed_id)
    asdf = len(h)
    text += "\nNumber of connected chats: <code>{}</code>".format(asdf)

    update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)
Exemple #8
0
def fed_info(bot: Bot, update: Update, args: List[str]):
	spam = spamfilters(update.effective_message.text, update.effective_message.from_user.id, update.effective_chat.id)
	if spam == True:
		return update.effective_message.reply_text("Spammer detected! Ignoring user.")

	chat = update.effective_chat  # type: Optional[Chat]
	user = update.effective_user  # type: Optional[User]
	fed_id = sql.get_fed_id(chat.id)
	info = sql.get_fed_info(fed_id)

	if not fed_id:
		update.effective_message.reply_text("This group is not in any federation!")
		return

	if is_user_fed_admin(fed_id, user.id) == False:
		update.effective_message.reply_text("Only a federation admin can do this!")
		return

	owner = bot.get_chat(info['owner'])
	try:
		owner_name = owner.first_name + " " + owner.last_name
	except:
		owner_name = owner.first_name
	FEDADMIN = sql.all_fed_users(fed_id)
	FEDADMIN.append(int(owner.id))
	TotalAdminFed = len(FEDADMIN)

	user = update.effective_user  # type: Optional[Chat]
	chat = update.effective_chat  # type: Optional[Chat]
	info = sql.get_fed_info(fed_id)

	text = "<b>ℹ️ Federation Information:</b>"
	text += "\nFedID: <code>{}</code>".format(fed_id)
	text += "\nName: {}".format(info['fname'])
	text += "\nCreator: {}".format(mention_html(owner.id, owner_name))
	text += "\nAll Admins: <code>{}</code>".format(TotalAdminFed)
	getfban = sql.get_all_fban_users(fed_id)
	text += "\nTotal banned users: <code>{}</code>".format(len(getfban))
	getfchat = sql.all_fed_chats(fed_id)
	text += "\nNumber of groups in this federation: <code>{}</code>".format(len(getfchat))

	update.effective_message.reply_text(text, parse_mode=ParseMode.HTML)
Exemple #9
0
def unfban(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    message = update.effective_message  # type: Optional[Message]
    fed_id = sql.get_fed_id(chat.id)

    if not fed_id:
        update.effective_message.reply_text(
            tld(chat.id, "This group is not in any federation!"))
        return

    info = sql.get_fed_info(fed_id)

    if is_user_fed_admin(fed_id, user.id) == False:
        update.effective_message.reply_text(
            tld(chat.id, "Only fed admins can do this!"))
        return

    user_id = extract_user(message, args)
    if not user_id:
        message.reply_text(
            tld(chat.id, "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(tld(chat.id, "That's not a user!"))
        return

    if sql.get_fban_user(fed_id, user_id) == False:
        message.reply_text(tld(chat.id, "This user is not fbanned!"))
        return

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

    message.reply_text(
        tld(chat.id,
            "I'll give {} a second chance in this federation.").format(
                user_chat.first_name))

    h = sql.all_fed_chats(fed_id)

    for O in h:
        try:
            member = bot.get_chat_member(O, user_id)
            if member.status == 'kicked':
                bot.unban_chat_member(O, user_id)

        except BadRequest as excp:

            if excp.message in UNFBAN_ERRORS:
                pass
            else:
                message.reply_text(
                    tld(chat.id,
                        "Could not un-fban due to: {}").format(excp.message))
                return

        except TelegramError:
            pass

        try:
            x = sql.un_fban_user(fed_id, user_id)
            if not x:
                message.reply_text(
                    tld(chat.id,
                        "Failed to fban, This user is probably fbanned!"))
                return
        except:
            pass

    message.reply_text(tld(chat.id, "Person has been un-fbanned."))

    OW = bot.get_chat(info.owner_id)
    HAHA = OW.id
    FEDADMIN = sql.all_fed_users(fed_id)
    FEDADMIN.append(int(HAHA))

    send_to_list(bot, FEDADMIN,
             "<b>Un-FedBan</b>" \
             "\n<b>Fed:</b> {}" \
             "\n<b>FedAdmin:</b> {}" \
             "\n<b>User:</b> {}" \
             "\n<b>User ID:</b> <code>{}</code>".format(info.fed_name, mention_html(user.id, user.first_name),
                                                 mention_html(user_chat.id, user_chat.first_name),
                                                              user_chat.id),
            html=True)
Exemple #10
0
def fed_ban(bot: Bot, update: Update, args: List[str]):
    chat = update.effective_chat  # type: Optional[Chat]
    user = update.effective_user  # type: Optional[User]
    fed_id = sql.get_fed_id(chat.id)

    if not fed_id:
        update.effective_message.reply_text(
            tld(chat.id, "This group is not in any federation!"))
        return

    info = sql.get_fed_info(fed_id)
    OW = bot.get_chat(info.owner_id)
    HAHA = OW.id
    FEDADMIN = sql.all_fed_users(fed_id)
    FEDADMIN.append(int(HAHA))

    if is_user_fed_admin(fed_id, user.id) == False:
        update.effective_message.reply_text(
            tld(chat.id, "Only fed admins can do this!"))
        return

    message = update.effective_message  # type: Optional[Message]

    user_id, reason = extract_user_and_text(message, args)

    fban = sql.get_fban_user(fed_id, user_id)
    if not fban == False:
        update.effective_message.reply_text(
            tld(chat.id, "This user already fbanned!"))
        return

    if not user_id:
        message.reply_text(
            tld(chat.id, "You don't seem to be referring to a user."))
        return

    if user_id == bot.id:
        message.reply_text(
            tld(
                chat.id,
                "You can't fban me, better hit your head against the wall, it's more fun."
            ))
        return

    if is_user_fed_owner(fed_id, user_id) == True:
        message.reply_text(
            tld(chat.id, "Why you are trying to fban the federation owner?"))
        return

    if is_user_fed_admin(fed_id, user_id) == True:
        message.reply_text(
            tld(chat.id,
                "Why so serious trying to fban the federation admin?"))
        return

    if user_id == OWNER_ID:
        message.reply_text(
            tld(chat.id,
                "I'm not fbanning my master, That's pretty dumb idea!"))
        return

    if int(user_id) in SUDO_USERS:
        message.reply_text(tld(chat.id, "I'm not fbanning the bot sudoers!"))
        return

    if int(user_id) in WHITELIST_USERS:
        message.reply_text(
            tld(chat.id, "This person is whitelisted from being fbanned!"))
        return

    try:
        user_chat = bot.get_chat(user_id)
    except BadRequest as excp:
        message.reply_text(excp.message)
        return

    if user_chat.type != 'private':
        message.reply_text(tld(chat.id, "That's not a user!"))
        return

    ok123 = mention_html(user_chat.id, user_chat.first_name)
    ok1234 = info.fed_name

    text12 = f"Beginning federation ban of {ok123} in {ok1234}."
    update.effective_message.reply_text(text12, parse_mode=ParseMode.HTML)

    if reason == "":
        reason = "No Reason."

    x = sql.fban_user(fed_id, user_id, reason)
    if not x:
        message.reply_text(
            "Failed to federation ban! Probably this bug is not fixed yet due to the developer is lazy as f**k."
        )
        return

    h = sql.all_fed_chats(fed_id)
    for O in h:
        try:
            bot.kick_chat_member(O, user_id)
            #text = tld(chat.id, "I should fban {}, but it's only test fban, right? So i let him live.").format(O)
            text = "Fbanning {}".format(user_id)
            #message.reply_text(text)
        except BadRequest as excp:
            if excp.message in FBAN_ERRORS:
                pass
            else:
                message.reply_text(
                    tld(chat.id,
                        "Could not fban due to: {}").format(excp.message))
                return
        except TelegramError:
            pass
    message.reply_text(tld(chat.id, "Person has been fbanned."))

    send_to_list(bot, FEDADMIN,
             "<b>New FedBan</b>" \
             "\n<b>Fed:</b> {}" \
             "\n<b>FedAdmin:</b> {}" \
             "\n<b>User:</b> {}" \
             "\n<b>User ID:</b> <code>{}</code>" \
             "\n<b>Reason:</b> {}".format(info.fed_name, mention_html(user.id, user.first_name),
                                   mention_html(user_chat.id, user_chat.first_name),
                                                user_chat.id, reason),
            html=True)
Exemple #11
0
def unfban(bot: Bot, update: Update, args: List[str]):
	chat = update.effective_chat  # type: Optional[Chat]
	user = update.effective_user  # type: Optional[User]
	message = update.effective_message  # type: Optional[Message]
	fed_id = sql.get_fed_id(chat.id)

	if not fed_id:
		update.effective_message.reply_text("This group is not a part of any federation!")
		return

	info = sql.get_fed_info(fed_id)

	if is_user_fed_admin(fed_id, user.id) == False:
		update.effective_message.reply_text("Only federation admins can do this!")
		return

	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

	fban, fbanreason = sql.get_fban_user(fed_id, user_id)
	if fban == False:
		message.reply_text("This user is not fbanned!")
		return

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

	message.reply_text("I'll give {} a second chance in this federation".format(mention_html(user_chat.id, user_chat.first_name)),
	parse_mode=ParseMode.HTML)

	chat_list = sql.all_fed_chats(fed_id)

	for chat in chat_list:
		try:
			member = bot.get_chat_member(chat, user_id)
			if member.status == 'kicked':
				bot.unban_chat_member(chat, user_id)
				"""
				bot.send_message(chat, "<b>Un-FedBan</b>" \
						 "\n<b>Federation:</b> {}" \
						 "\n<b>Federation Admin:</b> {}" \
						 "\n<b>User:</b> {}" \
						 "\n<b>User ID:</b> <code>{}</code>".format(info['fname'], mention_html(user.id, user.first_name), mention_html(user_chat.id, user_chat.first_name),
															user_chat.id), parse_mode="HTML")
				"""

		except BadRequest as excp:
			if excp.message in UNFBAN_ERRORS:
				pass
			else:
				LOGGER.warning("Cannot remove fban in {} because: {}".format(chat, excp.message))
		except TelegramError:
			pass

		try:
			x = sql.un_fban_user(fed_id, user_id)
			if not x:
				message.reply_text("Un-fban failure, this user may have been un-fbanned already!")
				return
		except:
			pass

	message.reply_text("{} has been un-fbanned.".format(mention_html(user_chat.id, user_chat.first_name)),
        parse_mode=ParseMode.HTML)
	FEDADMIN = sql.all_fed_users(fed_id)
Exemple #12
0
def fed_ban(bot: Bot, update: Update, args: List[str]):
	chat = update.effective_chat  # type: Optional[Chat]
	user = update.effective_user  # type: Optional[User]
	fed_id = sql.get_fed_id(chat.id)

	if not fed_id:
		update.effective_message.reply_text("This group is not a part of any federation!")
		return

	info = sql.get_fed_info(fed_id)
	OW = bot.get_chat(info['owner'])
	HAHA = OW.id
	FEDADMIN = sql.all_fed_users(fed_id)
	FEDADMIN.append(int(HAHA))

	if is_user_fed_admin(fed_id, user.id) == False:
		update.effective_message.reply_text("Only federation admins can do this!")
		return

	message = update.effective_message  # type: Optional[Message]

	user_id, reason = extract_user_and_text(message, args)

	fban, fbanreason = sql.get_fban_user(fed_id, user_id)

	if not user_id:
		message.reply_text("You don't seem to be referring to a user.")
		return

	if user_id == bot.id:
		message.reply_text("Nice try!")
		return

	if is_user_fed_owner(fed_id, user_id) == True:
		message.reply_text("You can't ban the federation owner!")
		return

	if is_user_fed_admin(fed_id, user_id) == True:
		message.reply_text("Why are you trying to ban a federation admin?")
		return

	if user_id == OWNER_ID:
		message.reply_text("I'm not gonna ban my owner!")
		return

	if int(user_id) in SUDO_USERS:
		message.reply_text("This person is sudo so I won't ban them!")
		return

	if int(user_id) in WHITELIST_USERS:
		message.reply_text("This person is whitelisted so I can't ban them!")
		return

	try:
		user_chat = 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 fban:
		user_target = mention_html(user_chat.id, user_chat.first_name)
		fed_name = info['fname']
		starting = "Starting FedBan for {} in the Federation <b>{}</b>is for\n".format(user_target, fed_name)
		update.effective_message.reply_text(starting, parse_mode=ParseMode.HTML)

		if reason == "":
			reason = "No reason given."

		temp = sql.un_fban_user(fed_id, user_id)
		if not temp:
			message.reply_text("Failed to update fban reason!")
			return
		x = sql.fban_user(fed_id, user_id, user_chat.first_name, user_chat.last_name, user_chat.username, reason)
		if not x:
			message.reply_text("Failed to ban from the federation! If this problem persists, reach out to us @tohsakas.")
			return

		fed_chats = sql.all_fed_chats(fed_id)
		for chat in fed_chats:
			try:
				bot.kick_chat_member(chat, user_id)
			except BadRequest as excp:
				if excp.message in FBAN_ERRORS:
					pass
				else:
					LOGGER.warning("Could not fban in {} because: {}".format(chat, excp.message))
			except TelegramError:
				pass

		send_to_list(bot, FEDADMIN,
				 "<b>FedBan reason updated</b>" \
							 "\n<b>Federation:</b> {}" \
							 "\n<b>Federation Admin:</b> {}" \
							 "\n<b>User:</b> {}" \
							 "\n<b>User ID:</b> <code>{}</code>" \
							 "\n<b>Reason:</b> {}".format(fed_name, mention_html(user.id, user.first_name),
									   mention_html(user_chat.id, user_chat.first_name),
													user_chat.id, reason), 
				html=True)
		message.reply_text("I've updated the FedBan reason!")
		return

	user_target = mention_html(user_chat.id, user_chat.first_name)
	fed_name = info['fname']

	starting = "Starting a federation ban for {} in the Federation <b>{}</b>.".format(user_target, fed_name)
	update.effective_message.reply_text(starting, parse_mode=ParseMode.HTML)

	if reason == "":
		reason = "No reason given."

	x = sql.fban_user(fed_id, user_id, user_chat.first_name, user_chat.last_name, user_chat.username, reason)
	if not x:
		message.reply_text("Failed to ban from the federation! If this problem persists, reach out to us @tohsakas.")
		return

	fed_chats = sql.all_fed_chats(fed_id)
	for chat in fed_chats:
		try:
			bot.kick_chat_member(chat, user_id)
		except BadRequest as excp:
			if excp.message in FBAN_ERRORS:
				try:
					dispatcher.bot.getChat(chat)
				except Unauthorized:
					sql.chat_leave_fed(chat)
					LOGGER.info("Chat {} has left fed {} because bot has been kicked.".format(chat, info['fname']))
					continue
			else:
				LOGGER.warning("Cannot fban in {} because: {}".format(chat, excp.message))
		except TelegramError:
			pass

	send_to_list(bot, FEDADMIN,
			 "<b>New FedBan</b>" \
			 "\n<b>Federation:</b> {}" \
			 "\n<b>Federation Admin:</b> {}" \
			 "\n<b>User:</b> {}" \
			 "\n<b>User ID:</b> <code>{}</code>" \
			 "\n<b>Reason:</b> {}".format(fed_name, mention_html(user.id, user.first_name),
								   mention_html(user_chat.id, user_chat.first_name),
												user_chat.id, reason), 
			html=True)
	message.reply_text("{} has been fbanned.".format(mention_html(user_chat.id, user_chat.first_name)),
	parse_mode=ParseMode.HTML)
Exemple #13
0
def fed_ban(bot: Bot, update: Update, args: List[str]):
	spam = spamfilters(update.effective_message.text, update.effective_message.from_user.id, update.effective_chat.id)
	if spam == True:
		return update.effective_message.reply_text("Spammer detected! Ignoring user.")

	chat = update.effective_chat  # type: Optional[Chat]
	user = update.effective_user  # type: Optional[User]
	fed_id = sql.get_fed_id(chat.id)

	if not fed_id:
		update.effective_message.reply_text("This group is not a part of any federation!")
		return

	info = sql.get_fed_info(fed_id)
	FEDADMIN = sql.all_fed_users(fed_id)
	for x in FEDADMIN:
		getreport = sql.user_feds_report(x)
		if getreport == False:
			FEDADMIN.remove(x)

	if is_user_fed_admin(fed_id, user.id) == False:
		update.effective_message.reply_text("Only federation admins can do this!")
		return

	message = update.effective_message  # type: Optional[Message]

	user_id, reason = extract_user_and_text(message, args)

	fban, fbanreason = sql.get_fban_user(fed_id, user_id)

	if not user_id:
		message.reply_text("You don't seem to be referring to a user")
		return

	if user_id == bot.id:
		message.reply_text("What is funnier than kicking the group creator? Self sacrifice.")
		return

	if is_user_fed_owner(fed_id, user_id) == True:
		message.reply_text("Why did you try the federation fban?")
		return

	if is_user_fed_admin(fed_id, user_id) == True:
		message.reply_text("He is a federation admin, I can't fban him.")
		return

	if user_id == OWNER_ID:
		message.reply_text("I don't want to block my master, that's a very stupid idea!")
		return

	if int(user_id) in SUDO_USERS:
		message.reply_text("I will not use sudo fban!")
		return

	if int(user_id) in WHITELIST_USERS:
		message.reply_text("This person is whitelisted, so they can't be fban!")
		return

	try:
		user_chat = 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 fban:
		user_target = mention_html(user_chat.id, user_chat.first_name)
		fed_name = info['fname']
		starting = "The reason fban is replaced for {} in the Federation <b>{}</b>.".format(user_target, fed_name)
		update.effective_message.reply_text(starting, parse_mode=ParseMode.HTML)

		if reason == "":
			reason = "No reason given."

		temp = sql.un_fban_user(fed_id, user_id)
		if not temp:
			message.reply_text("Failed to update the reason for fedban!")
			return
		x = sql.fban_user(fed_id, user_id, user_chat.first_name, user_chat.last_name, user_chat.username, reason)
		if not x:
			message.reply_text("Failed to ban from the federation! If this problem continues, contact @onepunchsupport.")
			return

		fed_chats = sql.all_fed_chats(fed_id)
		for chat in fed_chats:
			try:
				bot.send_message(chat, "<b>FedBan reason updated</b>" \
							 "\n<b>Federation:</b> {}" \
							 "\n<b>Federation Admin:</b> {}" \
							 "\n<b>User:</b> {}" \
							 "\n<b>User ID:</b> <code>{}</code>" \
							 "\n<b>Reason:</b> {}".format(fed_name, mention_html(user.id, user.first_name),
												   mention_html(user_chat.id, user_chat.first_name),
																user_chat.id, reason), parse_mode="HTML")
				bot.kick_chat_member(chat, user_id)
			except BadRequest as excp:
				if excp.message in FBAN_ERRORS:
					pass
				else:
					LOGGER.warning("Could not fban on {} because: {}".format(chat, excp.message))
			except TelegramError:
				pass

		send_to_list(bot, FEDADMIN,
				 "<b>FedBan reason updated</b>" \
							 "\n<b>Federation:</b> {}" \
							 "\n<b>Federation Admin:</b> {}" \
							 "\n<b>User:</b> {}" \
							 "\n<b>User ID:</b> <code>{}</code>" \
							 "\n<b>Reason:</b> {}".format(fed_name, mention_html(user.id, user.first_name),
									   mention_html(user_chat.id, user_chat.first_name),
													user_chat.id, reason), 
				html=True)
		message.reply_text("FedBan reason has been updated.")
		return

	user_target = mention_html(user_chat.id, user_chat.first_name)
	fed_name = info['fname']

	starting = "Starting a federation ban for {} in the Federation <b>{}</b>.".format(user_target, fed_name)
	update.effective_message.reply_text(starting, parse_mode=ParseMode.HTML)

	if reason == "":
		reason = "No reason given."

	x = sql.fban_user(fed_id, user_id, user_chat.first_name, user_chat.last_name, user_chat.username, reason)
	if not x:
		message.reply_text("Failed to ban from the federation! If this problem continues, contact @onepunchsupport.")
		return

	fed_chats = sql.all_fed_chats(fed_id)
	for chat in fed_chats:
		try:
			bot.send_message(chat, "<b>New FedBan</b>" \
						 "\n<b>Federation:</b> {}" \
						 "\n<b>Federation Admin:</b> {}" \
						 "\n<b>User:</b> {}" \
						 "\n<b>User ID:</b> <code>{}</code>" \
						 "\n<b>Reason:</b> {}".format(fed_name, mention_html(user.id, user.first_name),
											   mention_html(user_chat.id, user_chat.first_name),
															user_chat.id, reason), parse_mode="HTML")
			bot.kick_chat_member(chat, user_id)
		except BadRequest as excp:
			if excp.message in FBAN_ERRORS:
				pass
			else:
				LOGGER.warning("Cannot fban on {} because: {}".format(chat, excp.message))
		except TelegramError:
			pass

	send_to_list(bot, FEDADMIN,
			 "<b>New FedBan</b>" \
			 "\n<b>Federation:</b> {}" \
			 "\n<b>Federation Admin:</b> {}" \
			 "\n<b>User:</b> {}" \
			 "\n<b>User ID:</b> <code>{}</code>" \
			 "\n<b>Reason:</b> {}".format(fed_name, mention_html(user.id, user.first_name),
								   mention_html(user_chat.id, user_chat.first_name),
												user_chat.id, reason), 
			html=True)
	message.reply_text("This person has been fbanned")