From 63f03e7cbd86ef0634564939368f8e8cf1229b4c Mon Sep 17 00:00:00 2001 From: henceiusegentoo Date: Mon, 7 Aug 2023 01:03:07 +0200 Subject: [PATCH] Fixed that shit --- rewardsystem/autocredits.py | 11 ++++--- rewardsystem/main.py | 59 +++++++++++++++---------------------- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/rewardsystem/autocredits.py b/rewardsystem/autocredits.py index 10afb91..47c480a 100644 --- a/rewardsystem/autocredits.py +++ b/rewardsystem/autocredits.py @@ -1,12 +1,15 @@ async def get_all_users(client): users = client['nova-core']['users'] return users - -async def update_credits(users, settings=None): + + +async def update_credits(pymongo_client, settings=None): + users = await get_all_users(pymongo_client) + if not settings: users.update_many({}, {"$inc": {"credits": 250}}) else: for key, value in settings.items(): - users.update_many({'role': key}, {"$inc": {"credits": int(value)}}) - print(f'Updated {key} to {value}') + users.update_many( + {'level': key}, {"$inc": {"credits": int(value)}}) diff --git a/rewardsystem/main.py b/rewardsystem/main.py index 59a5a39..f4b943a 100644 --- a/rewardsystem/main.py +++ b/rewardsystem/main.py @@ -1,59 +1,48 @@ -import os import asyncio -import aiohttp -import pymongo - -import autocredits - -from dotenv import load_dotenv - from settings import roles +import autocredits +import aiohttp +from dotenv import load_dotenv +import os +import pymongo load_dotenv() -pymongo_client = pymongo.MongoClient(os.getenv('MONGO_URI')) +CONNECTION_STRING = os.getenv("MONGO_URI") + async def main(): - users = await autocredits.get_all_users(pymongo_client) + pymongo_client = pymongo.MongoClient(CONNECTION_STRING) - await update_roles(users) - await autocredits.update_credits(users, roles) + await update_roles(pymongo_client) + await autocredits.update_credits(pymongo_client, roles) -async def update_roles(users): + +async def update_roles(pymongo_client): async with aiohttp.ClientSession() as session: try: - async with session.get('http://localhost:3224/get_roles') as response: - discord_users = await response.json() + async with session.get('http://0.0.0.0:3224/get_roles') as response: + data = await response.json() except aiohttp.ClientError as e: - raise ValueError('Could not get roles.') from exc - - lvlroles = [f'lvl{lvl}' for lvl in range(10, 110, 10)] + [''] + print(f"Error: {e}") + return + lvlroles = [f"lvl{lvl}" for lvl in range(10, 110, 10)] + discord_users = data users = await autocredits.get_all_users(pymongo_client) - filtered_users = users.find({'role': {'$in': lvlroles}}) - - bulk_updates = [] - for user in filtered_users: + for user in users.find(): discord = str(user['auth']['discord']) for id_, roles in discord_users.items(): if id_ == discord: for role in lvlroles: - print(2, id_) if role in roles: - print(0, id_) - bulk_updates.append(pymongo.UpdateOne( - {'auth.discord': int(discord)}, - {'$set': {'role': role}}) - ) - print(1, id_) - print(f'Updated {id_} to {role}') - break + users.update_one({'auth.discord': int(discord)}, { + '$set': {'level': role}}) + print(f"Updated {discord} to {role}") - if bulk_updates: - with pymongo_client: - users.bulk_write(bulk_updates) + return users if __name__ == "__main__": - asyncio.run(main()) \ No newline at end of file + asyncio.run(main())