Compare commits

..

No commits in common. "dbaec0332b4abc08c98bca4b922a0bc2fc6562f0" and "9a1a9d9e3e384ed9f2c090490c1a368a01e0a490" have entirely different histories.

3 changed files with 58 additions and 50 deletions

View file

@ -1,15 +1,12 @@
async def get_all_users(client): async def get_all_users(client):
users = client['nova-core']['users'] users = client['nova-core']['users']
return 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: if not settings:
users.update_many({}, {"$inc": {"credits": 2500}}) users.update_many({}, {"$inc": {"credits": 250}})
else: else:
for key, value in settings.items(): for key, value in settings.items():
users.update_many( users.update_many({'role': key}, {"$inc": {"credits": int(value)}})
{'level': key}, {"$inc": {"credits": int(value)}}) print(f'Updated {key} to {value}')

View file

@ -1,48 +1,59 @@
import asyncio
from settings import roles
import autocredits
import aiohttp
from dotenv import load_dotenv
import os import os
import asyncio
import aiohttp
import pymongo import pymongo
import autocredits
from dotenv import load_dotenv
from settings import roles
load_dotenv() load_dotenv()
CONNECTION_STRING = os.getenv("MONGO_URI") pymongo_client = pymongo.MongoClient(os.getenv('MONGO_URI'))
async def main(): async def main():
pymongo_client = pymongo.MongoClient(CONNECTION_STRING)
await update_roles(pymongo_client)
await autocredits.update_credits(pymongo_client, roles)
async def update_roles(pymongo_client):
async with aiohttp.ClientSession() as session:
try:
async with session.get('http://0.0.0.0:3224/get_roles') as response:
data = await response.json()
except aiohttp.ClientError as e:
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) users = await autocredits.get_all_users(pymongo_client)
for user in users.find(): await update_roles(users)
await autocredits.update_credits(users, roles)
async def update_roles(users):
async with aiohttp.ClientSession() as session:
try:
async with session.get('http://localhost:3224/get_roles') as response:
discord_users = 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)] + ['']
users = await autocredits.get_all_users(pymongo_client)
filtered_users = users.find({'role': {'$in': lvlroles}})
bulk_updates = []
for user in filtered_users:
discord = str(user['auth']['discord']) discord = str(user['auth']['discord'])
for id_, roles in discord_users.items(): for id_, roles in discord_users.items():
if id_ == discord: if id_ == discord:
for role in lvlroles: for role in lvlroles:
print(2, id_)
if role in roles: if role in roles:
users.update_one({'auth.discord': int(discord)}, { print(0, id_)
'$set': {'level': role}}) bulk_updates.append(pymongo.UpdateOne(
print(f"Updated {discord} to {role}") {'auth.discord': int(discord)},
{'$set': {'role': role}})
)
print(1, id_)
print(f'Updated {id_} to {role}')
break
return users if bulk_updates:
with pymongo_client:
users.bulk_write(bulk_updates)
if __name__ == "__main__": if __name__ == "__main__":
asyncio.run(main()) asyncio.run(main())

View file

@ -1,13 +1,13 @@
roles = { roles = {
'': '2500', '': '250',
'lvl10': '2800', 'lvl10': '280',
'lvl20': '3100', 'lvl20': '310',
'lvl30': '3400', 'lvl30': '340',
'lvl40': '3700', 'lvl40': '370',
'lvl50': '4000', 'lvl50': '400',
'lvl60': '4300', 'lvl60': '430',
'lvl70': '4600', 'lvl70': '460',
'lvl80': '4900', 'lvl80': '490',
'lvl90': '5200', 'lvl90': '520',
'lvl100': '5500', 'lvl100': '550',
} }