def find_user(username: Optional[str], chat_id: int) -> Optional[User]: if not username: return None uid = User.get_id_by_name(username) if not uid: return None chat_user = ChatUser.get(uid, chat_id) if not chat_user: return None if chat_user.left: return None return User.get(uid)
def get_mentions(entities: List[Tuple[telegram.MessageEntity, str]]) \ -> Set[Union[VChatsUser, VUnknownUser]]: mentions = set() for entity, entity_text in entities: if entity.type == 'mention': user_id = User.get_id_by_name(entity_text) elif entity.type == 'text_mention': user_id = entity.user.id else: continue mentions.add(get_vuser(user_id)) return mentions
def send_mylove(bot: telegram.Bot, update: telegram.Update, send_to_cid: int, find_in_cid: int) -> None: def format_love(type: str, b: User, _: bool) -> typing.Optional[str]: if not b: return None b_pair, b_inbound, b_outbound = ReplyTop.get_user_top_strast(find_in_cid, b.uid) mutual_sign = ' ❤' if type == 'pair' and b_pair: mutual = mutual_sign if b_pair.uid == user_id else '' return f'Парная: {ReplyLove.get_fullname_or_username(b)}{mutual}' if type == 'inbound' and b_inbound: mutual = mutual_sign if b_inbound and b_inbound.uid == user_id else '' return f'Входящая: {ReplyLove.get_fullname_or_username(b)}{mutual}' if type == 'outbound' and b_outbound: mutual = mutual_sign if b_outbound and b_outbound.uid == user_id else '' return f'Исходящая: {ReplyLove.get_fullname_or_username(b)}{mutual}' return None bot.sendChatAction(send_to_cid, ChatAction.TYPING) reply_to_msg = update.message.reply_to_message if reply_to_msg: user_id = reply_to_msg.from_user.id else: splitted = update.message.text.split() if len(splitted) == 2: user_id = User.get_id_by_name(splitted[1]) else: user_id = update.message.from_user.id user = User.get(user_id) if not user: bot.send_message(send_to_cid, 'А кто это? Таких не знаю.', reply_to_message_id=update.message.message_id) return pair, inbound, outbound = ReplyTop.get_user_top_strast(find_in_cid, user_id) formats = (format_love('pair', pair, user.female), format_love('inbound', inbound, user.female), format_love('outbound', outbound, user.female)) love_list = [s for s in formats if s] if len(love_list) == 0: result = '🤷♀️🤷♂️ А нет никакой страсти' else: result = '\n'.join(love_list) if user_id in CONFIG.get('replylove__dragon_lovers', []): result = '🐉' bot.send_message(send_to_cid, f'Страсть {user.get_username_or_link()}:\n\n{result}', reply_to_message_id=update.message.message_id, parse_mode=ParseMode.HTML)
def send_personal_stat_handler(bot: telegram.Bot, update: telegram.Update) -> None: message: telegram.Message = update.message reply_to_msg = message.reply_to_message if reply_to_msg: user_id = reply_to_msg.from_user.id else: splitted = message.text.split() if len(splitted) == 2: user_id = User.get_id_by_name(splitted[1]) else: user_id = message.from_user.id send_personal_stat(bot, message.chat_id, user_id, reply_to_message_id=message.message_id)
def send_whois(bot: telegram.Bot, update: telegram.Update, send_to_cid: int, find_in_cid: int) -> None: requestor_id = update.message.from_user.id msg_id = update.message.message_id text = update.message.text.split() reply_to_msg = update.message.reply_to_message bot.sendChatAction(send_to_cid, ChatAction.TYPING) if reply_to_msg: user_id = reply_to_msg.from_user.id username = '******' + reply_to_msg.from_user.username else: if len(text) < 2: bot.sendMessage(send_to_cid, 'Укажи имя, пидор.', reply_to_message_id=msg_id) return username = text[1] user_id = User.get_id_by_name(username) if not user_id: for entity, entity_text in update.message.parse_entities().items(): if entity.type == 'text_mention': user_id = entity.user.id if not user_id: bot.sendMessage(send_to_cid, f'Нет такого: {username}', reply_to_message_id=msg_id) return info = UserStat.get_user_position(user_id, find_in_cid, update.message.date) msg = UserStat.me_format_position(username, info['msg_count'], info['position'], user_id) msg_userstat = UserStat.me_format(update.message.date, user_id, find_in_cid) if msg_userstat != '': msg_userstat = f"\n\n{msg_userstat}" bot.sendMessage(send_to_cid, f'{msg}{msg_userstat}', reply_to_message_id=msg_id, parse_mode=ParseMode.HTML) logger.info(f'User {requestor_id} requested stats for user {user_id}')
def ban_handler(bot: telegram.Bot, update: telegram.Update) -> None: message: telegram.Message = update.message chat_id = message.chat_id user_id = message.from_user.id msg_id = message.message_id if not check_admin(bot, chat_id, user_id): bot.send_message(chat_id, 'Хуй тебе, а не бан.', reply_to_message_id=msg_id) return splitted = message.text.split() if len(splitted) == 2: user_id = User.get_id_by_name(splitted[1]) else: bot.send_message(chat_id, 'Укажи юзернейм, сестра', reply_to_message_id=msg_id) return ban(bot, chat_id, user_id, msg_id=msg_id)
def find_users(message: telegram.Message, usernames: List[str]) -> Tuple[List[str], Set[int], List[str]]: """ Ищет пользователей по таким юзернеймам. Возвращает кортеж: - список найденных uid - список найденных юзернеймов - список ненайденных юзернеймов """ not_found_usernames: List[str] = [] found_uids: Set[int] = set() found_usernames: List[str] = [] for username in usernames: uid = User.get_id_by_name(username) if uid is None: # на случай если вместо юзернейма указан цифровой user_id user = User.get(username) if user is not None: uid = user.uid if uid is None: not_found_usernames.append(username) continue found_uids.add(uid) found_usernames.append(username.lstrip('@')) # ищем упоминания людей без юзернейма for entity, _ in message.parse_entities().items(): if entity.type == 'text_mention': uid = entity.user.id if uid is None: continue user = User.get(uid) if user is None: continue found_uids.add(uid) found_usernames.append(user.fullname) return not_found_usernames, found_uids, found_usernames
def on_cmd_for_user(bot, update): chat_id = update.message.chat_id user_id = update.message.from_user.id msg_id = update.message.message_id if not check_admin(bot, chat_id, user_id): bot.sendMessage(chat_id, 'Хуй тебе, а не плохишам команды включать.', reply_to_message_id=msg_id) return text = update.message.text.split() if len(text) < 2: if update.message.reply_to_message is None: bot.sendMessage(chat_id, 'Ты забыл указать команду.', reply_to_message_id=msg_id) return text = update.message.reply_to_message.text.split(' ') bot_command = text[0] else: bot_command = text[1] cmd_name = is_valid_command(bot_command) if not cmd_name: bot.sendMessage(chat_id, f'Нет такой команды: {bot_command}.', reply_to_message_id=msg_id) return reply_to_msg = update.message.reply_to_message if reply_to_msg: plohish_id = reply_to_msg.from_user.id plohish_name = '@' + reply_to_msg.from_user.username else: if len(text) < 3: on_cmd(bot, update) return plohish_name = text[2] plohish_id = User.get_id_by_name(plohish_name) if not plohish_id: bot.sendMessage(chat_id, f'Нет такого плохиша: {text[2]}', reply_to_message_id=msg_id) if check_command_is_off(chat_id, cmd_name): bot.sendMessage( chat_id, f'Команда {cmd_name} отключена у всех, без исключений') return plohish_cmd_cache_key = f'plohish_cmd:{chat_id}:{plohish_id}:{cmd_name}' disabled = cache.get(plohish_cmd_cache_key) if not disabled: bot.sendMessage( chat_id, f'Команда {cmd_name} у плохиша {plohish_name} и так работает') return cache.delete(plohish_cmd_cache_key) bot.sendMessage( chat_id, f'Команда {cmd_name} у плохиша {plohish_name} теперь работает')
def off_cmd_for_user(bot, update): chat_id = update.message.chat_id user_id = update.message.from_user.id msg_id = update.message.message_id if not check_admin(bot, chat_id, user_id): bot.sendMessage(chat_id, 'Хуй тебе, а не плохишам команды выключать.', reply_to_message_id=msg_id) return # вычисляем название команды cmd_name = None words = update.message.text.split() reply_to_msg = update.message.reply_to_message if len(words) < 2: if not reply_to_msg: bot.sendMessage(chat_id, 'Ты забыл указать команду.', reply_to_message_id=msg_id) return for entity, entity_text in reply_to_msg.parse_entities().items(): if entity.type == 'bot_command': cmd_name = entity_text break else: cmd_name = words[1] valid_cmd_name = is_valid_command(cmd_name) if not valid_cmd_name: bot.sendMessage(chat_id, f'Нет такой команды: {cmd_name}.', reply_to_message_id=msg_id) return # вычисляем кому отключать if reply_to_msg: plohish_id = reply_to_msg.from_user.id plohish_name = '@' + reply_to_msg.from_user.username else: # если не указали кому, то отключаем для всех if len(words) < 3: off_cmd(bot, update) return plohish_name = words[2] plohish_id = User.get_id_by_name(plohish_name) if not plohish_id: bot.sendMessage(chat_id, f'Нет такого плохиша: {words[2]}', reply_to_message_id=msg_id) return plohish_cmd_cache_key = f'plohish_cmd:{chat_id}:{plohish_id}:{valid_cmd_name}' disabled = cache.get(plohish_cmd_cache_key) if disabled: bot.sendMessage( chat_id, f'Команда /{valid_cmd_name} у плохиша {plohish_name} уже не работает' ) return cache.set(plohish_cmd_cache_key, True, time=MONTH) if reply_to_msg: data = { "name": '/off', "bot_command": cmd_name, "plohish_id": plohish_id, "valid_cmd_name": valid_cmd_name } keyboard = [[ InlineKeyboardButton("Отключить у всех", callback_data=(get_callback_data(data))) ]] reply_markup = InlineKeyboardMarkup(keyboard) bot.sendMessage( chat_id, f'Команда /{valid_cmd_name} у плохиша {plohish_name} теперь не работает.', reply_to_message_id=msg_id, reply_markup=reply_markup) return bot.sendMessage( chat_id, f'Команда /{valid_cmd_name} у плохиша {plohish_name} теперь не работает' )