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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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import asyncio
import logging
import os
import shlex
import sys
from typing import Dict, List, Optional, Tuple, Union

import aiohttp
import arrow
from apscheduler.triggers.cron import CronTrigger
from apscheduler.triggers.interval import IntervalTrigger
from pyrogram import Client, enums, errors
from pyrogram.enums import ChatType
from pyrogram.session import Session
from pyrogram.storage import Storage
from pyrogram.types import Chat, Message, User


class CustomFormatter(logging.Formatter):
    # Colors
    black = "\x1b[30m"
    red = "\x1b[31m"
    green = "\x1b[32m"
    yellow = "\x1b[33m"
    blue = "\x1b[34m"
    gray = "\x1b[38m"
    # Styles
    reset = "\x1b[0m"
    bold = "\x1b[1m"

    COLORS = {
        logging.DEBUG: gray + bold,
        logging.INFO: blue + bold,
        logging.WARNING: yellow + bold,
        logging.ERROR: red,
        logging.CRITICAL: red + bold,
    }

    def format(self, record):
        log_color = self.COLORS[record.levelno]
        fmt = "(black){asctime}(reset) (levelcolor){levelname:<8}(reset) (green){name}(reset) {message}"
        fmt = fmt.replace("(black)", self.black + self.bold)
        fmt = fmt.replace("(reset)", self.reset)
        fmt = fmt.replace("(levelcolor)", log_color)
        fmt = fmt.replace("(green)", self.green + self.bold)
        formatter = logging.Formatter(fmt, "%Y-%m-%d %H:%M:%S", style="{")
        return formatter.format(record)

def get_full_name(obj: Union[User, Chat]) -> str:
    if isinstance(obj, Chat):
        if obj.type == ChatType.PRIVATE:
            return f"{obj.first_name} {obj.last_name}" if obj.last_name else obj.first_name
        return obj.title
    elif isinstance(obj, User):
        return f"{obj.first_name} {obj.last_name}" if obj.last_name else obj.first_name
    else:
        raise TypeError("obj must be User or Chat")

def format_exc(e: Exception, suffix="") -> str:
    if isinstance(e, errors.RPCError):
        return (
            f"<b>Telegram API error!</b>\n"
            f"<code>[{e.CODE} {e.ID or e.NAME}] β€” {e.MESSAGE.format(value=e.value)}</code>\n\n"
            f"<b>{suffix}</b>"
        )
    return f"<code>{e.__class__.__name__}: {e}</code>\n\n<b>{suffix}</b>"

def with_reply(func):
    async def wrapped(client: Client, message: Message):
        if not message.reply_to_message:
            await message.edit("<b>Reply to message is required</b>")
        else:
            return await func(client, message)

    return wrapped

def with_args(text: str):
    def decorator(func):
        async def wrapped(client: Client, message: Message):
            if message.text and len(message.text.split()) == 1:
                await message.edit(text)
            else:
                return await func(client, message)

        return wrapped

    return decorator

def with_premium(func):
    async def wrapped(client: Client, message: Message):
        if not (await client.get_me()).is_premium:
            await message.edit("<b>Premium account is required</b>")
        else:
            return await func(client, message)

    return wrapped

async def dpaste(code: str):
    async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
        data = {"content": code, "lexer": "python", "expires": "never"}
        async with session.post("https://dpaste.org/api/", data=data) as resp:
            if resp.status != 200:
                return "Pasting failed!"
            else:
                return (await resp.text()).replace('"', "")

async def paste_neko(code: str):
    try:
        async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session:
            async with session.post(
                "https://nekobin.com/api/documents",
                json={"content": code},
            ) as paste:
                paste.raise_for_status()
                result = await paste.json()
    except Exception:
        return await dpaste(code=code)
    else:
        return f"nekobin.com/{result['result']['key']}.py"

def get_args_raw(message: Union[Message, str], use_reply: bool = None) -> str:
    """Returns text after command.

    Args:
        message (Union[Message, str]): Message or text.

        use_reply (bool, optional): Try to get args from reply message if no args in message. Defaults to None.

    Returns:
        str: Text after command or empty string.
    """
    if isinstance(message, Message):
        text = message.text or message.caption
        args = text.split(maxsplit=1)[1] if len(text.split()) > 1 else ""

        if use_reply and not args:
            args = message.reply_to_message.text or message.reply_to_message.caption

    elif not isinstance(message, str):
        return ""

    return args or ""


