def warns(bot: Bot, update: Update, args: List[str]): message = update.effective_message chat = update.effective_chat user_id = extract_user(message, args) or update.effective_user.id result = sql.get_warns(user_id, chat.id) num = 1 if result and result[0] != 0: num_warns, reasons = result limit, soft_warn = sql.get_warn_setting(chat.id) if reasons: text = tld(chat.id, 'warns_list_warns').format(num_warns, limit) for reason in reasons: text += "\n {}. {}".format(num, reason) num += 1 msgs = split_message(text) for msg in msgs: update.effective_message.reply_text(msg) else: update.effective_message.reply_text( tld(chat.id, 'warns_list_warns_no_reason').format(num_warns, limit)) else: update.effective_message.reply_text( tld(chat.id, 'warns_list_warns_none'))
def get_id(bot: Bot, update: Update, args: List[str]): user_id = extract_user(update.effective_message, args) chat = update.effective_chat # type: Optional[Chat] if user_id: if update.effective_message.reply_to_message and update.effective_message.reply_to_message.forward_from: user1 = update.effective_message.reply_to_message.from_user user2 = update.effective_message.reply_to_message.forward_from update.effective_message.reply_markdown( tld(chat.id, "misc_get_id_1").format(escape_markdown(user2.first_name), user2.id, escape_markdown(user1.first_name), user1.id)) else: user = bot.get_chat(user_id) update.effective_message.reply_markdown( tld(chat.id, "misc_get_id_2").format(escape_markdown(user.first_name), user.id)) else: chat = update.effective_chat # type: Optional[Chat] if chat.type == "private": update.effective_message.reply_markdown( tld(chat.id, "misc_id_1").format(chat.id)) else: update.effective_message.reply_markdown( tld(chat.id, "misc_id_2").format(chat.id))
def promote(bot: Bot, update: Update, args: List[str]) -> str: message = update.effective_message user = update.effective_user chat = update.effective_chat conn = connected(bot, update, chat, user.id) if conn: chatD = dispatcher.bot.getChat(conn) else: chatD = update.effective_chat if chat.type == "private": return if not chatD.get_member(bot.id).can_promote_members: update.effective_message.reply_text(tld(chat.id, "admin_err_no_perm")) return member = chatD.get_member(user.id) if not member.can_promote_members and member.status != 'creator': update.effective_message.reply_text( tld(chat.id, "admin_err_user_no_perm")) return "" user_id = extract_user(message, args) if not user_id: message.reply_text(tld(chat.id, "common_err_no_user")) return user_member = chatD.get_member(user_id) if user_member.status == 'administrator' or user_member.status == 'creator': message.reply_text(tld(chat.id, "admin_err_user_admin")) return if user_id == bot.id: message.reply_text(tld(chat.id, "admin_err_self_promote")) return # set same perms as bot - bot can't assign higher perms than itself! bot_member = chatD.get_member(bot.id) bot.promoteChatMember(chatD.id, user_id, can_change_info=bot_member.can_change_info, can_post_messages=bot_member.can_post_messages, can_edit_messages=bot_member.can_edit_messages, can_delete_messages=bot_member.can_delete_messages, can_invite_users=bot_member.can_invite_users, can_restrict_members=bot_member.can_restrict_members, can_pin_messages=bot_member.can_pin_messages, can_promote_members=bot_member.can_promote_members) message.reply_text(tld(chat.id, "admin_promote_success").format( mention_html(user.id, user.first_name), mention_html(user_member.user.id, user_member.user.first_name), html.escape(chatD.title)), parse_mode=ParseMode.HTML) return f"<b>{html.escape(chatD.title)}:</b>" \ "\n#PROMOTED" \ f"\n<b>Admin:</b> {mention_html(user.id, user.first_name)}" \ f"\n<b>User:</b> {mention_html(user_member.user.id, user_member.user.first_name)}"
def mute(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat user = update.effective_user message = update.effective_message conn = connected(bot, update, chat, user.id) if conn: chatD = dispatcher.bot.getChat(conn) else: if chat.type == "private": return else: chatD = chat user_id = extract_user(message, args) if not user_id: message.reply_text(tld(chat.id, "mute_invalid")) return "" if user_id == bot.id: message.reply_text(tld(chat.id, "mute_not_myself")) return "" member = chatD.get_member(int(user_id)) if member: if user_id in SUDO_USERS: message.reply_text(tld(chat.id, "mute_not_sudo")) elif is_user_admin(chatD, user_id, member=member): message.reply_text(tld(chat.id, "mute_not_m_admin")) elif member.can_send_messages is None or member.can_send_messages: bot.restrict_chat_member(chatD.id, user_id, can_send_messages=False) keyboard = [] reply = tld(chat.id, "mute_success").format( mention_html(member.user.id, member.user.first_name), chatD.title) message.reply_text(reply, reply_markup=keyboard, parse_mode=ParseMode.HTML) return "<b>{}:</b>" \ "\n#MUTE" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</b> {}".format(html.escape(chatD.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name)) else: message.reply_text( tld(chat.id, "mute_already_mute").format(chatD.title)) else: message.reply_text( tld(chat.id, "mute_not_in_chat").format(chatD.title)) return ""
def nomedia(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat user = update.effective_user message = update.effective_message conn = connected(bot, update, chat, user.id) if conn: chatD = dispatcher.bot.getChat(conn) else: if chat.type == "private": return else: chatD = chat user_id = extract_user(message, args) if not user_id: message.reply_text(tld(chat.id, "restrict_invalid")) return "" if user_id == bot.id: message.reply_text(tld(chat.id, "restrict_is_bot")) return "" member = chatD.get_member(int(user_id)) if member: if is_user_admin(chatD, user_id, member=member): message.reply_text(tld(chat.id, "restrict_is_admin")) elif member.can_send_messages is None or member.can_send_messages: bot.restrict_chat_member(chatD.id, user_id, can_send_messages=True, can_send_media_messages=False, can_send_other_messages=False, can_add_web_page_previews=False) keyboard = [] reply = tld(chat.id, "restrict_success").format( mention_html(member.user.id, member.user.first_name), chatD.title) message.reply_text(reply, reply_markup=keyboard, parse_mode=ParseMode.HTML) return "<b>{}:</b>" \ "\n#RESTRICTED" \ "\n<b>• Admin:</b> {}" \ "\n<b>• User:</b> {}" \ "\n<b>• ID:</b> <code>{}</code>".format(html.escape(chatD.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), user_id) else: message.reply_text(tld(chat.id, "restrict_already_restricted")) else: message.reply_text( tld(chat.id, "mute_not_in_chat").format(chatD.title)) return ""
def reset_warns(update, context): message = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] args = context.args user_id = extract_user(message, args) conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": send_message(update.effective_message, "You can do this command in groups, not PM") return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title check = context.bot.getChatMember(chat_id, context.bot.id) if check.status == 'member' or check['can_restrict_members'] == False: if conn: text = "I can't restrect people on {}! Make sure I'm already an admin.".format( chat_name) else: text = "I can't restrect people in here! Make sure I'm already an admin." send_message(update.effective_message, text, parse_mode=ParseMode.HTML) return "" if user_id and user_id != "error": warned = chat.get_member(user_id).user sql.reset_warns(user_id, chat.id) if conn: send_message(update.effective_message, "Warnings have been reset in *{}*!".format(chat_name), parse_mode=ParseMode.HTML) else: send_message(update.effective_message, "User {} warnings have been reset!".format( mention_html(warned.id, warned.first_name)), parse_mode=ParseMode.HTML) return "<b>{}:</b>" \ "\n#RESETWARNS" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</b> {} (<code>{}</code>)".format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(warned.id, warned.first_name), warned.id) else: send_message(update.effective_message, "No user was designated!") return ""
def get_id(update, context): args = context.args user_id = extract_user(update.effective_message, args) if user_id and user_id != "error": if update.effective_message.reply_to_message and update.effective_message.reply_to_message.forward_from: user1 = update.effective_message.reply_to_message.from_user user2 = update.effective_message.reply_to_message.forward_from text = "The original sender, {}, has an ID of `{}`.\nThe forwarder, {}, has an ID of `{}`.".format( escape_markdown(user2.first_name), user2.id, escape_markdown(user1.first_name), user1.id) if update.effective_message.chat.type != "private": text += "\n" + "This group's id is `{}`.".format( update.effective_message.chat.id) send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN) else: user = context.bot.get_chat(user_id) text = "{}'s id is `{}`.".format(escape_markdown(user.first_name), user.id) if update.effective_message.chat.type != "private": text += "\n" + "This group's id is `{}`.".format( update.effective_message.chat.id) send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN) elif user_id == "error": try: user = context.bot.get_chat(args[0]) except BadRequest: send_message(update.effective_message, "Error: Unknown User/Chat!") return text = "Your id is `{}`.".format(update.effective_message.from_user.id) text += "\n" + "This group's id is `{}`.".format(user.id) if update.effective_message.chat.type != "private": text += "\n" + "This group's id is `{}`.".format( update.effective_message.chat.id) send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN) else: chat = update.effective_chat # type: Optional[Chat] if chat.type == "private": send_message(update.effective_message, "Your id is `{}`.".format( update.effective_message.from_user.id), parse_mode=ParseMode.MARKDOWN) else: send_message(update.effective_message, "Your id is `{}`.".format( update.effective_message.from_user.id) + "\n" + "This group's id is `{}`.".format(chat.id), parse_mode=ParseMode.MARKDOWN)
def slap(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat msg = update.effective_message # reply to correct message reply_text = msg.reply_to_message.reply_text if msg.reply_to_message else msg.reply_text # get user who sent message if msg.from_user.username: curr_user = "******" + escape_markdown(msg.from_user.username) else: curr_user = "******".format(msg.from_user.first_name, msg.from_user.id) user_id = extract_user(update.effective_message, args) if user_id: slapped_user = bot.get_chat(user_id) user1 = curr_user if slapped_user.username == "Destroyer32": reply_text(tld(chat.id, "memes_not_doing_that")) return if slapped_user.username: user2 = "@" + escape_markdown(slapped_user.username) else: user2 = "[{}](tg://user?id={})".format(slapped_user.first_name, slapped_user.id) # if no target found, bot targets the sender else: user1 = "[{}](tg://user?id={})".format(bot.first_name, bot.id) user2 = curr_user temp = random.choice(tld_list(chat.id, "memes_slaps_templates_list")) item = random.choice(tld_list(chat.id, "memes_items_list")) hit = random.choice(tld_list(chat.id, "memes_hit_list")) throw = random.choice(tld_list(chat.id, "memes_throw_list")) itemp = random.choice(tld_list(chat.id, "memes_items_list")) itemr = random.choice(tld_list(chat.id, "memes_items_list")) repl = temp.format(user1=user1, user2=user2, item=item, hits=hit, throws=throw, itemp=itemp, itemr=itemr) reply_text(repl, parse_mode=ParseMode.MARKDOWN)
def reset_warns(bot: Bot, update: Update, args: List[str]) -> str: message = update.effective_message chat = update.effective_chat user = update.effective_user user_id = extract_user(message, args) if user_id: sql.reset_warns(user_id, chat.id) message.reply_text(tld(chat.id, 'warns_reset_success')) warned = chat.get_member(user_id).user return tld(chat.id, 'warns_reset_log_channel').format( html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(warned.id, warned.first_name), warned.id) else: message.reply_text(tld(chat.id, 'common_err_no_user')) return ""
def user_join_fed(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat user = update.effective_user msg = update.effective_message fed_id = sql.get_fed_id(chat.id) if is_user_fed_owner(fed_id, user.id): user_id = extract_user(msg, args) if user_id: user = bot.get_chat(user_id) elif not msg.reply_to_message and not args: user = msg.from_user elif not msg.reply_to_message and ( not args or (len(args) >= 1 and not args[0].startswith("@") and not args[0].isdigit() and not msg.parse_entities([MessageEntity.TEXT_MENTION]))): msg.reply_text(tld(chat.id, "common_err_no_user")) return else: LOGGER.warning('error') getuser = sql.search_user_in_fed(fed_id, user_id) fed_id = sql.get_fed_id(chat.id) info = sql.get_fed_info(fed_id) get_owner = eval(info['fusers'])['owner'] get_owner = bot.get_chat(get_owner).id if user_id == get_owner: update.effective_message.reply_text( tld(chat.id, "feds_promote_owner")) return if getuser: update.effective_message.reply_text( tld(chat.id, "feds_promote_owner")) return if user_id == bot.id: update.effective_message.reply_text( tld(chat.id, "feds_promote_bot")) return res = sql.user_join_fed(fed_id, user_id) if res: update.effective_message.reply_text( tld(chat.id, "feds_promote_success")) else: update.effective_message.reply_text("") else: update.effective_message.reply_text(tld(chat.id, "feds_owner_only"))
def user_demote_fed(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat user = update.effective_user fed_id = sql.get_fed_id(chat.id) if is_user_fed_owner(fed_id, user.id): msg = update.effective_message user_id = extract_user(msg, args) if user_id: user = bot.get_chat(user_id) elif not msg.reply_to_message and not args: user = msg.from_user elif not msg.reply_to_message and ( not args or (len(args) >= 1 and not args[0].startswith("@") and not args[0].isdigit() and not msg.parse_entities([MessageEntity.TEXT_MENTION]))): msg.reply_text(tld(chat.id, "common_err_no_user")) return else: LOGGER.warning('error') if user_id == bot.id: update.effective_message.reply_text(tld(chat.id, "feds_demote_bot")) return if sql.search_user_in_fed(fed_id, user_id) == False: update.effective_message.reply_text( tld(chat.id, "feds_demote_target_not_admin")) return res = sql.user_demote_fed(fed_id, user_id) if res == True: update.effective_message.reply_text( tld(chat.id, "feds_demote_success")) else: update.effective_message.reply_text( tld(chat.id, "feds_demote_failed")) else: update.effective_message.reply_text(tld(chat.id, "feds_owner_only")) return
def slap(update, context): args = context.args msg = update.effective_message # type: Optional[Message] # reply to correct message reply_text = msg.reply_to_message.reply_text if msg.reply_to_message else msg.reply_text # get user who sent message #if msg.from_user.username: # curr_user = "******" + escape_markdown(msg.from_user.username) #else: curr_user = "******".format( mention_markdown(msg.from_user.id, msg.from_user.first_name)) user_id = extract_user(update.effective_message, args) if user_id and user_id != "error": slapped_user = context.bot.get_chat(user_id) user1 = curr_user #if slapped_user.username: # user2 = "@" + escape_markdown(slapped_user.username) #else: user2 = "{}".format( mention_markdown(slapped_user.id, slapped_user.first_name)) # if no target found, bot targets the sender else: user1 = "{}".format( mention_markdown(context.bot.id, context.bot.first_name)) user2 = curr_user temp = random.choice(SLAP_TEMPLATES) item = random.choice(ITEMS) hit = random.choice(HIT) throw = random.choice(THROW) repl = temp.format(user1=user1, user2=user2, item=item, hits=hit, throws=throw) send_message(update.effective_message, repl, parse_mode=ParseMode.MARKDOWN)
def about_bio(bot: Bot, update: Update, args: List[str]): message = update.effective_message chat = update.effective_chat user_id = extract_user(message, args) if user_id: user = bot.get_chat(user_id) else: user = message.from_user info = sql.get_user_bio(user.id) if info: update.effective_message.reply_text("*{}*:\n{}".format( user.first_name, escape_markdown(info)), parse_mode=ParseMode.MARKDOWN) elif message.reply_to_message: username = user.first_name update.effective_message.reply_text( tld(chat.id, 'userinfo_bio_none_they').format(username)) else: update.effective_message.reply_text( tld(chat.id, 'userinfo_bio_none_you'))
def unmute(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat user = update.effective_user message = update.effective_message conn = connected(bot, update, chat, user.id) if conn: chatD = dispatcher.bot.getChat(conn) else: if chat.type == "private": return else: chatD = chat user_id = extract_user(message, args) if not user_id: message.reply_text(tld(chat.id, "unmute_invalid")) return "" try: member = chatD.get_member(int(user_id)) except BadRequest as excp: if excp.message == "User not found.": message.reply_text(tld(chat.id, "bans_err_usr_not_found")) return "" else: raise if is_user_ban_protected(chat, user_id, member): message.reply_text(tld(chat.id, "unmute_is_an_admin")) return "" if member.status != 'restricted': message.reply_text( tld(chat.id, "unmute_not_muted").format(chatD.title)) return "" if member.status != 'kicked' and member.status != 'left': if member.can_send_messages and member.can_send_media_messages \ and member.can_send_other_messages and member.can_add_web_page_previews: message.reply_text( tld(chat.id, "unmute_not_muted").format(chatD.title)) else: bot.restrict_chat_member(chatD.id, int(user_id), can_send_messages=True, can_send_media_messages=True, can_send_other_messages=True, can_add_web_page_previews=True) keyboard = [] reply = tld(chat.id, "unmute_success").format( mention_html(member.user.id, member.user.first_name), chatD.title) message.reply_text(reply, reply_markup=keyboard, parse_mode=ParseMode.HTML) return "<b>{}:</b>" \ "\n#UNMUTE" \ "\n<b>• Admin:</b> {}" \ "\n<b>• User:</b> {}" \ "\n<b>• ID:</b> <code>{}</code>".format(html.escape(chatD.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name), user_id) else: message.reply_text(tld(chat.id, "unmute_not_in_chat")) return ""
def unfban(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat user = update.effective_user message = update.effective_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 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 do not 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!") if not fbanreason: return return message.reply_text( "I'll give {} a second chance in this federation".format( user_chat.first_name)) 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) except BadRequest as excp: if excp.message in UNFBAN_ERRORS: pass else: LOGGER.warning("Cannot remove fban on {} because: {}".format( chat, excp.message)) except TelegramError: pass try: x = sql.un_fban_user(fed_id, user_id) if not x: message.reply_text( "Fban failure, this user may have been un-fedbanned!") return except Exception: pass message.reply_text("This person is un-fbanned.")
def promote(update, context): chat_id = update.effective_chat.id message = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] args = context.args conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": send_message(update.effective_message, "You can do this command in groups, not PM") return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title user_id = extract_user(message, args) if not user_id: send_message(update.effective_message, "You don't seem to be referring to a user.") return "" if user_id == "error": send_message(update.effective_message, "Error: Unknown user!") return "" user_member = chat.get_member(user_id) if user_member.status == 'administrator' or user_member.status == 'creator': send_message(update.effective_message, "How am I meant to promote someone that's already an admin?") return "" if user_id == context.bot.id: send_message(update.effective_message, "I can't promote myself! Get an admin to do it for me.") return "" # set same perms as bot - bot can't assign higher perms than itself! bot_member = chat.get_member(context.bot.id) try: context.bot.promote_chat_member(chat_id, user_id, # can_change_info=bot_member.can_change_info, can_post_messages=bot_member.can_post_messages, can_edit_messages=bot_member.can_edit_messages, can_delete_messages=bot_member.can_delete_messages, can_invite_users=bot_member.can_invite_users, can_restrict_members=bot_member.can_restrict_members, can_pin_messages=bot_member.can_pin_messages, # can_promote_members=bot_member.can_promote_members ) except BadRequest as error: if error.message == "Bot_groups_blocked": send_message(update.effective_message, "Failed to promote: Bot was locked") else: send_message(update.effective_message, "Cannot promote users, maybe I am not admin or do not have permission to promote users.") return send_message(update.effective_message, f"Admin {mention_html(user.id, user.first_name)} promoted {mention_html(user_member.user.id, user_member.user.first_name)}", parse_mode=ParseMode.HTML) return "<b>{}:</b>" \ "\n#PROMOTED" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(user_member.user.id, user_member.user.first_name))
def demote(bot: Bot, update: Update, args: List[str]) -> str: chat = update.effective_chat message = update.effective_message user = update.effective_user conn = connected(bot, update, chat, user.id) if conn: chatD = dispatcher.bot.getChat(conn) else: chatD = update.effective_chat if chat.type == "private": return "" if not chatD.get_member(bot.id).can_promote_members: update.effective_message.reply_text(tld(chat.id, "admin_err_no_perm")) return "" member = chatD.get_member(user.id) if not member.can_promote_members and member.status != 'creator': update.effective_message.reply_text( tld(chat.id, "admin_err_user_no_perm")) return "" user_id = extract_user(message, args) if not user_id: message.reply_text(tld(chat.id, "common_err_no_user")) return "" user_member = chatD.get_member(user_id) if user_member.status == 'creator': message.reply_text(tld(chat.id, "admin_err_demote_creator")) return "" if not user_member.status == 'administrator': message.reply_text(tld(chat.id, "admin_err_demote_noadmin")) return "" if user_id == bot.id: message.reply_text(tld(chat.id, "admin_err_self_demote")) return "" try: bot.promoteChatMember(int(chatD.id), int(user_id), can_change_info=False, can_post_messages=False, can_edit_messages=False, can_delete_messages=False, can_invite_users=False, can_restrict_members=False, can_pin_messages=False, can_promote_members=False) message.reply_text(tld(chat.id, "admin_demote_success").format( mention_html(user.id, user.first_name), mention_html(user_member.user.id, user_member.user.first_name), html.escape(chatD.title)), parse_mode=ParseMode.HTML) return f"<b>{html.escape(chatD.title)}:</b>" \ "\n#DEMOTED" \ f"\n<b>Admin:</b> {mention_html(user.id, user.first_name)}" \ f"\n<b>User:</b> {mention_html(user_member.user.id, user_member.user.first_name)}" except BadRequest: message.reply_text(tld(chat.id, "admin_err_cant_demote")) return ""
def warns(update, context): message = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] args = context.args conn = connected(context.bot, update, chat, user.id, need_admin=False) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": send_message(update.effective_message, "You can do this command in groups, not PM") return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title user_id = extract_user(message, args) or update.effective_user.id result = sql.get_warns(user_id, chat.id) if result and result[0] != 0: num_warns, reasons = result limit, soft_warn, warn_mode = sql.get_warn_setting(chat.id) if reasons: if conn: text = "This user has {}/{} warnings at *{}*, for the following reasons:".format( num_warns, limit, chat_name) else: text = "This user has {}/{} warnings, for the following reasons:".format( num_warns, limit) for reason in reasons: text += "\n - {}".format(reason) msgs = split_message(text) for msg in msgs: send_message(update.effective_message, msg, parse_mode=ParseMode.HTML) else: if conn: send_message( update.effective_message, "User has {}/{} warnings at *{}*, but no reasons for any of them." .format(num_warns, limit, chat_name), parse_mode=ParseMode.HTML) else: send_message( update.effective_message, "User has {}/{} warnings, but no reasons for any of them.". format(num_warns, limit)) else: if conn: send_message( update.effective_message, "This user hasn't got any warnings in *{}*!".format(chat_name), parse_mode=ParseMode.HTML) else: send_message(update.effective_message, "This user hasn't got any warnings!")
def ungban(update, context): message = update.effective_message # type: Optional[Message] chat_name = update.effective_message.chat.title args = context.args user_id = extract_user(message, args) if not user_id: send_message(update.effective_message, "You don't seem to be referring to a user.") return if user_id == "error": send_message(update.effective_message, "Error: Unknown user!") return "" user_chat = context.bot.get_chat(user_id) if user_chat.type != 'private': send_message(update.effective_message, "That's not a user!") return if not sql.is_user_gbanned(user_id): send_message(update.effective_message, "This user is not gbanned!") return banner = update.effective_user # type: Optional[User] send_message(update.effective_message, "I'll give {} a second chance, globally.".format(user_chat.first_name)) send_to_list(context.bot, SUDO_USERS + SUPPORT_USERS, "{} has ungbanned user {}".format(mention_html(banner.id, banner.first_name), mention_html(user_chat.id, user_chat.first_name)), html=True) sql.ungban_user(user_id) chats = get_all_chats() for chat in chats: chat_id = chat.chat_id # Check if this group has disabled gbans if not sql.does_chat_gban(chat_id): continue try: member = context.bot.get_chat_member(chat_id, user_id) if member.status == 'kicked': context.bot.unban_chat_member(chat_id, user_id) except BadRequest as excp: if excp.message in UNGBAN_ERRORS: pass else: send_message(update.effective_message, "Could not un-gban due to: {}".format(excp.message)) context.bot.send_message(OWNER_ID, "Could not un-gban due to: {}".format(excp.message)) return except TelegramError: pass log_message = (f"#UNGBANNED\n" f"<b>Originated from:</b> {chat_name}\n" f"<b>Admin:</b> {mention_html(banner.id, banner.first_name)}\n" f"<b>Banned User:</b> {mention_html(user_chat.id, user_chat.first_name)}\n" f"<b>Banned User ID:</b> {user_chat.id}") if GBAN_LOGS: context.bot.send_message(GBAN_LOGS, log_message, parse_mode=ParseMode.HTML)
def info(update, context): args = context.args msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user_id = extract_user(update.effective_message, args) if user_id and user_id != "error": user = context.bot.get_chat(user_id) elif not msg.reply_to_message and not args: user = msg.from_user elif not msg.reply_to_message and ( not args or (len(args) >= 1 and not args[0].startswith("@") and not args[0].isdigit() and not msg.parse_entities([MessageEntity.TEXT_MENTION]))): send_message(update.effective_message, "I can't extract a user from this.") return else: return text = "<b>User info</b>:" \ + "\nID: <code>{}</code>".format(user.id) + \ "\nFirst Name: {}".format(html.escape(user.first_name)) if user.last_name: text += "\nLast Name: {}".format(html.escape(user.last_name)) if user.username: text += "\nUsername: @{}".format(html.escape(user.username)) text += "\nPermanent user link: {}".format(mention_html(user.id, "link")) if user.id == OWNER_ID: text += "\n\nThis person is my owner - I would never do anything against them!" else: if user.id in SUDO_USERS: text += "\n\nThis person is one of my sudo users! Nearly as powerful as my owner - so watch it." else: if user.id in SUPPORT_USERS: text += "\n\nThis person is one of my support users! Not quite a sudo user, but can still gban you off the map." if user.id in WHITELIST_USERS: text += "\n\nThis person has been whitelisted! That means I'm not allowed to ban/kick them." fedowner = feds_sql.get_user_owner_fed_name(user.id) if fedowner: text += "\n\n<b>This user is a owner fed in the current federation:</b>\n<code>" text += "</code>, <code>".join(fedowner) text += "</code>" # fedadmin = feds_sql.get_user_admin_fed_name(user.id) # if fedadmin: # text += tl(update.effective_message, "\n\nThis user is a fed admin in the current federation:\n") # text += ", ".join(fedadmin) for mod in USER_INFO: mod_info = mod.__user_info__(user.id, chat.id).strip() if mod_info: text += "\n\n" + mod_info send_message(update.effective_message, text, parse_mode=ParseMode.HTML)
def unmute(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] message = update.effective_message # type: Optional[Message] args = context.args user_id = extract_user(message, args) if not user_id or user_id == "error": send_message( update.effective_message, "You'll need to either give me a username to unmute, or reply to someone to be unmuted." ) return "" conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title text = "This user already has the right to speak in *{}*.".format( chat_name) text2 = "Unmuted on *{}*.".format(chat_name) else: if update.effective_message.chat.type == "private": update.effective_send_message( update.effective_message, "You can do this command in groups, not PM") return "" chat = update.effective_chat member = chat.get_member(int(user_id)) chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title text = "This user already has the right to speak." text2 = "{} can talk now".format( mention_html(member.user.id, member.user.first_name)) check = context.bot.getChatMember(chat.id, user.id) if check['can_restrict_members'] == False: send_message(update.effective_message, "You have no right to restrict someone.") return "" if member: if is_user_admin(chat, user_id, member=member): send_message(update.effective_message, "This is an admin, what do you expect me to do?") return "" elif member.status != 'kicked' and member.status != 'left': if member.can_send_messages and member.can_send_media_messages \ and member.can_send_other_messages and member.can_add_web_page_previews: send_message(update.effective_message, text, parse_mode=ParseMode.HTML) return "" else: context.bot.restrict_chat_member( chat.id, int(user_id), permissions=ChatPermissions( can_send_messages=True, can_send_media_messages=True, can_send_other_messages=True, can_add_web_page_previews=True)) send_message(update.effective_message, text2, parse_mode=ParseMode.HTML) return "<b>{}:</b>" \ "\n#UNMUTE" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(member.user.id, member.user.first_name)) else: send_message( update.effective_message, "This user isn't even in the chat, unmuting them won't make them talk more than they already do!" ) return ""
def info(bot: Bot, update: Update, args: List[str]): message = update.effective_message chat = update.effective_chat user_id = extract_user(update.effective_message, args) if user_id: user = bot.get_chat(user_id) elif not message.reply_to_message and not args: user = message.from_user elif not message.reply_to_message and ( not args or (len(args) >= 1 and not args[0].startswith("@") and not args[0].isdigit() and not message.parse_entities([MessageEntity.TEXT_MENTION]))): message.reply_text("I can't extract a user from this.") return else: return text = (f"<b>Characteristics:</b>\n" f"ID: <code>{user.id}</code>\n" f"First Name: {html.escape(user.first_name)}") if user.last_name: text += f"\nLast Name: {html.escape(user.last_name)}" if user.username: text += f"\nUsername: @{html.escape(user.username)}" text += f"\nPermanent user link: {mention_html(user.id, 'link')}" num_chats = sql.get_user_num_chats(user.id) text += f"\nChat count: <code>{num_chats}</code>" try: user_member = chat.get_member(user.id) if user_member.status == "administrator": result = requests.post( f"https://api.telegram.org/bot{TOKEN}/getChatMember?chat_id={chat.id}&user_id={user.id}" ) result = result.json()["result"] if "custom_title" in result.keys(): custom_title = result["custom_title"] text += f"\nTitle: <b>{custom_title}</b>" except BadRequest: pass if user.id == OWNER_ID: text += tld(chat.id, "misc_info_is_original_owner") elif user.id in SUDO_USERS: text += tld(chat.id, "misc_info_is_sudo") elif user.id in WHITELIST_USERS: text += tld(chat.id, "misc_info_is_whitelisted") for mod in USER_INFO: if mod.__mod_name__ == "Users": continue try: mod_info = mod.__user_info__(user.id) except TypeError: mod_info = mod.__user_info__(user.id, chat.id) if mod_info: text += "\n" + mod_info if INFOPIC: try: profile = bot.get_user_profile_photos(user.id).photos[0][-1] _file = bot.get_file(profile["file_id"]) _file.download(f"{user.id}.png") message.reply_photo( photo=open(f"{user.id}.png", "rb"), caption=(text), parse_mode=ParseMode.HTML, ) os.remove(f"{user.id}.png") # Incase user don't have profile pic, send normal text except IndexError: message.reply_text(text, parse_mode=ParseMode.HTML, disable_web_page_preview=True) else: message.reply_text(text, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
def ungban(bot: Bot, update: Update, args: List[str]): message = update.effective_message user = update.effective_user chat = update.effective_chat log_message = "" user_id = extract_user(message, args) if not user_id: message.reply_text( "You don't seem to be referring to a user or the ID specified is incorrect.." ) 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_gbanned(user_id): message.reply_text("This user is not gbanned!") return message.reply_text(f"I'll give {user_chat.first_name} a second chance, globally.") start_time = time.time() datetime_fmt = "%Y-%m-%dT%H:%M" current_time = datetime.utcnow().strftime(datetime_fmt) if chat.type != "private": chat_origin = f"<b>{html.escape(chat.title)} ({chat.id})</b>\n" else: chat_origin = f"<b>{chat.id}</b>\n" log_message = ( f"#UNGBANNED\n" f"<b>Originated from:</b> <code>{chat_origin}</code>\n" f"<b>Admin:</b> {mention_html(user.id, user.first_name)}\n" f"<b>Unbanned User:</b> {mention_html(user_chat.id, user_chat.first_name)}\n" f"<b>Unbanned User ID:</b> <code>{user_chat.id}</code>\n" f"<b>Event Stamp:</b> <code>{current_time}</code>" ) if EVENT_LOGS: try: log = bot.send_message(EVENT_LOGS, log_message, parse_mode=ParseMode.HTML) except BadRequest as excp: log = bot.send_message( EVENT_LOGS, log_message + "\n\nFormatting has been disabled due to an unexpected error.", ) else: send_to_list(bot, log_message, html=True) chats = get_user_com_chats(user_id) ungbanned_chats = 0 for chat in chats: chat_id = int(chat) # Check if this group has disabled gbans if not sql.does_chat_gban(chat_id): continue try: member = bot.get_chat_member(chat_id, user_id) if member.status == "kicked": bot.unban_chat_member(chat_id, user_id) ungbanned_chats += 1 except BadRequest as excp: if excp.message in UNGBAN_ERRORS: pass else: message.reply_text(f"Could not un-gban due to: {excp.message}") if EVENT_LOGS: bot.send_message( EVENT_LOGS, f"Could not un-gban due to: {excp.message}", parse_mode=ParseMode.HTML, ) else: bot.send_message( OWNER_ID, f"Could not un-gban due to: {excp.message}" ) return except TelegramError: pass sql.ungban_user(user_id) if EVENT_LOGS: log.edit_text( log_message + f"\n<b>Chats affected:</b> {ungbanned_chats}", parse_mode=ParseMode.HTML, ) else: send_to_list(bot, "un-gban complete!") end_time = time.time() ungban_time = round((end_time - start_time), 2) if ungban_time > 60: ungban_time = round((ungban_time / 60), 2) message.reply_text(f"Person has been un-gbanned. Took {ungban_time} min") else: message.reply_text(f"Person has been un-gbanned. Took {ungban_time} sec")
def demote(update, context): chat = update.effective_chat # type: Optional[Chat] message = update.effective_message # type: Optional[Message] user = update.effective_user # type: Optional[User] args = context.args conn = connected(context.bot, update, chat, user.id, need_admin=True) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if update.effective_message.chat.type == "private": send_message(update.effective_message, "You can do this command in groups, not PM") return "" chat = update.effective_chat chat_id = update.effective_chat.id chat_name = update.effective_message.chat.title user_id = extract_user(message, args) if not user_id: send_message(update.effective_message, "You don't seem to be referring to a user.") return "" if user_id == "error": send_message(update.effective_message, "Error: Unknown user!") return "" user_member = chat.get_member(user_id) if user_member.status == 'creator': send_message(update.effective_message, "This person CREATED the chat, how would I demote them?") return "" if not user_member.status == 'administrator': send_message(update.effective_message, "Can't demote what wasn't promoted!") return "" if user_id == context.bot.id: send_message(update.effective_message, "I can't demote myself! Get an admin to do it for me.") return "" try: context.bot.promoteChatMember(int(chat.id), int(user_id), can_change_info=False, can_post_messages=False, can_edit_messages=False, can_delete_messages=False, can_invite_users=False, can_restrict_members=False, can_pin_messages=False, can_promote_members=False ) send_message(update.effective_message, f"Admin {mention_html(user.id, user.first_name)} demoted {mention_html(user_member.user.id, user_member.user.first_name)}", parse_mode=ParseMode.HTML) return "<b>{}:</b>" \ "\n#DEMOTED" \ "\n<b>Admin:</b> {}" \ "\n<b>User:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name), mention_html(user_member.user.id, user_member.user.first_name)) except BadRequest: send_message(update.effective_message, "Could not demote. I might not be admin, or the admin status was appointed by another user, so I can't act upon them!") return ""