summaryrefslogtreecommitdiff
path: root/src/home/bot/util.py
blob: 4f80a67e8d547bc81482e14eaa117df3fcadcbc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from telegram import User
from .lang import LangStrings

_strings = {
    'en': LangStrings(
        usage='Usage',
        arguments='Arguments'
    ),
    'ru': LangStrings(
        usage='Использование',
        arguments='Аргументы'
    )
}


def command_usage(command: str, arguments: dict, language='en') -> str:
    if language not in _strings:
        raise ValueError('unsupported language')

    blocks = []
    argument_names = []
    argument_lines = []
    for k, v in arguments.items():
        argument_names.append(k)
        argument_lines.append(
            f'<code>{k}</code>: {v}'
        )

    command = f'/{command}'
    if argument_names:
        command += ' ' + ' '.join(argument_names)

    blocks.append(
        f'<b>{_strings[language]["usage"]}</b>\n'
        f'<code>{command}</code>'
    )

    if argument_lines:
        blocks.append(
            f'<b>{_strings[language]["arguments"]}</b>\n' + '\n'.join(argument_lines)
        )

    return '\n\n'.join(blocks)


def user_any_name(user: User) -> str:
    name = [user.first_name, user.last_name]
    name = list(filter(lambda s: s is not None, name))
    name = ' '.join(name).strip()

    if not name:
        name = user.username

    if not name:
        name = str(user.id)

    return name