def unpin(update, context): chat = update.effective_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 try: context.bot.unpinChatMessage(chat.id) if conn: send_message(update.effective_message, "I have unpin the message in the group {}".format(chat_name)) except BadRequest as excp: if excp.message == "Chat_not_modified": pass else: raise return "<b>{}:</b>" \ "\n#UNPINNED" \ "\n<b>Admin:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name))
def invite(bot: Bot, update: Update): chat = update.effective_chat user = update.effective_user conn = connected(bot, update, chat, user.id, need_admin=False) if conn: chatP = dispatcher.bot.getChat(conn) else: chatP = update.effective_chat if chat.type == "private": return if chatP.username: update.effective_message.reply_text(chatP.username) elif chatP.type == chatP.SUPERGROUP or chatP.type == chatP.CHANNEL: bot_member = chatP.get_member(bot.id) if bot_member.can_invite_users: invitelink = chatP.invite_link #print(invitelink) if not invitelink: invitelink = bot.exportChatInviteLink(chatP.id) update.effective_message.reply_text(invitelink) else: update.effective_message.reply_text( tld(chat.id, "admin_err_no_perm_invitelink")) else: update.effective_message.reply_text( tld(chat.id, "admin_chat_no_invitelink"))
def enable(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_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 if len(args) >= 1: enable_cmd = args[0] if enable_cmd.startswith(CMD_STARTERS): enable_cmd = enable_cmd[1:] if sql.enable_command(chat.id, enable_cmd): if conn: text = "Enabled the use of `{}` in *{}*".format(enable_cmd, chat_name) else: text = "Enabled the use of `{}`".format(enable_cmd) send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN) else: send_message(update.effective_message, "Is that even disabled?") else: send_message(update.effective_message, "What should I enable?")
def blacklist(bot: Bot, update: Update, args: List[str]): msg = update.effective_message chat = update.effective_chat user = update.effective_user conn = connected(bot, update, chat, user.id, need_admin=False) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if chat.type == "private": return else: chat_id = update.effective_chat.id chat_name = chat.title filter_list = tld(chat.id, "blacklist_active_list").format(chat_name) all_blacklisted = sql.get_chat_blacklist(chat_id) if len(args) > 0 and args[0].lower() == 'copy': for trigger in all_blacklisted: filter_list += "<code>{}</code>\n".format(html.escape(trigger)) else: for trigger in all_blacklisted: filter_list += " • <code>{}</code>\n".format(html.escape(trigger)) split_text = split_message(filter_list) for text in split_text: if filter_list == tld(chat.id, "blacklist_active_list").format( chat_name): #We need to translate msg.reply_text(tld(chat.id, "blacklist_no_list").format(chat_name), parse_mode=ParseMode.HTML) return msg.reply_text(text, parse_mode=ParseMode.HTML)
def blackliststicker(update, context): msg = 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_id = conn chat_name = dispatcher.bot.getChat(conn).title else: if chat.type == "private": return else: chat_id = update.effective_chat.id chat_name = chat.title sticker_list = "<b>List black stickers currently in {}:</b>\n".format(chat_name) all_stickerlist = sql.get_chat_stickers(chat_id) if len(args) > 0 and args[0].lower() == 'copy': for trigger in all_stickerlist: sticker_list += "<code>{}</code>\n".format(html.escape(trigger)) elif len(args) == 0: for trigger in all_stickerlist: sticker_list += " - <code>{}</code>\n".format(html.escape(trigger)) split_text = split_message(sticker_list) for text in split_text: if sticker_list == "<b>List black stickers currently in {}:</b>\n".format(chat_name).format(chat_name): send_message(update.effective_message, "There is no blacklist sticker in <b>{}</b>!".format(chat_name), parse_mode=ParseMode.HTML) return send_message(update.effective_message, text, parse_mode=ParseMode.HTML)
def unblackliststicker(update, context): msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] words = msg.text.split(None, 1) conn = connected(context.bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": return else: chat_name = chat.title if len(words) > 1: text = words[1].replace('https://t.me/addstickers/', '') to_unblacklist = list(set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) successful = 0 for trigger in to_unblacklist: success = sql.rm_from_stickers(chat_id, trigger.lower()) if success: successful += 1 if len(to_unblacklist) == 1: if successful: send_message(update.effective_message, "Sticker <code>{}</code> deleted from blacklist in <b>{}</b>!".format(html.escape(to_unblacklist[0]), chat_name), parse_mode=ParseMode.HTML) else: send_message(update.effective_message, "This is not on the blacklist stickers...!") elif successful == len(to_unblacklist): send_message(update.effective_message, "Sticker <code>{}</code> deleted from blacklist in <b>{}</b>!".format( successful, chat_name), parse_mode=ParseMode.HTML) elif not successful: send_message(update.effective_message, "None of these stickers exist, so they cannot be removed.".format( successful, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML) else: send_message(update.effective_message, "Sticker <code>{}</code> deleted from blacklist. {} did not exist, so it's not deleted.".format(successful, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML) elif msg.reply_to_message: trigger = msg.reply_to_message.sticker.set_name if trigger == None: send_message(update.effective_message, "Sticker is invalid!") return success = sql.rm_from_stickers(chat_id, trigger.lower()) if success: send_message(update.effective_message, "Sticker <code>{}</code> deleted from blacklist in <b>{}</b>!".format(trigger, chat_name), parse_mode=ParseMode.HTML) else: send_message(update.effective_message, "{} not found on blacklisted stickers...!".format(trigger)) else: send_message(update.effective_message, "Tell me what stickers you want to add to the blacklist stickers.")
def warn_user(update, context): message = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] warner = update.effective_user # type: Optional[User] user = update.effective_user args = context.args user_id, reason = extract_user_and_text(message, args) if user_id == "error": send_message(update.effective_message, reason) 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 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: if conn: warning = warn(chat.get_member(user_id).user, chat, reason, message, warner, conn=True) send_message(update.effective_message, "That user has been warned in *{}*".format(chat_name), parse_mode=ParseMode.HTML) return warning else: if message.reply_to_message and message.reply_to_message.from_user.id == user_id: return warn(message.reply_to_message.from_user, chat, reason, message.reply_to_message, warner) else: return warn( chat.get_member(user_id).user, chat, reason, message, warner) else: send_message(update.effective_message, "No user was designated!") return ""
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 timeout_mode(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] msg = update.effective_message # type: Optional[Message] 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 getcur, extra_verify, cur_value, timeout, timeout_mode, cust_text = sql.welcome_security( chat.id) if args: if args[0].lower() == 'kick' or args[0].lower() == 'leave': settypeblacklist = 'kick' sql.set_welcome_security(chat.id, getcur, extra_verify, cur_value, timeout, 1, cust_text) elif args[0].lower() == 'ban' or args[0].lower() == 'banned': settypeblacklist = 'banned' sql.set_welcome_security(chat.id, getcur, extra_verify, cur_value, timeout, 2, cust_text) else: send_message(update.effective_message, "I only understand kick/banned!") return if conn: text = "Timeout mode changed, User will be `{}` at *{}*!".format( settypeblacklist, chat_name) else: text = "Timeout mode changed, User will be `{}`!".format( settypeblacklist) send_message(update.effective_message, text, parse_mode="markdown") else: if timeout_mode == 1: settypeblacklist = "kick" elif timeout_mode == 2: settypeblacklist = "banned" if conn: text = "The current timeout mode is set to *{}* at *{}*.".format( settypeblacklist, chat_name) else: text = "The current timeout mode is set to *{}*.".format( settypeblacklist) send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN) 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 add_blackliststicker(update, context): msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] words = msg.text.split(None, 1) conn = connected(context.bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": return else: chat_name = chat.title if len(words) > 1: text = words[1].replace('https://t.me/addstickers/', '') to_blacklist = list(set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) added = 0 for trigger in to_blacklist: try: get = context.bot.getStickerSet(trigger) sql.add_to_stickers(chat_id, trigger.lower()) added += 1 except BadRequest: send_message(update.effective_message, "Sticker `{}` can not be found!".format(trigger), parse_mode="markdown") if added == 0: return if len(to_blacklist) == 1: send_message(update.effective_message, "Sticker <code>{}</code> added to blacklist stickers in <b>{}</b>!".format(html.escape(to_blacklist[0]), chat_name), parse_mode=ParseMode.HTML) else: send_message(update.effective_message, "<code>{}</code> stickers added to blacklist sticker in <b>{}</b>!".format(added, chat_name), parse_mode=ParseMode.HTML) elif msg.reply_to_message: added = 0 trigger = msg.reply_to_message.sticker.set_name if trigger == None: send_message(update.effective_message, "Sticker is invalid!") return try: get = context.bot.getStickerSet(trigger) sql.add_to_stickers(chat_id, trigger.lower()) added += 1 except BadRequest: send_message(update.effective_message, "Sticker `{}` can not be found!".format(trigger), parse_mode="markdown") if added == 0: return send_message(update.effective_message, "Sticker <code>{}</code> added to blacklist stickers in <b>{}</b>!".format(trigger, chat_name), parse_mode=ParseMode.HTML) else: send_message(update.effective_message, "Tell me what stickers you want to add to the blacklist stickers.")
def set_rules(update, context): chat = update.effective_chat chat_id = update.effective_chat.id user = update.effective_user msg = update.effective_message # type: Optional[Message] raw_text = msg.text args = raw_text.split(None, 1) # use python's maxsplit to separate cmd and 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 if len(args) == 2: txt = args[1] offset = len(txt) - len( raw_text) # set correct offset relative to command markdown_rules = markdown_parser(txt, entities=msg.parse_entities(), offset=offset) sql.set_rules(chat_id, markdown_rules) if conn: send_message(update.effective_message, "Successfully set rules for *{}*.".format(chat_name), parse_mode="markdown") else: send_message(update.effective_message, "Successfully set rules for this group.") elif msg.reply_to_message and len(args) == 1: txt = msg.reply_to_message.text offset = len(txt) - len( raw_text) # set correct offset relative to command markdown_rules = markdown_parser(txt, entities=msg.parse_entities(), offset=offset) sql.set_rules(chat_id, markdown_rules) if conn: send_message(update.effective_message, "Successfully set rules for *{}*.".format(chat_name), parse_mode="markdown") else: send_message(update.effective_message, "Successfully set rules for this group.")
def unblacklist(bot: Bot, update: Update): msg = update.effective_message chat = update.effective_chat user = update.effective_user words = msg.text.split(None, 1) conn = connected(bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": return else: chat_name = chat.title if len(words) > 1: text = words[1] to_unblacklist = list( set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) successful = 0 for trigger in to_unblacklist: success = sql.rm_from_blacklist(chat_id, trigger.lower()) if success: successful += 1 if len(to_unblacklist) == 1: if successful: msg.reply_text(tld(chat.id, "blacklist_del").format( html.escape(to_unblacklist[0]), chat_name), parse_mode=ParseMode.HTML) else: msg.reply_text(tld(chat.id, "blacklist_err_not_trigger")) elif successful == len(to_unblacklist): msg.reply_text(tld(chat.id, "blacklist_multi_del").format( successful, chat_name), parse_mode=ParseMode.HTML) elif not successful: msg.reply_text(tld(chat.id, "blacklist_err_multidel_no_trigger").format( successful, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML) else: msg.reply_text(tld( chat.id, "blacklist_err_multidel_some_no_trigger").format( successful, chat_name, len(to_unblacklist) - successful), parse_mode=ParseMode.HTML) else: msg.reply_text(tld(chat.id, "blacklist_err_del_no_args"))
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 pin(update, context): user = update.effective_user # type: Optional[User] chat = update.effective_chat # type: Optional[Chat] args = context.args pin_args = update.message.text.split() 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 if len(args) <= 1: send_message(update.effective_message, "Use /pin <notify/loud/silent/violent> <message link>") return "" prev_message = args[1] if "/" in prev_message: prev_message = prev_message.split("/")[-1] 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 if update.effective_message.reply_to_message: prev_message = update.effective_message.reply_to_message.message_id else: send_message(update.effective_message, "Reply to a message for pin that message in this group") return "" is_group = chat.type != "private" and chat.type != "channel" is_silent = False if len(pin_args) > 1: if pin_args[1].lower() == 'silent' or pin_args[1].lower() == 'off' or pin_args[1].lower() == 'mute': is_silent = True if len(pin_args) == 1: is_silent = True if prev_message and is_group: try: context.bot.pinChatMessage(chat.id, prev_message) if is_silent else context.bot.pinChatMessage(chat.id, prev_message, disable_notification=is_silent) if conn: send_message(update.effective_message, "I have pinned messages in the group {}".format(chat_name)) except BadRequest as excp: if excp.message == "Chat_not_modified": pass else: raise return "<b>{}:</b>" \ "\n#PINNED" \ "\n<b>Admin:</b> {}".format(html.escape(chat.title), mention_html(user.id, user.first_name)) return ""
def private_note(update, context): args = context.args chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] conn = connected(context.bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": chat_name = chat.title else: chat_name = chat.title if len(args) >= 1: if args[0] in ("yes", "on"): if len(args) >= 2: if args[1] == "del": sql.private_note(str(chat_id), True, True) send_message( update.effective_message, "Private Note was *enabled*, when users get notes, the message will be sent to the PM and the hashtag message will be deleted.", parse_mode="markdown") else: sql.private_note(str(chat_id), True, False) send_message( update.effective_message, "Private Note was *enabled*, when users get notes, the message will be sent to the PM.", parse_mode="markdown") else: sql.private_note(str(chat_id), True, False) send_message( update.effective_message, "Private Note was *enabled*, when users get notes, the message will be sent to the PM.", parse_mode="markdown") elif args[0] in ("no", "off"): sql.private_note(str(chat_id), False, False) send_message( update.effective_message, "Private Note was *disabled*, notes will be sent to group.", parse_mode="markdown") else: send_message(update.effective_message, "Unknown argument - please use 'yes', or 'no'.") else: is_private, is_delete = sql.get_private_note(chat_id) print(is_private, is_delete) send_message(update.effective_message, "Current Private Note settings at {}: *{}*{}".format( chat_name, "Enabled" if is_private else "Disabled", " - *Hash will be deleted*" if is_delete else ""), parse_mode="markdown")
def set_warn_limit(update, context) -> str: chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] msg = update.effective_message # type: Optional[Message] 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 if args: if args[0].isdigit(): if int(args[0]) < 3: send_message(update.effective_message, "The minimum warn limit is 3!") else: sql.set_warn_limit(chat.id, int(args[0])) if conn: text = "Updated the warn limit to {} at *{}*".format( args[0], chat_name) else: text = "Updated the warn limit to {}".format(args[0]) send_message(update.effective_message, text, parse_mode=ParseMode.HTML) return "<b>{}:</b>" \ "\n#SET_WARN_LIMIT" \ "\n<b>Admin:</b> {}" \ "\nSet the warn limit to <code>{}</code>".format(html.escape(chat.title), mention_html(user.id, user.first_name), args[0]) else: send_message(update.effective_message, "Give me a number as an arg!") else: limit, soft_warn, warn_mode = sql.get_warn_setting(chat.id) if conn: text = "The current warn limit is {} at *{}*".format( limit, chat_name) else: text = "The current warn limit is {}".format(limit) send_message(update.effective_message, text, parse_mode=ParseMode.HTML) return ""
def add_warn_filter(update, context): chat = update.effective_chat # type: Optional[Chat] msg = update.effective_message # type: Optional[Message] user = update.effective_user # type: Optional[User] 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 args = msg.text.split( None, 1) # use python's maxsplit to separate Cmd, keyword, and reply_text if len(args) < 2: return extracted = split_quotes(args[1]) if len(extracted) >= 2: # set trigger -> lower, so as to avoid adding duplicate filters with different cases keyword = extracted[0].lower() content = extracted[1] else: return # Note: perhaps handlers can be removed somehow using sql.get_chat_filters for handler in dispatcher.handlers.get(WARN_HANDLER_GROUP, []): if handler.filters == (keyword, chat.id): dispatcher.remove_handler(handler, WARN_HANDLER_GROUP) sql.add_warn_filter(chat.id, keyword, content) if conn: text = "Warn handler added for '{}' at *{}*!".format( keyword, chat_name) else: text = "Warn handler added for '{}'!".format(keyword) send_message(update.effective_message, text, parse_mode=ParseMode.HTML) raise DispatcherHandlerStop
def locale_button(bot, update): chat = update.effective_chat user = update.effective_user query = update.callback_query lang_match = re.findall(r"en-US", query.data) if lang_match: if lang_match[0]: switch_to_locale(chat.id, lang_match[0]) query.answer(text=tld(chat.id, 'language_switch_success_pm'). format(list_locales[lang_match[0]])) else: query.answer(text="Error!", show_alert=True) try: LANGUAGE = prev_locale(chat.id) locale = LANGUAGE.locale_name curr_lang = list_locales[locale] except Exception: curr_lang = "English (US)" text = tld(chat.id, "language_select_language") text += tld(chat.id, "language_user_language").format(curr_lang) conn = connected(bot, update, chat, user.id, need_admin=False) if conn: try: chatlng = prev_locale(conn).locale_name chatlng = list_locales[chatlng] text += tld(chat.id, "language_chat_language").format(chatlng) except Exception: chatlng = "English (US)" text += tld(chat.id, "language_sel_user_lang") bot.edit_message_text( chat_id=query.message.chat_id, message_id=query.message.message_id, text=text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton("English (US) 🇺🇸", callback_data="set_lang_en-US"), InlineKeyboardButton(f"{tld(chat.id, 'btn_go_back')}", callback_data="bot_start") ]])) # query.message.delete() bot.answer_callback_query(query.id)
def adminlist(update, context): 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 administrators = context.bot.getChatAdministrators(chat_id) text = "Admin in *{}*:".format(update.effective_chat.title or "current chat") for admin in administrators: user = admin.user status = admin.status if user.first_name == '': name = "☠Deleted Account" else: name = "{}".format(mention_markdown(user.id, user.first_name + " " + (user.last_name or ""))) #if user.username: # name = escape_markdown("@" + user.username) if status == "creator": text += "\n 👑 *Creator:*" text += "\n • {} (`{}`) \n\n 🔱 *Admins:*".format(name, user.id) for admin in administrators: user = admin.user status = admin.status if user.first_name == '': name = "☠Deleted Account" else: name = "{}".format(mention_markdown(user.id, user.first_name + " " + (user.last_name or ""))) #if user.username: # name = escape_markdown("@" + user.username) if status == "administrator": text += "\n • {} (`{}`)".format(name, user.id) try: send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN) except BadRequest: send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN, quote=False)
def list_warn_filters(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] 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 all_handlers = sql.get_chat_warn_triggers(chat.id) if not all_handlers: if conn: text = "No warning filters are active in *{}*!".format(chat_name) else: text = "No warning filters are active here!" send_message(update.effective_message, text, parse_mode=ParseMode.HTML) return filter_list = "CURRENT_WARNING_FILTER_STRING" if conn: filter_list = filter_list.replace('this chat', 'chat *{}*'.format(chat_name)) for keyword in all_handlers: entry = " • `{}`\n".format(html.escape(keyword)) if len(entry) + len(filter_list) > telegram.MAX_MESSAGE_LENGTH: send_message(update.effective_message, filter_list, parse_mode=telegram.ParseMode.HTML) filter_list = entry else: filter_list += entry if not filter_list == "CURRENT_WARNING_FILTER_STRING": send_message(update.effective_message, filter_list, parse_mode=ParseMode.HTML)
def save(bot: Bot, update: Update): chat = update.effective_chat user = update.effective_user conn = connected(bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": chat_name = tld(chat.id, "note_is_local") else: chat_name = chat.title msg = update.effective_message note_name, text, data_type, content, buttons = get_note_type(msg) note_name = note_name.lower() if data_type is None: msg.reply_text(tld(chat.id, "save_invalid")) return if not sql.get_note(chat_id, note_name): sql.add_note_to_db(chat_id, note_name, text, data_type, buttons=buttons, file=content) msg.reply_text(tld(chat.id, "save_success").format(note_name, chat_name, note_name, note_name), parse_mode=ParseMode.MARKDOWN) else: sql.add_note_to_db(chat_id, note_name, text, data_type, buttons=buttons, file=content) msg.reply_text(tld(chat.id, "save_updated").format(note_name, chat_name, note_name, note_name), parse_mode=ParseMode.MARKDOWN)
def flood(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] conn = connected(context.bot, update, chat, user.id, need_admin=False) if 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_id = update.effective_chat.id chat_name = update.effective_message.chat.title limit = sql.get_flood_limit(chat_id) if limit == 0: if conn: text = "I'm not currently enforcing flood control in *{}*!".format( chat_name) else: text = "I'm not currently enforcing flood control!" send_message(update.effective_message, text, parse_mode="markdown") else: getmode, getvalue = sql.get_flood_setting(chat.id) if getmode == 1: settypeflood = 'banned' elif getmode == 2: settypeflood = 'kicked' elif getmode == 3: settypeflood = 'muted' elif getmode == 4: settypeflood = 'temporarily banned for {}'.format(getvalue) elif getmode == 5: settypeflood = 'temporarily muted for {}'.format(getvalue) if conn: text = "This chat is currently enforcing flood control after *{}* messages. Any users sending more than that amount of messages will be *{}* in *{}*.".format( limit, settypeflood, chat_name) # text = "If member is flooding messages, they will got *{}* in *{}*.".format(settypeflood, chat_name) else: text = "This chat is currently enforcing flood control after *{}* messages. Any users sending more than that amount of messages will be *{}*.".format( limit, settypeflood) # text = "If member is flooding messages, they will got *{}*.".format(settypeflood) send_message(update.effective_message, text, parse_mode="markdown")
def commands(update, context): chat = update.effective_chat user = update.effective_user 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 text = build_curr_disabled(chat.id) send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN)
def add_blacklist(update, context): msg = update.effective_message # type: Optional[Message] chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] words = msg.text.split(None, 1) conn = connected(context.bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": return else: chat_name = chat.title if len(words) > 1: text = words[1] to_blacklist = list( set(trigger.strip() for trigger in text.split("\n") if trigger.strip())) for trigger in to_blacklist: sql.add_to_blacklist(chat_id, trigger.lower()) if len(to_blacklist) == 1: send_message( update.effective_message, "<code>{}</code> Added to the blacklist in <b>{}</b>!".format( html.escape(to_blacklist[0]), chat_name), parse_mode=ParseMode.HTML) else: send_message( update.effective_message, "Added <code>{}</code> triggers to the blacklist in <b>{}</b>!" .format(len(to_blacklist), chat_name), parse_mode=ParseMode.HTML) else: send_message( update.effective_message, "Tell me which words you would like to add to the blacklist.")
def clear_rules(update, context): chat = update.effective_chat chat_id = update.effective_chat.id user = update.effective_user 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 chat_id = update.effective_chat.id sql.set_rules(chat_id, "") send_message(update.effective_message, "Successfully cleared rules!")
def stop_filter(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] args = update.effective_message.text.split(None, 1) conn = connected(context.bot, update, chat, user.id) if not conn == False: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": chat_name = "local notes" else: chat_name = chat.title if len(args) < 2: send_message(update.effective_message, "What should I stop?") return chat_filters = sql.get_chat_triggers(chat_id) if not chat_filters: send_message(update.effective_message, "There are no active filters here!") return for keyword in chat_filters: if keyword == args[1]: sql.remove_filter(chat_id, args[1]) send_message( update.effective_message, "Yep, I'll stop replying than in *{}*.".format(chat_name), parse_mode=telegram.ParseMode.MARKDOWN) raise DispatcherHandlerStop send_message( update.effective_message, "That's not a current filter - run /filters for all active filters.")
def list_handlers(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] conn = connected(context.bot, update, chat, user.id, need_admin=False) if not conn == False: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title filter_list = "*Filters in {}:*\n" else: chat_id = update.effective_chat.id if chat.type == "private": chat_name = "Local Filters" filter_list = "*Local Filters:*\n" else: chat_name = chat.title filter_list = "*Filters in {}*:\n" all_handlers = sql.get_chat_triggers(chat_id) if not all_handlers: send_message(update.effective_message, "There's no Filters in {}!".format(chat_name)) return for keyword in all_handlers: entry = " - {}\n".format(escape_markdown(keyword)) if len(entry) + len(filter_list) > telegram.MAX_MESSAGE_LENGTH: send_message(update.effective_message, filter_list.format(chat_name), parse_mode=telegram.ParseMode.MARKDOWN) filter_list = entry else: filter_list += entry send_message(update.effective_message, filter_list.format(chat_name), parse_mode=telegram.ParseMode.MARKDOWN)
def permapin(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 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 text, data_type, content, buttons = get_message_type(message) tombol = build_keyboard_alternate(buttons) try: message.delete() except BadRequest: pass if str(data_type) in ('Types.BUTTON_TEXT', 'Types.TEXT'): try: sendingmsg = context.bot.send_message(chat_id, text, parse_mode="markdown", disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup(tombol)) except BadRequest: context.bot.send_message(chat_id, "Wrong markdown text!\nIf you don't know what markdown is, please type `/markdownhelp` in PM.", parse_mode="markdown") return else: sendingmsg = ENUM_FUNC_MAP[str(data_type)](chat_id, content, caption=text, parse_mode="markdown", disable_web_page_preview=True, reply_markup=InlineKeyboardMarkup(tombol)) try: context.bot.pinChatMessage(chat_id, sendingmsg.message_id) except BadRequest: send_message(update.effective_message, "I don't have access to pin message!")