Генерируем айди, за вместо того, чтобы писать его попорядку

This commit is contained in:
Alexey Polyakov 2026-03-19 23:13:27 +03:00
parent bbee49d2d8
commit 2cf18b878a
5 changed files with 29 additions and 9 deletions

View File

@ -6,9 +6,9 @@ class SQLQueries:
INSERT_USER = """
INSERT INTO users
(phone, telegram_id, firstname, lastname, username,
(id, phone, telegram_id, firstname, lastname, username,
profileoptions, options, accountstatus, updatetime, lastseen)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
INSERT_USER_DATA = """

View File

@ -1,4 +1,7 @@
import json, time
import json
import time
import random
import hashlib
class Tools:
def __init__(self):
@ -284,3 +287,17 @@ class Tools:
async def auth_required(self, userPhone, coro, *args):
if userPhone:
await coro(*args)
def generate_user_id(self):
# Получаем время в юниксе
timestamp = int(time.time())
# Генерируем дополнительно рандомное число
random_number = random.randint(0, 9999)
# Собираем их вместе и вычисляем хеш
combined = f"{timestamp}{random_number}".encode()
unique_id = int(hashlib.md5(combined).hexdigest(), 16) % 1000000000
# Возвращаем
return unique_id

View File

@ -360,12 +360,12 @@ class Processors:
await cursor.execute(
"""
INSERT INTO users
(phone, telegram_id, firstname, lastname, username,
(id, phone, telegram_id, firstname, lastname, username,
profileoptions, options, accountstatus, updatetime, lastseen)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""",
(
phone, None, first_name, last_name, None,
self.tools.generate_user_id(), phone, None, first_name, last_name, None,
json.dumps([]), json.dumps(["ONEME"]),
0, str(now_ms), str(now_s),
)

View File

@ -6,10 +6,12 @@ from telebot.async_telebot import AsyncTeleBot
from textwrap import dedent
from common.static import Static
from common.sql_queries import SQLQueries
from common.tools import Tools
class TelegramBot:
def __init__(self, token, enabled, db_pool, whitelist_ids=None):
self.bot = AsyncTeleBot(token)
self.tools = Tools()
self.enabled = enabled
self.db_pool = db_pool
self.whitelist_ids = whitelist_ids if whitelist_ids is not None else []
@ -72,6 +74,7 @@ class TelegramBot:
await cursor.execute(
self.sql_queries.INSERT_USER,
(
self.tools.generate_user_id(),
new_phone, # phone
tg_id, # telegram_id
message.from_user.first_name[:59], # firstname

View File

@ -1,5 +1,5 @@
CREATE TABLE `users` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`id` INT PRIMARY KEY,
`phone` VARCHAR(20) UNIQUE,
`telegram_id` VARCHAR(64) UNIQUE,
`firstname` VARCHAR(59) NOT NULL,
@ -33,7 +33,7 @@ CREATE TABLE `auth_tokens` (
);
CREATE TABLE `user_data` (
`phone` VARCHAR(20) NOT NULL UNIQUE,
`phone` VARCHAR(20) NOT NULL UNIQUE PRIMARY KEY,
`chats` JSON NOT NULL,
`contacts` JSON NOT NULL,
`folders` JSON NOT NULL,
@ -42,7 +42,7 @@ CREATE TABLE `user_data` (
);
CREATE TABLE `chats` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`id` INT NOT NULL PRIMARY KEY,
`owner` INT NOT NULL,
`type` VARCHAR(16) NOT NULL,
`participants` JSON NOT NULL