2023-08-06 23:20:14 +02:00
|
|
|
import os
|
2023-08-06 02:14:46 +02:00
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
|
|
import pymongo
|
|
|
|
|
2023-08-06 23:20:14 +02:00
|
|
|
import autocredits
|
2023-08-06 21:42:07 +02:00
|
|
|
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
2023-08-06 23:20:14 +02:00
|
|
|
from settings import roles
|
|
|
|
|
2023-08-06 02:14:46 +02:00
|
|
|
load_dotenv()
|
|
|
|
|
2023-08-06 23:20:14 +02:00
|
|
|
pymongo_client = pymongo.MongoClient(os.getenv('MONGO_URI'))
|
2023-08-06 02:14:46 +02:00
|
|
|
|
|
|
|
async def main():
|
|
|
|
users = await autocredits.get_all_users(pymongo_client)
|
|
|
|
|
|
|
|
await update_roles(users)
|
|
|
|
await autocredits.update_credits(users, roles)
|
|
|
|
|
|
|
|
async def update_roles(users):
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
try:
|
2023-08-06 23:20:14 +02:00
|
|
|
async with session.get('http://localhost:3224/get_roles') as response:
|
|
|
|
discord_users = await response.json()
|
2023-08-06 02:14:46 +02:00
|
|
|
except aiohttp.ClientError as e:
|
2023-08-06 23:20:14 +02:00
|
|
|
raise ValueError('Could not get roles.') from exc
|
2023-08-06 21:42:07 +02:00
|
|
|
|
|
|
|
lvlroles = [f'lvl{lvl}' for lvl in range(10, 110, 10)] + ['']
|
2023-08-06 23:20:14 +02:00
|
|
|
|
2023-08-06 02:14:46 +02:00
|
|
|
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'])
|
|
|
|
|
|
|
|
for id_, roles in discord_users.items():
|
|
|
|
if id_ == discord:
|
|
|
|
for role in lvlroles:
|
2023-08-06 23:20:14 +02:00
|
|
|
print(2, id_)
|
2023-08-06 02:14:46 +02:00
|
|
|
if role in roles:
|
2023-08-06 23:20:14 +02:00
|
|
|
print(0, id_)
|
|
|
|
bulk_updates.append(pymongo.UpdateOne(
|
|
|
|
{'auth.discord': int(discord)},
|
|
|
|
{'$set': {'role': role}})
|
|
|
|
)
|
|
|
|
print(1, id_)
|
2023-08-06 21:42:07 +02:00
|
|
|
print(f'Updated {id_} to {role}')
|
2023-08-06 02:14:46 +02:00
|
|
|
break
|
2023-08-06 21:42:07 +02:00
|
|
|
|
2023-08-06 02:14:46 +02:00
|
|
|
if bulk_updates:
|
|
|
|
with pymongo_client:
|
|
|
|
users.bulk_write(bulk_updates)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-08-06 23:20:14 +02:00
|
|
|
asyncio.run(main())
|