[+] Standard server field getter

This commit is contained in:
Charles Le Maux 2024-09-13 04:00:14 +02:00
parent 8b90dc2c3d
commit dec704f3c5

View File

@ -6,7 +6,7 @@ import firebase_admin
import jwt
from firebase_admin import auth, credentials, firestore
from google.api_core.exceptions import Aborted, DataLoss, NotFound, OutOfRange, PermissionDenied, ResourceExhausted
from google.cloud.firestore_v1 import FieldFilter
from google.cloud.firestore_v1 import FieldFilter, DocumentReference
import file_manager
from generic_executor import mc_manager
@ -133,6 +133,21 @@ def create_server(user_id: str, server_name: str, version: str, port: int, frame
"enableCommandBlock": "false"})
def get_server_field(user_id: str, name: str, field_name: str) -> Union[str, bool, None]:
try:
server_doc: DocumentReference = firestore_database.document(f'users/{user_id}/servers/{name}')
doc = server_doc.get()
if doc.exists:
field = doc.to_dict().get(field_name)
return field
else:
return None
except (NotFound, PermissionDenied, Aborted, ResourceExhausted,
OutOfRange, DataLoss, TypeError, Exception, ValueError) as e:
log_exception_to_firestore(e, user_id, {"function": "get_server_field", "name": name, "field": field_name})
return None
def delete_server(user_id: str, server_name: str):
user_ref = firestore_database.collection('users').document(user_id)
servers_ref = user_ref.collection('servers')