First public release, working on autocredits system

Co-authored-by: Leander <henceiusegentoo@users.noreply.github.com>
This commit is contained in:
nsde 2023-08-06 23:20:14 +02:00
parent 83d57307cc
commit 9a1a9d9e3e
7 changed files with 25 additions and 62 deletions

View file

@ -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

View file

@ -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)

View file

@ -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):

View file

@ -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)

View file

@ -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())

View file

@ -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)

View file

@ -1 +1 @@
screen -s nova-api python run prod
screen -S nova-api python run prod