-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
31 lines (24 loc) · 870 Bytes
/
db.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
from motor.motor_asyncio import AsyncIOMotorClient
from beanie import Document, Indexed, init_beanie
from typing import Optional
import os, datetime
from pydantic import BaseModel
MONGO_URL = os.getenv('MONGO_URL')
DB_NAME = os.getenv('DB_NAME', "mozzarellabot")
db_client = AsyncIOMotorClient(MONGO_URL)
class Operation(BaseModel):
type: str
roles: None|list[int] = None
rename: None|str = None
class Token(Document):
token: Indexed(str, unique=True)
guild: Indexed(int)
email: None|str = None
operations: list[Operation]
def RoleOp(roles: list[int]):
return Operation(type="role", roles=roles)
def RenameOp(name: str):
return Operation(type="rename", rename=name.strip())
async def init_db():
# Initialize beanie with the Product document class
await init_beanie(database=db_client[DB_NAME], document_models=[Token])