Updated main.py

This commit is contained in:
Leander 2023-08-07 14:55:17 +02:00 committed by GitHub
parent 9a1a9d9e3e
commit 3f1db2dd3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

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