2023-08-03 01:46:49 +02:00
|
|
|
import os
|
2023-08-04 03:30:56 +02:00
|
|
|
import yaml
|
2023-08-03 01:46:49 +02:00
|
|
|
import random
|
|
|
|
import string
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
from motor.motor_asyncio import AsyncIOMotorClient
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
2023-08-04 03:30:56 +02:00
|
|
|
with open('config/credits.yml', encoding='utf8') as f:
|
|
|
|
credits_config = yaml.safe_load(f)
|
|
|
|
|
2023-08-12 18:20:18 +02:00
|
|
|
## MONGODB Setup
|
|
|
|
|
|
|
|
conn = AsyncIOMotorClient(os.getenv('MONGO_URI'))
|
|
|
|
|
|
|
|
async def _get_collection(collection_name: str):
|
|
|
|
return conn['nova-core'][collection_name]
|
2023-08-03 01:46:49 +02:00
|
|
|
|
2023-08-09 11:15:49 +02:00
|
|
|
async def create(discord_id: str='') -> dict:
|
2023-08-13 17:12:35 +02:00
|
|
|
"""Add a user to the mongodb
|
|
|
|
|
|
|
|
Args:
|
|
|
|
discord_id (str): Defaults to ''.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict: The user object
|
|
|
|
"""
|
2023-08-04 03:30:56 +02:00
|
|
|
|
2023-08-03 01:46:49 +02:00
|
|
|
chars = string.ascii_letters + string.digits
|
|
|
|
|
|
|
|
infix = os.getenv('KEYGEN_INFIX')
|
|
|
|
suffix = ''.join(random.choices(chars, k=20))
|
|
|
|
prefix = ''.join(random.choices(chars, k=20))
|
|
|
|
|
|
|
|
new_api_key = f'nv-{prefix}{infix}{suffix}'
|
|
|
|
|
|
|
|
new_user = {
|
|
|
|
'api_key': new_api_key,
|
2023-08-04 03:30:56 +02:00
|
|
|
'credits': credits_config['start-credits'],
|
2023-08-03 01:46:49 +02:00
|
|
|
'role': '',
|
2023-08-10 06:07:26 +02:00
|
|
|
'level': '',
|
2023-08-03 01:46:49 +02:00
|
|
|
'status': {
|
|
|
|
'active': True,
|
|
|
|
'ban_reason': '',
|
|
|
|
},
|
|
|
|
'auth': {
|
2023-08-09 11:15:49 +02:00
|
|
|
'discord': str(discord_id),
|
2023-08-03 01:46:49 +02:00
|
|
|
'github': None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-12 18:20:18 +02:00
|
|
|
db = await _get_collection('users')
|
2023-08-06 23:20:14 +02:00
|
|
|
await db.insert_one(new_user)
|
|
|
|
user = await db.find_one({'api_key': new_api_key})
|
2023-08-03 01:46:49 +02:00
|
|
|
return user
|
|
|
|
|
|
|
|
async def by_id(user_id: str):
|
2023-08-12 18:20:18 +02:00
|
|
|
db = await _get_collection('users')
|
2023-08-06 21:42:07 +02:00
|
|
|
return await db.find_one({'_id': user_id})
|
2023-08-03 01:46:49 +02:00
|
|
|
|
|
|
|
async def by_discord_id(discord_id: str):
|
2023-08-12 18:20:18 +02:00
|
|
|
db = await _get_collection('users')
|
2023-08-09 11:06:55 +02:00
|
|
|
return await db.find_one({'auth.discord': str(int(discord_id))})
|
2023-08-03 01:46:49 +02:00
|
|
|
|
|
|
|
async def by_api_key(key: str):
|
2023-08-12 18:20:18 +02:00
|
|
|
db = await _get_collection('users')
|
2023-08-06 21:42:07 +02:00
|
|
|
return await db.find_one({'api_key': key})
|
2023-08-03 01:46:49 +02:00
|
|
|
|
|
|
|
async def update_by_id(user_id: str, update):
|
2023-08-12 18:20:18 +02:00
|
|
|
db = await _get_collection('users')
|
2023-08-06 21:42:07 +02:00
|
|
|
return await db.update_one({'_id': user_id}, update)
|
2023-08-03 01:46:49 +02:00
|
|
|
|
|
|
|
async def update_by_filter(obj_filter, update):
|
2023-08-12 18:20:18 +02:00
|
|
|
db = await _get_collection('users')
|
2023-08-06 21:42:07 +02:00
|
|
|
return await db.update_one(obj_filter, update)
|
2023-08-03 01:46:49 +02:00
|
|
|
|
|
|
|
async def delete(user_id: str):
|
2023-08-12 18:20:18 +02:00
|
|
|
db = await _get_collection('users')
|
2023-08-06 21:42:07 +02:00
|
|
|
await db.delete_one({'_id': user_id})
|
2023-08-03 01:46:49 +02:00
|
|
|
|
|
|
|
async def demo():
|
|
|
|
user = await create(69420)
|
|
|
|
print(user)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
asyncio.run(demo())
|