From 9a1a9d9e3e384ed9f2c090490c1a368a01e0a490 Mon Sep 17 00:00:00 2001 From: nsde Date: Sun, 6 Aug 2023 23:20:14 +0200 Subject: [PATCH] First public release, working on autocredits system Co-authored-by: Leander --- api/config/credits.yml | 4 ---- api/core.py | 4 ++-- api/db/users.py | 7 ++++--- api/transfer.py | 2 -- rewardsystem/main.py | 30 ++++++++++++++++++------------ rewardsystem/role_bot.py | 38 -------------------------------------- screen.sh | 2 +- 7 files changed, 25 insertions(+), 62 deletions(-) delete mode 100644 rewardsystem/role_bot.py diff --git a/api/config/credits.yml b/api/config/credits.yml index 7025e66..5582a84 100644 --- a/api/config/credits.yml +++ b/api/config/credits.yml @@ -17,7 +17,3 @@ bonuses: admin: 0.3 helper: 0.4 booster: 0.5 - - -rewards: - day: 250 # scales with level. More in ../../credit_management/settings.py \ No newline at end of file diff --git a/api/core.py b/api/core.py index 2ed74e3..f266147 100644 --- a/api/core.py +++ b/api/core.py @@ -41,8 +41,8 @@ async def new_user_webhook(user: dict) -> None: ) embed.add_field(name='ID', value=str(user['_id']), inline=False) - embed.add_field(name='Discord', value=user['auth']['discord']) - embed.add_field(name='Github', value=user['auth']['github']) + embed.add_field(name='Discord', value=user['auth']['discord'] or '-') + embed.add_field(name='Github', value=user['auth']['github'] or '-') dhook.send(embed=embed) diff --git a/api/db/users.py b/api/db/users.py index 5003250..ffcbbd2 100644 --- a/api/db/users.py +++ b/api/db/users.py @@ -30,6 +30,7 @@ async def create(discord_id: int=0) -> dict: 'api_key': new_api_key, 'credits': credits_config['start-credits'], 'role': '', + 'level': 0, 'status': { 'active': True, 'ban_reason': '', @@ -40,9 +41,9 @@ async def create(discord_id: int=0) -> dict: } } - await _get_mongo('users').insert_one(new_user) - - user = await _get_mongo('users').find_one({'api_key': new_api_key}) + db = await _get_mongo('users') + await db.insert_one(new_user) + user = await db.find_one({'api_key': new_api_key}) return user async def by_id(user_id: str): diff --git a/api/transfer.py b/api/transfer.py index f1fe040..3159941 100644 --- a/api/transfer.py +++ b/api/transfer.py @@ -98,8 +98,6 @@ async def handle(incoming_request): error = await errors.error(400, 'The request contains content which violates this model\'s policies.', 'We currently don\'t support any NSFW models.') return error - return - role_cost_multiplier = credits_config['bonuses'].get(user['role'], 1) cost = round(cost * role_cost_multiplier) diff --git a/rewardsystem/main.py b/rewardsystem/main.py index 92435f3..59a5a39 100644 --- a/rewardsystem/main.py +++ b/rewardsystem/main.py @@ -1,17 +1,17 @@ -import asyncio -import autocredits -import aiohttp import os +import asyncio +import aiohttp import pymongo -from settings import roles +import autocredits from dotenv import load_dotenv +from settings import roles + load_dotenv() -CONNECTION_STRING = os.getenv("CONNECTION_STRING") -pymongo_client = pymongo.MongoClient(CONNECTION_STRING) +pymongo_client = pymongo.MongoClient(os.getenv('MONGO_URI')) async def main(): users = await autocredits.get_all_users(pymongo_client) @@ -22,13 +22,13 @@ async def main(): async def update_roles(users): async with aiohttp.ClientSession() as session: try: - async with session.get('http://localhost:50000/get_roles') as response: - data = await response.json() + 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 + raise ValueError('Could not get roles.') from exc lvlroles = [f'lvl{lvl}' for lvl in range(10, 110, 10)] + [''] - discord_users = data + users = await autocredits.get_all_users(pymongo_client) filtered_users = users.find({'role': {'$in': lvlroles}}) @@ -40,8 +40,14 @@ async def update_roles(users): for id_, roles in discord_users.items(): if id_ == discord: for role in lvlroles: + print(2, id_) if role in roles: - bulk_updates.append(pymongo.UpdateOne({'auth.discord': int(discord)}, {'$set': {'role': role}})) + print(0, id_) + bulk_updates.append(pymongo.UpdateOne( + {'auth.discord': int(discord)}, + {'$set': {'role': role}}) + ) + print(1, id_) print(f'Updated {id_} to {role}') break @@ -50,4 +56,4 @@ async def update_roles(users): users.bulk_write(bulk_updates) if __name__ == "__main__": - asyncio.run(main()) + asyncio.run(main()) \ No newline at end of file diff --git a/rewardsystem/role_bot.py b/rewardsystem/role_bot.py deleted file mode 100644 index 954e6fa..0000000 --- a/rewardsystem/role_bot.py +++ /dev/null @@ -1,38 +0,0 @@ -import nextcord -import aiohttp.web -from dotenv import load_dotenv -import os - -load_dotenv() - -TOKEN = os.getenv("BOT_TOKEN") -GUILD_ID = os.getenv("GUILD_ID") - -client = nextcord.Client(intents=nextcord.Intents.all()) - -@client.event -async def on_ready(): - # start webserver using aiohttp - app = aiohttp.web.Application() - - async def get_roles(request): - return aiohttp.web.json_response(await get_userinfo()) - - app.router.add_get('/get_roles', get_roles) - - runner = aiohttp.web.AppRunner(app) - await runner.setup() - site = aiohttp.web.TCPSite(runner, 'localhost', 50000) - await site.start() - - print('Bot is ready') - - -async def get_userinfo(): - guild = client.get_guild(int(GUILD_ID)) - members = guild.members - - user_roles = {member.id: [role.name for role in member.roles] for member in members} - return user_roles - -client.run(TOKEN) \ No newline at end of file diff --git a/screen.sh b/screen.sh index 18923f7..7cb5b12 100755 --- a/screen.sh +++ b/screen.sh @@ -1 +1 @@ -screen -s nova-api python run prod +screen -S nova-api python run prod