-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
152 lines (107 loc) · 4.31 KB
/
utils.py
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
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
import html
from asyncio import Lock
from datetime import datetime, timedelta
from functools import wraps
from typing import Callable, Iterable, Optional, Tuple, Union
import json
from random import randint
import httpx
from async_lru import alru_cache
from pyrogram import Client
from pyrogram.types import CallbackQuery, User
from database import cur
timeout = httpx.Timeout(40, pool=None)
hc = httpx.AsyncClient(http2=True, timeout=timeout)
lock = Lock()
def is_bot_online() -> bool:
"""Retorna `True` se o bot está online ou `False` se ele está em manutenção."""
q = cur.execute("SELECT is_on from bot_config")
return bool(q.fetchone()[0])
def is_user_banned(user_id: int) -> bool:
"""Retorna `True` se o usuário está banido ou `False` caso contrário."""
q = cur.execute("SELECT is_blacklisted from users WHERE id = ?", [user_id])
res = q.fetchone()
return bool(res[0] if res else res)
def get_lara_info() -> Tuple[str, str]:
"""Retorna uma tupla contendo o nome da lara e chave Pix."""
q = cur.execute("SELECT lara_name, lara_key from bot_config")
return q.fetchone()
def get_support_user() -> str:
"""Retorna uma string contendo o username do usuário de suporte."""
q = cur.execute("SELECT support_user from bot_config")
return q.fetchone()[0]
def get_news_user() -> str:
"""Retorna uma string contendo o username do canal de notícias."""
q = cur.execute("SELECT channel_user from bot_config")
return q.fetchone()[0]
def get_info_wallet(user_id: int) -> str:
base = """<b>🏦 DASHBOARD FINANCEIRO:</b>
<b> ├👥 Titular:</b> <code>{}</code>
<b> ├🏷 Usuário:</b> <code>{}</code>
<b> ├🆔 Identificação:</b> <code>{}</code>
<b> ├💳 Saldo Em Conta:</b> <code>R$ {}</code>
<b> └🔄 Pontos acumulados:</b> <code>{}</code>"""
rt = cur.execute(
"SELECT name_user, username, id, balance, balance_diamonds FROM users WHERE id=?", [user_id]
).fetchone()
if rt is None:
return base.format("", "Sem username", "", "", "")
else:
name_user, username, id, balance, balance_diamonds = rt
if username is None:
username = "Sem username"
return base.format(name_user, username, id, balance, balance_diamonds)
def insert_sold_balance(value: int, owner: int, type_add_saldo: str, quantity: int = 1):
cur.execute(
"""INSERT INTO sold_balance(type, value, owner, quantity) VALUES(?, ?, ?, ?)""",
[type_add_saldo, value, owner, quantity],
)
def create_mention(user: User, with_id: bool = True) -> str:
name = f"@{user.username}" if user.username else html.escape(user.first_name)
mention = f"<a href='tg://user?id={user.id}'>{name}</a>"
if with_id:
mention += f" (<code>{user.id}</code>)"
return mention
def to_hex(dec: float):
digits = "0123456789ABCDEF"
x = dec % 16
rest = dec // 16
if rest == 0:
return digits[x]
return to_hex(rest) + digits[x]
def get_crc16(payload: str):
crc = 0xFFFF
for i in range(len(payload)):
crc ^= ord(payload[i]) << 8
for j in range(8):
if (crc & 0x8000) > 0:
crc = (crc << 1) ^ 0x1021
else:
crc = crc << 1
return to_hex(crc & 0xFFFF).upper()
def create_copy_paste_pix(location: str) -> str:
copy_paste = f"00020126830014br.gov.bcb.pix2561{location}520489995303986540105802BR5921Pagseguro Internet SA6009SAO PAULO62070503***6304"
return copy_paste + get_crc16(copy_paste)
def lock_user_buy(f: Callable):
@wraps(f)
async def lock_user(c: Client, m: CallbackQuery, *args, **kwargs):
q = cur.execute(
"SELECT is_action_pending FROM users WHERE id = ?", [m.from_user.id]
).fetchone()
cur.execute(
"UPDATE users SET is_action_pending = ? WHERE id = ?",
[True, m.from_user.id],
)
if q[0]:
return await m.answer(
"Você só pode fazer uma compra/troca por vez. Por favor aguarde seu pedido anterior ser concluído.",
show_alert=True,
)
try:
return await f(c, m, *args, **kwargs)
finally:
cur.execute(
"UPDATE users SET is_action_pending = ? WHERE id = ?",
[False, m.from_user.id],
)
return lock_user