-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst.py
More file actions
91 lines (75 loc) · 3.05 KB
/
first.py
File metadata and controls
91 lines (75 loc) · 3.05 KB
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
#############################################################
######### ПРОГРАММА ДЛЯ ЗАПИСИ ПЕРВОГО ПОЛЬЗОВАТЕЛЯ #########
#############################################################
import datetime
import os
from pymongo import MongoClient
from dotenv import load_dotenv
load_dotenv()
MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017/")
DB_NAME = os.getenv("DB_NAME", "telegram_bot")
def connect_to_mongodb():
try:
client = MongoClient(MONGO_URI)
client.admin.command('ping')
db = client[DB_NAME]
return client, db
except Exception as e:
print(f"Ошибка подключения к MongoDB: {e}")
return None, None
def check_user_exists(collection, user_id):
return collection.count_documents({"id": user_id}) > 0
def create_user(collection, user_data):
try:
collection.insert_one(user_data)
return True
except Exception as e:
print(f"Ошибка при создании пользователя: {e}")
return False
def main():
print("Добавление администратора в MongoDB\n")
try:
client, db = connect_to_mongodb()
if client is None:
print("Не удалось подключиться к MongoDB. Проверьте параметры подключения.")
return
users_collection = db['users']
users_collection.create_index("id", unique=True)
try:
user_id = int(input("Введите Telegram ID: ").strip())
except ValueError:
print("ID должен быть числом!")
client.close()
return
if check_user_exists(users_collection, user_id):
print("Пользователь с таким ID уже существует.")
client.close()
return
password = input("Введите пароль: ").strip()
username = input("Введите имя пользователя: ").strip()
created_at = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
user = {
"id": user_id,
"password": password,
"status": "admin",
"username": username,
"authorized": False,
"folders": 0,
"created_at": created_at,
"folders_limit": 0,
"addition": True,
"download": True,
"rename": True,
"delete": True
}
if create_user(users_collection, user):
print(f"Пользователь {username} (ID: {user_id}) успешно добавлен как администратор.")
else:
print("Не удалось добавить пользователя.")
except Exception as e:
print(f"Произошла ошибка: {e}")
finally:
if 'client' in locals():
client.close()
if __name__ == "__main__":
main()