def get_args(
    message: Union[Message, str], use_reply: bool = None
) -> Tuple[List[str], Dict[str, str]]:
    """Returns list of common args and a dictionary with named args.

    Args:
        message (Union[Message, str]): Message or text.

        use_reply (bool, optional): Try to get args from reply message if no args in message. Defaults to None.

    Returns:
        List[str]: List of args.
    """
    raw_args = get_args_raw(message, use_reply)

    try:
        args = list(filter(lambda x: len(x) > 0, shlex.split(raw_args)))
    except ValueError:
        return [raw_args], {}

    common_args = []
    named_args = {}

    i = 0
    while i < len(args):
        arg = args[i]
        if arg.startswith("-"):
            if i + 1 < len(args) and (
                not args[i + 1].startswith("-") or len(args[i + 1].split()) > 1
            ):
                named_args[arg] = args[i + 1]
                i += 2
            else:
                i += 1
        else:
            i += 1
        common_args.append(arg)
    return common_args, named_args


class ScheduleJob:
    def __init__(
        self,
        func: callable,
        trigger: Optional[Union[CronTrigger, IntervalTrigger]] = IntervalTrigger(seconds=3600),
        *args,
        **kwargs,
    ):
        self.func = func
        self.args = args or []
        self.kwargs = kwargs or {}
        self.id = func.__name__
        self.trigger = trigger


def get_ram_usage() -> float:
    """Returns current process tree memory usage in MB"""
    try:
        import psutil

        current_process = psutil.Process(os.getpid())
        mem = current_process.memory_info()[0] / 2.0**20
        for child in current_process.children(recursive=True):
            mem += child.memory_info()[0] / 2.0**20

        return round(mem, 1)
    except Exception:
        return 0


def get_cpu_usage() -> float:
    """Returns current process tree CPU usage in %"""
    try:
        import psutil

        current_process = psutil.Process(os.getpid())
        cpu = current_process.cpu_percent()
        for child in current_process.children(recursive=True):
            cpu += child.cpu_percent()

        return round(cpu, 1)
    except Exception:
        return 0


def humanize_seconds(seconds: Union[int, float]) -> str:
    """Returns humanized time delta from seconds"""
    current_time = arrow.get()
    target_time = current_time.shift(seconds=-seconds)
    return target_time.humanize(current_time, only_distance=True)

class Command:
    def __init__(
        self,
        name: str,
        description: Optional[str] = None,
        args: Optional[str] = None,
        aliases: Optional[List[str]] = None,
    ):
        self.name = name
        self.description = description
        self.args = args
        self.aliases = aliases
        self.hidden = False


class Module:
    def __init__(self, name: str, path: str):
        self.name = name
        self.path = path
        self.commands = {}
        self.hidden = False

    def add_command(
        self,
        command: str,
        description: Optional[str] = None,
        args: Optional[str] = None,
        aliases: Optional[List[str]] = None,
    ) -> Command:
        if command in self.commands:
            raise ValueError(f"Command {command} already exists")

        self.commands[command] = Command(command, description, args, aliases)

        return self.commands[command]

    def delete_command(self, command: str) -> None:
        if command not in self.commands:
            raise ValueError(f"Command {command} not found")

        del self.commands[command]

    def hide_command(self, command: str) -> None:
        if command not in self.commands:
            raise ValueError(f"Command {command} not found")

        self.commands[command].hidden = True

    def show_command(self, command: str) -> None:
        if command not in self.commands:
            raise ValueError(f"Command {command} not found")

        self.commands[command].hidden = False


