diff --git a/basicbot.py b/basicbot.py index ae7429f5..49eb5a2b 100644 --- a/basicbot.py +++ b/basicbot.py @@ -109,17 +109,25 @@ Sintassi: `/discord `""" await update.message.reply(bot, "Richiesta inviata.") -# TODO: delete me -async def login(bot, update, arguments): - """Fai il login usando i dati del database Royal Games. +async def sync(bot, update, arguments): + """Connetti il tuo account Telegram al Database Royal Games. -Sintassi: `/login `""" +Sintassi: `/sync `""" if len(arguments) != 2: - await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/login `") + await update.message.chat.send_message(bot, "⚠ Sintassi del comando non valida.\n`/sync `") return - logged_user = database.login(arguments[0], arguments[1]) + # Try to login + session, logged_user = database.login(arguments[0], arguments[1]) + # Check if the login is successful if logged_user is not None: - await update.message.chat.send_message(bot, f"Login riuscito: {logged_user}") + # Add the telegram_id to the user if it's missing + if logged_user.telegram_id is None: + # Handle duplicate + logged_user.telegram_id = update.message.sent_from.user_id + session.commit() + await update.message.chat.send_message(bot, f"Sincronizzazione riuscita: {logged_user}") + else: + await update.message.chat.send_message(bot, "⚠ L'account è già stato sincronizzato.") else: await update.message.chat.send_message(bot, "⚠ Username o password non validi.") @@ -128,6 +136,6 @@ if __name__ == "__main__": b.commands["leggi"] = leggi b.commands["diario"] = diario b.commands["discord"] = discord - b.commands["login"] = login + b.commands["sync"] = sync b.commands["help"] = help b.run() \ No newline at end of file diff --git a/database.py b/database.py index 1053901e..e0cf0e79 100644 --- a/database.py +++ b/database.py @@ -4,49 +4,78 @@ from sqlalchemy.ext.declarative import declarative_base import bcrypt # Initialize the database -engine = create_engine("sqlite:///tempdb.sqlite") +engine = create_engine("sqlite:///db.sqlite") Base = declarative_base() Session = sessionmaker(bind=engine) -class Member(Base): +class User(Base): __tablename__ = "members" id = Column(Integer, primary_key=True) username = Column(String, unique=True, nullable=False) password = Column(String, nullable=False) royal = Column(Boolean, nullable=False) + telegram_id = Column(Integer, unique=True) def __str__(self): return f"{self.id} - {self.username}" Base.metadata.create_all(engine) -def create_member(username, password, royal=False): - """Create a new member and add it to the database.""" +def create_user(username, password, royal=False): + """Create a new user and add it to the database.""" # Create a new session session = Session() # Hash the password with bcrypt hashed_password = bcrypt.hashpw(password.encode("utf8"), bcrypt.gensalt()) - # Create a new member - new_member = Member(username=username, password=hashed_password, royal=royal) + # Create a new user + new_member = User(username=username, password=hashed_password, royal=royal) # Add the newly created member to the session session.add(new_member) # Commit the changes session.commit() def login(username, password): - """Try to login using the database password.""" + """Try to login using the database password. The session is always returned, while the user object is returned if the login is successful.""" # Create a new session session = Session() # Find the matching user - users = session.query(Member).filter(Member.username == username).all() + users = session.query(User).filter(User.username == username).all() # No user with a matching username found if len(users) == 0: - return None + return session, None else: db_user = users[0] - # Test the password and return the user if successful + # Test the password and return the session and the user if successful if bcrypt.hashpw(password.encode("utf8"), db_user.password) == db_user.password: - return db_user + # TODO: Maybe there's a better way to do this? + return session, db_user else: - return None + return session, None + + +def init_royal_db(): + create_user("steffo", "uno", True) + create_user("adry", "due", True) + create_user("cate", "tre", True) + create_user("protoh", "quattro", True) + create_user("infopz", "cinque", True) + create_user("kappa", "sei", True) + create_user("manu", "sette", True) + create_user("frank", "otto", True) + create_user("paltri", "nove", True) + create_user("mestakes", "dieci", True) + create_user("tauei", "undici", True) + create_user("sensei", "dodici", True) + create_user("gattopardo", "tredici", True) + create_user("dima", "quattordici", True) + create_user("spaggia", "quindici", True) + create_user("igor", "sedici", True) + create_user("nemesis", "diciassette", True) + create_user("comiso", "diciotto", True) + create_user("fulz", "diciannove", True) + create_user("dailir", "venti", True) + create_user("fedececco", "ventuno", True) + create_user("albertwerk", "ventidue", True) + create_user("voltaggio", "ventitre", True) + create_user("doc", "ventiquattro", True) \ No newline at end of file