nova-api/rewards/main.py

61 lines
1.5 KiB
Python
Raw Normal View History

import os
import time
import aiohttp
import pymongo
import asyncio
import autocredits
from settings import roles
from dotenv import load_dotenv
load_dotenv()
2023-08-19 13:30:46 +02:00
async def main():
2023-08-19 13:30:46 +02:00
await update_roles()
await autocredits.update_credits(roles)
2023-08-19 13:30:46 +02:00
async def update_roles():
2023-10-12 00:03:15 +02:00
"""Updates the roles of all users."""
async with aiohttp.ClientSession() as session:
try:
async with session.get('http://0.0.0.0:3224/get_roles') as response:
discord_users = await response.json()
except aiohttp.ClientError as e:
print(f'Error: {e}')
return
level_role_names = [f'lvl{lvl}' for lvl in range(10, 110, 10)]
2023-08-19 13:38:03 +02:00
users_doc = await autocredits.get_all_users()
users = users_doc.find({})
2023-08-19 13:30:46 +02:00
users = await users.to_list(length=None)
for user in users:
if not 'auth' in user:
continue
discord = str(user['auth']['discord'])
for user_id, role_names in discord_users.items():
if user_id == discord:
for role in level_role_names:
if role in role_names:
2023-08-19 13:38:03 +02:00
users_doc.update_one(
{'auth.discord': discord},
{'$set': {'level': role}}
)
2023-08-19 13:38:03 +02:00
print(f'Updated {discord} to {role}')
return users
def launch():
asyncio.run(main())
2023-10-06 09:37:16 +02:00
with open(os.path.join('rewards', 'last_update.txt'), 'w', encoding='utf8') as f:
f.write(str(time.time()))
if __name__ == '__main__':
launch()