nova-cord/cord/accounts.py

167 lines
5.2 KiB
Python
Raw Normal View History

2023-08-01 02:39:20 +02:00
"""Account system functionality."""
import os
import json
import requests
import string
import random
2023-08-01 02:39:20 +02:00
import embedder
from dotenv import load_dotenv
load_dotenv()
async def request_user_by_discord_id(discord_id):
return requests.get(
2023-08-29 08:58:30 +02:00
url=f'http://localhost:2333/users?discord_id={discord_id}',
2023-08-01 02:39:20 +02:00
timeout=3,
headers={
'Content-Type': 'application/json',
'Authorization': os.getenv('CORE_API_KEY')
}
)
async def get_account(interaction):
try:
2023-08-02 17:00:18 +02:00
user_response = await request_user_by_discord_id(interaction.user.id)
2023-08-01 02:39:20 +02:00
except Exception as exc:
2023-08-02 17:00:18 +02:00
await embedder.error(interaction, """Sorry, there was an error while checking if you have an account.
2023-08-01 02:39:20 +02:00
Please report this issue to the staff!""", ephemeral=True)
raise exc
2023-08-02 17:00:18 +02:00
if user_response.status_code == 404:
await embedder.error(interaction, """You don't have an account yet!""", ephemeral=True)
2023-08-03 01:44:58 +02:00
return
2023-08-01 02:39:20 +02:00
2023-08-02 17:00:18 +02:00
return user_response.json()
async def get_info(interaction):
account = await get_account(interaction)
await embedder.info(interaction, f"""### Your account
2023-08-01 02:39:20 +02:00
This is all we have stored about your API account in our database.
Feel free to request a removal of your account by contacting the staff.
||```json
2023-08-02 17:00:18 +02:00
{json.dumps(account, indent=4)}
2023-08-01 02:39:20 +02:00
```||
(Click to reveal)
Learn more about how to use our API at **https://nova-oss.com**.
""", ephemeral=True)
2023-08-02 17:00:18 +02:00
async def get_credits(interaction):
account = await get_account(interaction)
2023-08-12 01:59:02 +02:00
if not account:
return
2023-08-03 01:44:58 +02:00
amount_credits = account["credits"]
2023-08-02 17:00:18 +02:00
await embedder.info(interaction, f"""### Your credits
2023-08-03 01:44:58 +02:00
Amount: **{amount_credits if amount_credits < 1000000 else ''}**
2023-08-02 17:00:18 +02:00
""", ephemeral=True)
async def get_credits_of(interaction, user):
2023-08-09 11:29:02 +02:00
if not interaction.user.guild_permissions.administrator:
await embedder.error(interaction, """Sorry, you don't have the permission to do that.""", ephemeral=True)
return
try:
userinfo = await request_user_by_discord_id(user.id)
except Exception as exc:
await embedder.error(interaction, """Sorry, there was an error while checking if you have an account.
Please report this issue to the staff!""", ephemeral=True)
raise exc
if userinfo.status_code == 404:
await embedder.error(interaction, """You don't have an account yet!""", ephemeral=True)
return
account = userinfo.json()
amount_credits = account["credits"]
await embedder.info(interaction, f"""### Credits of {user.name}
Amount: **{amount_credits if amount_credits < 1000000 else ''}**
""", ephemeral=True)
2023-08-09 13:04:00 +02:00
async def set_credits(interaction, user, amount):
if not interaction.user.guild_permissions.administrator:
await embedder.error(interaction, """Sorry, you don't have the permission to do that.""", ephemeral=True)
return
2023-08-29 00:29:25 +02:00
2023-08-09 13:04:00 +02:00
try:
2023-10-09 19:30:21 +02:00
account = await request_user_by_discord_id(user.id)
2023-08-09 13:04:00 +02:00
except Exception as exc:
await embedder.error(interaction, """Sorry, there was an error while checking if you have an account.
Please report this issue to the staff!""", ephemeral=True)
raise exc
2023-10-09 19:30:21 +02:00
if account.status_code == 404:
2023-08-09 13:04:00 +02:00
await embedder.error(interaction, """You don't have an account yet!""", ephemeral=True)
return
try:
2023-10-09 19:30:21 +02:00
account = account.json()
2023-08-09 13:04:00 +02:00
requests.put(
2023-10-09 19:30:21 +02:00
url=f'http://localhost:2333/users',
2023-08-09 13:04:00 +02:00
timeout=3,
headers={
'Content-Type': 'application/json',
'Authorization': os.getenv('CORE_API_KEY')
},
2023-10-09 19:30:21 +02:00
data=json.dumps({
"discord_id": account["auth"]["discord"],
"updates": {"$set": {"credits": amount}}
})
2023-08-09 13:04:00 +02:00
)
except Exception as exc:
await embedder.error(interaction, """Sorry, there was an error while setting your credits.
Please report this issue to the staff!""", ephemeral=True)
raise exc
await embedder.ok(interaction, f"""Successfully set the credits of {user.name} to **{amount}**.""", ephemeral=True)
2023-09-19 23:58:20 +02:00
async def reset_key(interaction):
try:
account = await get_account(interaction)
if not account:
await embedder.error(interaction, """It seems like you don't have an account yet.""", ephemeral=True)
requests.put(
url=f'http://localhost:2333/users',
timeout=3,
headers={
'Content-Type': 'application/json',
'Authorization': os.getenv('CORE_API_KEY')
},
data=json.dumps({
"discord_id": account["auth"]["discord"],
"updates": {"$set": {"api_key": await new_api_key()}}
})
)
await embedder.ok(interaction, f"""Successfully reset your key!""", ephemeral=True)
except Exception as exc:
await embedder.error(interaction, """Sorry, there was an error while resetting your key.
Please report this issue to the staff!""", ephemeral=True)
raise exc
async def new_api_key() -> str:
chars = string.ascii_letters + string.digits
infix = os.getenv('KEYGEN_INFIX')
suffix = ''.join(random.choices(chars, k=20))
prefix = ''.join(random.choices(chars, k=20))
new_api_key = f'nv-{prefix}{infix}{suffix}'
2023-09-20 00:00:02 +02:00
return new_api_key