class ModuleHelp:
    def __init__(self) -> None:
        self.modules = {}

    def add_module(self, name: str, path: str) -> Module:
        self.modules[name] = Module(name, path)

        return self.modules[name]

    def delete_module(self, name: str) -> None:
        del self.modules[name]

    def hide_module(self, name: str) -> None:
        if name not in self.modules:
            raise ValueError(f"Module {name} not found")

        self.modules[name].hidden = True

    def show_module(self, name: str) -> None:
        if name not in self.modules:
            raise ValueError(f"Module {name} not found")

        self.modules[name].hidden = False

    def get_module(self, name: str) -> Module:
        if name not in self.modules:
            raise ValueError(f"Module {name} not found")

        return self.modules[name]

    def get_module_by_path(self, path: str) -> Module:
        for module in self.modules.values():
            if module.path == path:
                return module

        raise ValueError(f"Module with path {path} not found")

    def help(self) -> List[str]:
        prefix = "."
        result = []

        help_text = f"For more help on how to use a command, type <code>{prefix}help [module]</code>\n\nAvailable Modules:\n"

        for module_name, module in sorted(self.modules.items(), key=lambda x: x[0]):
            help_text += f'β€’ {module_name.title()}: {" ".join([f"<code>{prefix + cmd_name}</code>" for cmd_name in module.commands.keys()])}\n'

            if len(help_text) >= 2048:
                result.append(help_text)
                help_text = ""

        help_text += f"\nThe number of modules in the userbot: {self.modules_count}\n"
        help_text += f"The number of commands in the userbot: {self.commands_count}"

        result.append(help_text)

        return result

    def module_help(self, module: str, full: bool = True) -> str:
        if module not in self.modules:
            raise ValueError(f"Module {module} not found")

        prefix = "."
        help_text = ""

        if full:
            help_text += f"<b>Help for |<code>{module}</code>|</b>\n\n"

        help_text += "<b>Usage:</b>\n"
        for command in self.modules[module].commands.values():
            help_text += f"<code>{prefix}{command.name}"
            if command.args:
                help_text += f" {command.args}"
            if command.description:
                help_text += f"</code> β€” <i>{command.description}</i>\n"

        return help_text

    def command_help(self, command: str) -> str:
        for module in self.modules.values():
            for cmd in module.commands.values():
                if cmd.name == command or (cmd.aliases and command in cmd.aliases):
                    command = cmd
                    break
            else:
                continue
            break
        else:
            raise ValueError(f"Command {command} not found")

        prefix = "."

        help_text = f"<b>Help for command</b> <code>{prefix}{command.name}</code>\n"
        if command.aliases:
            help_text += "<b>Aliases:</b> "
            help_text += (
                f"{' '.join([f'<code>{prefix}{alias}</code>' for alias in command.aliases])}\n"
            )

        help_text += (
            f"\n<b>Module: {module.name}</b> (<code>{prefix}help {module.name}</code>)\n\n"
        )
        help_text += f"<code>{prefix}{command.name}"

        if command.args:
            help_text += f" {command.args}"
        help_text += "</code>"
        if command.description:
            help_text += f" β€” <i>{command.description}</i>"

        return help_text

    @property
    def modules_count(self) -> int:
        return len(self.modules)

    @property
    def commands_count(self) -> int:
        return sum(len(module.commands) for module in self.modules.values())


def get_entity_url(
    entity: Union[User, Chat],
    openmessage: bool = False,
) -> str:
    """
    Get link to object, if available
    :param entity: Entity to get url of
    :param openmessage: Use tg://openmessage link for users
    :return: Link to object or empty string
    """
    return (
        (f"tg://openmessage?user_id={entity.id}" if openmessage else f"tg://user?id={entity.id}")
        if isinstance(entity, User)
        else (
            f"tg://resolve?domain={entity.username}" if getattr(entity, "username", None) else ""
        )
    )

def get_message_link(
    message: Message,
    chat: Optional[Chat] = None,
) -> str:
    """
    Get link to message
    :param message: Message to get link of
    :param chat: Chat, where message was sent
    :return: Link to message
    """
    if message.chat.type == ChatType.PRIVATE:
        return f"tg://openmessage?user_id={message.chat.id}&message_id={message.id}"

    return (
        f"https://t.me/{chat.username}/{message.id}"
        if getattr(chat, "username", False)
        else f"https://t.me/c/{chat.id}/{message.id}"
    )

async def shell_exec(
    command: str,
    executable: Optional[str] = None,
    timeout: Optional[Union[int, float]] = None,
    stdout=asyncio.subprocess.PIPE,
    stderr=asyncio.subprocess.PIPE,
) -> Tuple[int, str, str]:
    """Executes shell command and returns tuple with return code, decoded stdout and stderr"""
    process = await asyncio.create_subprocess_shell(
        cmd=command, stdout=stdout, stderr=stderr, shell=True, executable=executable
    )

    try:
        stdout, stderr = await asyncio.wait_for(process.communicate(), timeout)
    except asyncio.exceptions.TimeoutError as e:
        process.kill()
        raise e

    return process.returncode, stdout.decode(), stderr.decode()