File size: 3,285 Bytes
78b07ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# G: Glass Buttons

from math import ceil

from pyrogram.types import InlineKeyboardButton

from Hellbot.core import ENV, Symbols, db, Config


def gen_inline_keyboard(collection: list, row: int = 2) -> list[list[InlineKeyboardButton]]:
    keyboard = []
    for i in range(0, len(collection), row):
        kyb = []
        for x in collection[i : i + row]:
            button = btn(*x)
            kyb.append(button)
        keyboard.append(kyb)
    return keyboard


def btn(text, value, type="callback_data") -> InlineKeyboardButton:
    return InlineKeyboardButton(text, **{type: value})


async def gen_inline_help_buttons(page: int, plugins: list) -> tuple[list, int]:
    buttons = []
    column = await db.get_env(ENV.btn_in_help) or 5
    column = int(column)
    emoji = await db.get_env(ENV.help_emoji) or "✧"
    pairs = list(map(list, zip(plugins[::2], plugins[1::2])))

    if len(plugins) % 2 == 1:
        pairs.append([plugins[-1]])

    max_pages = ceil(len(pairs) / column)
    pairs = [pairs[i : i + column] for i in range(0, len(pairs), column)]

    for pair in pairs[page]:
        btn_pair = []
        for i, plugin in enumerate(pair):
            if i % 2 == 0:
                btn_pair.append(
                    InlineKeyboardButton(f"{emoji} {plugin}", f"help_menu:{page}:{plugin}")
                )
            else:
                btn_pair.append(
                    InlineKeyboardButton(f"{plugin} {emoji}", f"help_menu:{page}:{plugin}")
                )
        buttons.append(btn_pair)

    buttons.append(
        [
            InlineKeyboardButton(
                Symbols.previous, f"help_page:{(max_pages - 1) if page == 0 else (page - 1)}",
            ),
            InlineKeyboardButton(
                Symbols.close, "help_data:c"
            ),
            InlineKeyboardButton(
                Symbols.next, f"help_page:{0 if page == (max_pages - 1) else (page + 1)}",
            ),
        ]
    )

    return buttons, max_pages


async def gen_bot_help_buttons() -> list[list[InlineKeyboardButton]]:
    buttons = []
    plugins = sorted(Config.BOT_CMD_MENU)
    emoji = await db.get_env(ENV.help_emoji) or "✧"
    pairs = list(map(list, zip(plugins[::2], plugins[1::2])))

    if len(plugins) % 2 == 1:
        pairs.append([plugins[-1]])

    for pair in pairs:
        btn_pair = []
        for i, plugin in enumerate(pair):
            if i % 2 == 0:
                btn_pair.append(
                    InlineKeyboardButton(f"{emoji} {plugin}", f"bot_help_menu:{plugin}")
                )
            else:
                btn_pair.append(
                    InlineKeyboardButton(f"{plugin} {emoji}", f"bot_help_menu:{plugin}")
                )
        buttons.append(btn_pair)

    buttons.append(
        [
            InlineKeyboardButton("🏠", "help_data:start"),
            InlineKeyboardButton(Symbols.close, "help_data:botclose"),
        ]
    )

    return buttons


def start_button() -> list[list[InlineKeyboardButton]]:
    return [
        [
            InlineKeyboardButton("⚙️ Help", "help_data:bothelp"),
            InlineKeyboardButton("Source 📦", "help_data:source"),
        ]
    ]