index — Deutsche-Haus-Bot @ 151426d13068bc4bb19dabeedf01c82c41e46f96

Discord bot to dynamically create voice chats for clubs that boosters can create

db finished (probably?)
crispy-caesus 114518720+crispy-caesus@users.noreply.github.com
Fri, 09 Aug 2024 09:39:09 +0200
commit

151426d13068bc4bb19dabeedf01c82c41e46f96

parent

2813526592488834341398c22710c73a9e608a0e

2 files changed, 54 insertions(+), 17 deletions(-)

jump to
M db.pydb.py

@@ -13,13 +13,23 @@ async with db.execute("SELECT name FROM sqlite_master WHERE type = 'table';") as cursor:

async for row in cursor: print(row) - async def create_table(self): + async def create_tables(self): async with aiosqlite.connect(self.db_name) as db: await db.execute("""CREATE TABLE IF NOT EXISTS clubs ( id INTEGER PRIMARY KEY, channel_name TEXT NOT NULL UNIQUE, owner INTEGER NOT NULL UNIQUE, role_id INTEGER NOT NULL UNIQUE);""") + await db.commit() + await db.execute("""CREATE TABLE IF NOT EXISTS members ( + id INTEGER PRIMARY KEY, + user_id INTEGER NOT NULL, + club_id INTEGER NOT NULL, + UNIQUE(user_id, club_id) ON CONFLICT REPLACE, + FOREIGN KEY (club_id) + REFERENCES clubs (id) + ON UPDATE CASCADE + ON DELETE CASCADE);""") await db.commit() async def create_club(self, channel_name: str, owner: int, role_id: int):

@@ -33,31 +43,45 @@ await db.commit()

except Error as e: print(e) return("Club existiert schon") + + async def add_member(self, member: int, club: int): + sql = """INSERT INTO members (user_id, club_id) + VALUES(?,?);""" + try: + async with aiosqlite.connect(self.db_name) as db: + async with db.execute("SELECT id FROM clubs WHERE role_id = ?;", (club, )) as cursor: + async for row in cursor: + args = (member, row[0]) + await db.execute(sql, args) + await db.commit() + except Error as e: + print(e) + return("Member existiert schon") + + - async def select_clubs(self): + async def select_clubs(self)-> list: clubs = [] async with aiosqlite.connect(self.db_name) as db: async with db.execute("SELECT * FROM clubs;") as cursor: async for row in cursor: clubs.append(row) + return clubs -""" - async def select_clubs_of_member(self, member: int): + + async def select_clubs_of_member(self, member: int)-> list: clubs = [] - async with aiosqlite.connect(self.db_name) as db: - async with db.execute("SELECT * FROM clubs WHERE ;") as cursor: - async for row in cursor: - clubs.append(row) -""" -# seperate users table with user and foreign key club id -# select * where user=... and translate to club - + try: + async with aiosqlite.connect(self.db_name) as db: + async with db.execute("SELECT club_id FROM members WHERE user_id = ?;", (member, )) as cursor: + async for row in cursor: + async with db.execute("SELECT channel_name, role_id FROM clubs WHERE id = ?;", (row[0], )) as cursor2: + async for row2 in cursor2: + clubs.append(row2) + except Error as e: + print(e) + return clubs -my_db = Database("test.db") -asyncio.run(my_db.create_table()) -asyncio.run(my_db.list_tables()) -print(asyncio.run(my_db.create_club("test channel", 123123, 12312))) -print(asyncio.run(my_db.select_clubs()))
A test.py

@@ -0,0 +1,13 @@

+import db +import asyncio + +async def main(): + my_db = db.Database("test.db") + await my_db.create_tables() + await my_db.list_tables() + print(await my_db.create_club("test channel2", 123, 23)) + print(await my_db.select_clubs()) + await my_db.add_member(3, 23) + print(await my_db.select_clubs_of_member(3)) + +asyncio.run(main())