def get_admins(message): # Gets all human administrators of a chat admins_objects = [ admin for admin in bot.get_chat_administrators(config.chat_id) if not admin.user.is_bot ] # Gets every administrator's id, appends the default admins' ids admins_ids = [ad.user.id for ad in admins_objects] + [*config.admins_default] # Writes all admins' ids and information to data # Removes obsolete admins # Sends a message with the output with shelve.open(config.data_name, 'c', writeback=True) as data: data['admins'] = config.admins_default if not data.get( 'admins') else data['admins'] for admin in admins_objects: admin_id = admin.user.id admin_data = '@' + admin.user.username if admin.user.username else admin.user.first_name if admin_id not in data['admins']: data['admins'][admin_id] = admin_data for admin_id in list(data['admins']): if admin_id not in admins_ids: del data['admins'][admin_id] bot.reply_to( message, 'List of bot\'s admins:\n{}'.format(print_dict(data['admins'])))
def stk_command(message): logger.info("/stk command by {:s}, Username @{:s}".format( message.from_user.first_name, (message.from_user.username or "NONE"))) if message.chat.type != "private": return administrators = bot.get_chat_administrators(config.send_chat_id) if any(message.from_user.id == member.user.id for member in administrators): logger.info("The owner detected!") bot.send_message(message.chat.id, ru_strings.SEND_STICKER_MESSAGE['strings'][0], parse_mode='Markdown') bot.register_next_step_handler(message, send_sticker) else: logger.info("This isn't the owner!")
def togglensfw(message): global allow_nsfw administrators = bot.get_chat_administrators(config.send_chat_id) if any(message.from_user.id == member.user.id for member in administrators): allow_nsfw = not allow_nsfw if allow_nsfw is True: bot.send_message(message.chat.id, ru_strings.NSFW_TOGGLE_MESSAGE['strings'][1], parse_mode='Markdown') else: bot.send_message(message.chat.id, ru_strings.NSFW_TOGGLE_MESSAGE['strings'][0], parse_mode='Markdown') logger.info("/togglensfw command by {:s}, Username @{:s}".format( message.from_user.first_name, (message.from_user.username or "NONE")))
def everyone_command(message): administrators = bot.get_chat_administrators(config.send_chat_id) if any(message.from_user.id == member.user.id for member in administrators): logger.info("/everyone command by {:s}, Username @{:s}".format( message.from_user.first_name, (message.from_user.username or "NONE"))) message_text = "" bot.send_message(message.chat.id, "*@everyone* 😈", parse_mode='Markdown') for x in range(0, len(chatusers)): message_text += "[.](tg://user?id=" + str(chatusers[x].id) + ") " if (x + 1) % 10 == 0: bot.send_message(message.chat.id, message_text, parse_mode='Markdown') message_text = "" if len(message_text) > 1: bot.send_message(message.chat.id, message_text, parse_mode='Markdown')
def ban_user_command(message): ''' Alot of magic ''' orig_time = 40 time = 40 administrators = bot.get_chat_administrators(message.chat.id) if message.reply_to_message is not None: if all(message.from_user.id != member.user.id for member in administrators) and \ message.from_user.id != message.reply_to_message.from_user.id: return if message.chat.type != 'private' and message.reply_to_message is None: bot.delete_message(message.chat.id, message.message_id) time_str = re.search('[0-9]{1,8}', message.text) if time_str: orig_time = time = int(time_str.group(0)) time_time = re.search('([0-9]{1,8})\s?(с(ек)?|м(ин)?|ч(ас)?|д(ен|н)?)', message.text) time_text = 'сек.' if time_time: text = re.search('[А-Яа-я]{1,2}', time_time.group(0)) if text: if text.group(0)[0] == "м": time = time * 60 time_text = "мин." elif text.group(0)[0] == "ч": time = time * 60 * 60 time_text = "ч." elif text.group(0)[0] == "д": time = time * 24 * 60 * 60 time_text = "д." user_to_ban = None ban_message_num = int(time < 30) if message.chat.type == 'private': if all(message.from_user.id != user for user in config.allowed_users): return user_to_ban = message.reply_to_message.forward_from elif message.reply_to_message is None: user_to_ban = message.from_user ban_message_num = 2 if time < 40: time = 40 orig_time = 40 else: user_to_ban = message.reply_to_message.from_user if message.from_user.id == message.reply_to_message.from_user.id: ban_message_num = 2 if time < 40: time = 40 orig_time = 40 try: ban_user(config.send_chat_id, user_to_ban.id, time) bot.send_message( config.send_chat_id, ru_strings.BAN_MESSAGE['strings'][ban_message_num].format( user_to_ban.first_name, orig_time, time_text), parse_mode='Markdown') logger.info("User {}, Username @{} - banned!".format( user_to_ban.id, (user_to_ban.username or "NONE"))) except Exception as e: logger.error("[ban_command()] Unexpected error: {}".format(e))