nova-api/rewardsystem/main.py

49 lines
1.3 KiB
Python
Raw Normal View History

2023-08-06 02:14:46 +02:00
import asyncio
2023-08-07 14:55:17 +02:00
from settings import roles
import autocredits
2023-08-07 14:55:17 +02:00
import aiohttp
2023-08-06 21:42:07 +02:00
from dotenv import load_dotenv
2023-08-07 14:55:17 +02:00
import os
import pymongo
2023-08-06 02:14:46 +02:00
load_dotenv()
2023-08-07 14:55:17 +02:00
CONNECTION_STRING = os.getenv("MONGO_URI")
2023-08-06 02:14:46 +02:00
async def main():
2023-08-07 14:55:17 +02:00
pymongo_client = pymongo.MongoClient(CONNECTION_STRING)
await update_roles(pymongo_client)
await autocredits.update_credits(pymongo_client, roles)
2023-08-06 02:14:46 +02:00
2023-08-07 14:55:17 +02:00
async def update_roles(pymongo_client):
2023-08-06 02:14:46 +02:00
async with aiohttp.ClientSession() as session:
try:
2023-08-07 14:55:17 +02:00
async with session.get('http://0.0.0.0:3224/get_roles') as response:
data = await response.json()
2023-08-06 02:14:46 +02:00
except aiohttp.ClientError as e:
2023-08-07 14:55:17 +02:00
print(f"Error: {e}")
return
2023-08-07 14:55:17 +02:00
lvlroles = [f"lvl{lvl}" for lvl in range(10, 110, 10)]
discord_users = data
2023-08-06 02:14:46 +02:00
users = await autocredits.get_all_users(pymongo_client)
2023-08-07 14:55:17 +02:00
for user in users.find():
2023-08-06 02:14:46 +02:00
discord = str(user['auth']['discord'])
for id_, roles in discord_users.items():
if id_ == discord:
for role in lvlroles:
if role in roles:
2023-08-07 14:55:17 +02:00
users.update_one({'auth.discord': int(discord)}, {
'$set': {'level': role}})
print(f"Updated {discord} to {role}")
return users
2023-08-06 02:14:46 +02:00
if __name__ == "__main__":
2023-08-07 14:55:17 +02:00
asyncio.run(main())