discord_intro_quiz_bot/web/init_account.py

29 lines
995 B
Python
Raw Permalink Normal View History

2024-08-03 10:29:46 +00:00
import os
import secrets
import bcrypt
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Base, Account, ActiveSession, ChannelId
def create_initial_account(db_path):
if not os.path.exists(os.path.dirname(db_path)):
os.makedirs(os.path.dirname(db_path), exist_ok=True)
engine = create_engine(f"sqlite:///{db_path}")
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
if session.query(Account).count() == 0:
user_id = secrets.token_hex(8)
password = secrets.token_hex(16)
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
new_account = Account(user_id=user_id, password=hashed_password.decode('utf-8'))
session.add(new_account)
session.commit()
with open("/web/data/initial_account.txt", "w") as f:
f.write(f"User ID: {user_id}\nPassword: {password}\n")
session.close()