mirror of
https://github.com/NovaOSS/nova-api.git
synced 2024-11-25 20:53:58 +01:00
Fixed credit system?
This commit is contained in:
parent
c07af0aed8
commit
596861d2ee
12
api/db/helpers.py
Normal file
12
api/db/helpers.py
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
import os
|
||||||
|
|
||||||
|
def find_project_root():
|
||||||
|
current_path = os.getcwd()
|
||||||
|
while not os.path.isfile(os.path.join(current_path, 'LICENSE')):
|
||||||
|
current_path = os.path.dirname(current_path)
|
||||||
|
return current_path
|
||||||
|
|
||||||
|
root = find_project_root()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
print(find_project_root())
|
|
@ -7,9 +7,14 @@ import asyncio
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from motor.motor_asyncio import AsyncIOMotorClient
|
from motor.motor_asyncio import AsyncIOMotorClient
|
||||||
|
|
||||||
|
try:
|
||||||
|
from . import helpers
|
||||||
|
except ImportError:
|
||||||
|
import helpers
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
with open('config/config.yml', encoding='utf8') as f:
|
with open(helpers.root + '/api/config/config.yml', encoding='utf8') as f:
|
||||||
credits_config = yaml.safe_load(f)
|
credits_config = yaml.safe_load(f)
|
||||||
|
|
||||||
## MONGODB Setup
|
## MONGODB Setup
|
||||||
|
@ -36,7 +41,8 @@ class UserManager:
|
||||||
return self.conn[os.getenv('MONGO_NAME', 'nova-test')][collection_name]
|
return self.conn[os.getenv('MONGO_NAME', 'nova-test')][collection_name]
|
||||||
|
|
||||||
async def get_all_users(self):
|
async def get_all_users(self):
|
||||||
return self.conn[os.getenv('MONGO_NAME', 'nova-test')]['users']
|
collection = self.conn[os.getenv('MONGO_NAME', 'nova-test')]['users']
|
||||||
|
return collection#.find()
|
||||||
|
|
||||||
async def create(self, discord_id: str = '') -> dict:
|
async def create(self, discord_id: str = '') -> dict:
|
||||||
chars = string.ascii_letters + string.digits
|
chars = string.ascii_letters + string.digits
|
||||||
|
|
|
@ -1,17 +1,22 @@
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
# Weird hack because PYTHON IS GOOD LANGUAGE :))))
|
|
||||||
sys.path.append('../')
|
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
||||||
|
sys.path.append(project_root)
|
||||||
|
|
||||||
from api.db.users import UserManager
|
from api.db.users import UserManager
|
||||||
|
|
||||||
|
manager = UserManager()
|
||||||
|
|
||||||
async def update_credits(pymongo_client, settings=None):
|
async def update_credits(settings=None):
|
||||||
manager = UserManager()
|
users = await manager.get_all_users()
|
||||||
users = await manager.get_all_users(pymongo_client)
|
|
||||||
|
|
||||||
if not settings:
|
if not settings:
|
||||||
users.update_many({}, {'$inc': {'credits': 2500}})
|
await users.update_many({}, {'$inc': {'credits': 2500}})
|
||||||
|
|
||||||
else:
|
else:
|
||||||
for key, value in settings.items():
|
for key, value in settings.items():
|
||||||
users.update_many(
|
await users.update_many(
|
||||||
{'level': key}, {'$inc': {'credits': int(value)}})
|
{'level': key}, {'$inc': {'credits': int(value)}})
|
||||||
|
|
||||||
|
get_all_users = manager.get_all_users
|
||||||
|
|
|
@ -9,13 +9,14 @@ from settings import roles
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
mongo = pymongo.MongoClient(os.getenv('MONGO_URI'))
|
mongo = pymongo.MongoClient(os.getenv('MONGO_URI'))
|
||||||
|
|
||||||
await update_roles(mongo)
|
await update_roles()
|
||||||
await autocredits.update_credits(mongo, roles)
|
await autocredits.update_credits(roles)
|
||||||
|
|
||||||
async def update_roles(mongo):
|
async def update_roles():
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
try:
|
try:
|
||||||
async with session.get('http://0.0.0.0:3224/get_roles') as response:
|
async with session.get('http://0.0.0.0:3224/get_roles') as response:
|
||||||
|
@ -25,9 +26,15 @@ async def update_roles(mongo):
|
||||||
return
|
return
|
||||||
|
|
||||||
level_role_names = [f'lvl{lvl}' for lvl in range(10, 110, 10)]
|
level_role_names = [f'lvl{lvl}' for lvl in range(10, 110, 10)]
|
||||||
users = await autocredits.get_all_users(mongo)
|
users = await autocredits.get_all_users()
|
||||||
|
users = users.find({})
|
||||||
|
users = await users.to_list(length=None)
|
||||||
|
|
||||||
|
|
||||||
|
for user in users:
|
||||||
|
if not 'auth' in user:
|
||||||
|
continue
|
||||||
|
|
||||||
for user in users.find():
|
|
||||||
discord = str(user['auth']['discord'])
|
discord = str(user['auth']['discord'])
|
||||||
|
|
||||||
for user_id, role_names in discord_users.items():
|
for user_id, role_names in discord_users.items():
|
||||||
|
@ -48,6 +55,5 @@ def launch():
|
||||||
with open('rewards/last_update.txt', 'w') as f:
|
with open('rewards/last_update.txt', 'w') as f:
|
||||||
f.write(str(time.time()))
|
f.write(str(time.time()))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
launch()
|
launch()
|
||||||
|
|
Loading…
Reference in a new issue