1
Fork 0
mirror of https://github.com/RYGhub/nameless.git synced 2024-11-22 05:34:19 +00:00
nameless/database.py

68 lines
1.9 KiB
Python
Raw Normal View History

2017-07-28 10:39:50 +00:00
from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
db = create_engine("sqlite:///database.sqlite")
Base = declarative_base(bind=db)
Session = sessionmaker(bind=db)
# Using sqlite, only a single write session is available
session = Session()
class User(Base):
"""The basic data of a Telegram user"""
__tablename__ = "user"
id = Column(Integer, primary_key=True)
username = Column(String)
firstname = Column(String)
lastname = Column(String)
language = Column(String)
chapter = Column(Integer)
async def message(self, bot, text):
await bot.sendMessage(self.id, text)
def __init__(self, tid: int, firstname: str=None, lastname: str=None, username: str=None, language: str=None):
self.id = tid
self.username = username
self.firstname = firstname
self.lastname = lastname
self.language = language
self.chapter = 0
def __repr__(self):
return f"<User {self.id}>"
def __str__(self):
# Display the username
if self.username is not None:
return f"@{self.username}"
# Display first name and last name
if self.lastname is not None:
return f"{self.firstname} {self.lastname}"
# Fallback if no lastname
return f"{self.firstname}"
class FirstChapter(Base):
"""The save data of the first chapter"""
__tablename__ = "firstchapter"
user_id = Column(Integer, ForeignKey("user.id"), primary_key=True)
user = relationship("User")
current_question = Column(Integer)
game_topic = Column(String)
game_release = Column(String)
def __init__(self, user):
self.user_id = user.id
self.current_question = 0
# If the script is run as standalone, generate the database
if __name__ == "__main__":
Base.metadata.create_all()