Example #1
0
def send_short_top(message, language, format_string, start='', sort_key=lambda x: True):
    """Send a short version of a top for non-admins"""
    database = Database()
    # Declaring variables
    system = database.get('chats', ('id', message.chat.id))['system']
    formating_dict = {'m_emo': get_system_configs(system)['money_emoji'],
                      'bot_money': database.get('systems', ('id', system))['money']}
    text = start.format(**formating_dict)
    members = database.get_many('members', ('system', system))
    members = list(filter(lambda x: sort_key(x) != 0, members))
    members.sort(key=sort_key, reverse=True)
    person_index = 0
    for person_index in range(1, len(members) + 1):
        if members[person_index - 1]['id'] == message.from_user.id:
            break
    # Main loop
    for index in range(1, len(members) + 1):
        member = members[index - 1]
        p_link = link_text_wrapper(html_cleaner(member["nickname"]), f't.me/{member["username"]}')
        formating_dict.update(member)
        formating_dict.update({'index': index, 'p_link': p_link, 'day': member['day_birthday']})
        if '{month}' in format_string:
            formating_dict['month'] = MONTHS_GENITIVE[member['month_birthday'] - 1][language]
        if index <= 5 or abs(index - person_index) <= 2:
            text += format_string.format(**formating_dict)
        elif '.\n.\n.\n' not in text and person_index >= 9:
            text += '.\n.\n.\n'
    if text:
        send(message.chat.id, text, parse_mode='HTML')
    else:
        reply(message, "Ничего нет!")
def send_some_top(message,
                  language,
                  format_string,
                  start='',
                  sort_key=lambda x: True):
    """Send a full version of a top for admins"""
    database = Database()
    # Declaring variables
    sent = False
    system = database.get('chats', ('id', message.chat.id))['system']
    formating_dict = {
        'm_emo': get_system_configs(system)['money_emoji'],
        'bot_money': database.get('systems', ('id', system))['money']
    }
    text = start.format(**formating_dict)
    members = database.get_many('members', ('system', system))
    members = list(filter(lambda x: sort_key(x) != 0, members))
    members.sort(key=sort_key, reverse=True)
    if len(members) > 50:
        target_chat = message.from_user.id
    else:
        target_chat = message.chat.id
    # Main loop
    for index in range(1, len(members) + 1):
        member = members[index - 1]
        p_link = link_text_wrapper(html_cleaner(member["nickname"]),
                                   f't.me/{member["username"]}')
        formating_dict.update(member)
        formating_dict.update({
            'index': index,
            'p_link': p_link,
            'day': member['day_birthday']
        })
        if '{month}' in format_string:
            formating_dict['month'] = MONTHS_GENITIVE[member['month_birthday']
                                                      - 1][language]
        text += format_string.format(**formating_dict)
        if index % 50 == 0:
            sent = send(target_chat, text, parse_mode='HTML')
            text = ''
    sent = send(target_chat, text, parse_mode='HTML') or sent
    if len(members) > 50:
        if sent:
            reply(message, "Выслал инфу в личку")
        else:
            reply(message, "Сначала запусти меня в личных сообщениях")
    elif not sent:
        reply(message, "Ничего нет!")