-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.py
More file actions
62 lines (45 loc) · 2.1 KB
/
database.py
File metadata and controls
62 lines (45 loc) · 2.1 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
import os
import pymongo
import dotenv
from infisical_sdk import InfisicalSDKClient
from pymongo.server_api import ServerApi
from Guild import Guild
dotenv.load_dotenv()
print("[INFO] Connecting to Infisical")
i_client = InfisicalSDKClient(host="https://app.infisical.com")
i_client.auth.universal_auth.login(client_id=os.environ.get("I_CLIENT_ID"),
client_secret=os.environ.get("I_CLIENT_SECRET"))
i_response = i_client.secrets.get_secret_by_name(secret_name="MONGODB_CLOUD_USERNAME",
project_id="fcdb0041-ad1d-4fa6-854d-0745640829d0",
environment_slug="prod", secret_path="/")
print("[INFO] Received MONGODB_CLOUD_USERNAME")
database_username = i_response.secret.secret_value
i_response2 = i_client.secrets.get_secret_by_name(secret_name="MONGODB_CLOUD_PASSWORD",
project_id="fcdb0041-ad1d-4fa6-854d-0745640829d0",
environment_slug="prod", secret_path="/")
database_password = i_response2.secret.secret_value
print("[INFO] Received MONGODB_CLOUD_USERNAME")
uri = f"mongodb+srv://{database_username}:{database_password}@cusecbot.z72vt.mongodb.net/?retryWrites=true&w=majority&appName=CUSECBot"
my_client = pymongo.MongoClient(uri, server_api=ServerApi('1'))
my_client.admin.command('ping')
mydb = my_client["CUSECBot"]
guilds = mydb["guilds"]
def add_update_guild(guild: Guild):
global guilds
if get_guild_by_guild(guild) is None:
result = guilds.insert_one(guild.to_json())
else:
db_filter = {"_id": guild.guild_id}
guild_json = guild.to_json()
result = guilds.replace_one(db_filter, guild_json, True)
return result
def get_guild_by_guild(guild: Guild):
global guilds
result = guilds.find_one({"_id": guild.guild_id})
guild_obj = Guild.from_json(result)
return guild_obj
def get_guild_by_id(guild_id: int):
global guilds
result = guilds.find_one({"_id": guild_id})
guild_obj = Guild.from_json(result)
return guild_obj