index — Deutsche-Haus-Bot @ fcff38b74f08eb6f8ed52ec4c7416fd786a581e1

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

rewrite: setup done
crispy-caesus 114518720+crispy-caesus@users.noreply.github
Sun, 11 Aug 2024 17:15:36 +0200
commit

fcff38b74f08eb6f8ed52ec4c7416fd786a581e1

parent

b031f196c5d6b9732fa47db063fcdd7638485f01

4 files changed, 226 insertions(+), 44 deletions(-)

jump to
M bot.pybot.py

@@ -2,15 +2,71 @@ import discord

import discord.ext.commands import bot_token import db as database -import aiosqlite +import logic bot = discord.Bot() +role_converter = discord.ext.commands.RoleConverter() +member_converter = discord.ext.commands.MemberConverter() +# ======================= INITIALIZATION ========================== # + +@bot.listen() +async def on_guild_join(guild): + await logic.on_guild_join(guild.id) + + +# ======================= SETUP ========================= # + +@bot.slash_command(description="Gibt alle Commands zurück, die für die Initialisation des Bots nötig sind") +async def setup(ctx): + if not ctx.author.guild_permissions.administrator: + ctx.respond("Du musst Administrator sein, um diesen Command auszuführen") + return + await ctx.respond(await logic.setup(ctx.guild.id)) + +@bot.slash_command(description="Setzt die Booster Rolle intern im Bot") +async def setze_booster_rolle(ctx, booster_rolle): + if not ctx.author.guild_permissions.administrator: + ctx.respond("Du musst Administrator sein, um diesen Command auszuführen") + return + await ctx.respond(await logic.add_booster_role(ctx.guild.id, int(booster_rolle[3:-1]))) + +@bot.slash_command(description="Setzt den Verteiler Channel intern im Bot") +async def setze_verteiler_channel(ctx, verteiler_channel_id): + if not ctx.author.guild_permissions.administrator: + ctx.respond("Du musst Administrator sein, um diesen Command auszuführen") + return + await ctx.respond(await logic.add_distributor_channel(ctx.guild.id, int(verteiler_channel_id))) + +@bot.slash_command(description="Setzt die Kategorie, in der die Clubs erstellt werden sollen") +async def setze_club_kategorie(ctx, kategorie_id): + if not ctx.author.guild_permissions.administrator: + ctx.respond("Du musst Administrator sein, um diesen Command auszuführen") + return + await ctx.respond(await logic.add_club_category(ctx.guild.id, int(kategorie_id))) + + + +""" +@bot.slash_command(description="Füge neue Rollen zu der Datenbank hinzu") +@discord.ext.commands.has_role() +async def +""" + +async def get_existing_roles(ctx): + results = ctx.guild.roles + return results + + +# ======================== add club ================================ # @bot.slash_command(description="Erstellt einen Booster Club") @discord.ext.commands.has_role(1170646611332956208) -async def club_hinzuftügen(ctx, kanalname, emoji, rollenname): +async def club_hinzuftügen(ctx, kanalname, emoji, rollenname, rollenfarbe): user = ctx.author.id + + await logic.add_club(kanalname, emoji, rollenname, rollenfarbe, user) + db = database.Database(f"{ctx.guild.id}.db") await db.create_tables() check = await db.check_if_club_creatable(kanalname, user)

@@ -20,10 +76,9 @@ return

await ctx.guild.create_role(name=rollenname, color=0x5460D9, mentionable=False) - role_converter = discord.ext.commands.RoleConverter() - rolle = await role_converter.convert(ctx, rollenname) + role = await role_converter.convert(ctx, rollenname) channel_name = f"[{emoji}]{kanalname}" - response = await db.create_club(channel_name, user, rolle.id) + response = await db.create_club(channel_name, user, role.id) if response == None: await ctx.respond(f"{channel_name} created!") else:

@@ -37,27 +92,36 @@ if response != None:

await ctx.respond(response) return - member_converter = discord.ext.commands.MemberConverter() member = await member_converter.convert(ctx, member) response = await db.select_club_by_owner(ctx.author.id) if response == None: await ctx.respond("Du hast keinen Club") return - role_converter = discord.ext.commands.RoleConverter() role = await role_converter.convert(ctx, str(response)) await member.add_roles(role) - await ctx.respond(":white_check_mark:") -@bot.user_command(name="Say Hello") -async def hi(ctx, user): - await ctx.respond(f"{ctx.author.mention} says hello to {user.name}!") @bot.command(description="Sends the bot's latency.") # this decorator makes a slash command async def ping(ctx): # a slash command will be created with the name "ping" await ctx.respond(f"Pong! Latency is {bot.latency}") + +# ==================== distributor vc ======================= # + +@bot.slash_command(description="Setzt join to create VC") +@discord.ext.commands.has_role(1170646611332956208) +async def verteiler_setzen(kanal_id): + pass + +#@bot.listen() +async def on_voice_state_update(user, before, after): + if after.channel.id == 1: + print(f"{user.name} Joined The {after.channel.name} VC") + print(after.channel.members) + + bot.run(bot_token.token)
M db.pydb.py

@@ -1,18 +1,14 @@

import aiosqlite from aiosqlite import Error -import asyncio class Database(): def __init__(self, db_name: str): self.db_name = db_name - async def list_tables(self): - async with aiosqlite.connect(self.db_name) as db: - async with db.execute("SELECT name FROM sqlite_master WHERE type = 'table';") as cursor: - async for row in cursor: - print(row) - + +# ====================== INITIALIZATION ======================== # +# could be done more flexibly somehow probably, receiving the schema from the logic.py, but ehhhhhhhhhhh (also not databse independent then) async def create_tables(self): async with aiosqlite.connect(self.db_name) as db: await db.execute("""CREATE TABLE IF NOT EXISTS clubs (

@@ -31,6 +27,49 @@ REFERENCES clubs (id)

ON UPDATE CASCADE ON DELETE CASCADE);""") await db.commit() + await db.execute("""CREATE TABLE IF NOT EXISTS existing_roles ( + id INTEGER PRIMARY KEY, + roles TEXT NOT NULL UNIQUE);""") + await db.commit() + await db.execute("""CREATE TABLE IF NOT EXISTS ids ( + id INTEGER PRIMARY KEY, + id_type INTEGER UNIQUE NOT NULL, + discord_id INTEGER NOT NULL);""") + await db.commit() + +# ========================= SETUP ============================ # + + async def get_discord_id(self, id_type: str): + async with aiosqlite.connect(self.db_name) as db: + async with db.execute("SELECT discord_id FROM ids WHERE id_type = ?;", (id_type,)) as cursor: + async for row in cursor: + return(row[0]) + + async def add_id(self, id_type: str, discord_id: int): + async with aiosqlite.connect(self.db_name) as db: + try: + await db.execute("""INSERT INTO ids (id_type, discord_id) + VALUES(?,?);""", (id_type, discord_id)) + await db.commit() + except Error as e: + print(e) + return("Error") + return(None) + +# =========================== create club =========================== # + + async def check_if_club_creatable(self, channel_name, owner): + async with aiosqlite.connect(self.db_name) as db: + async with db.execute("SELECT owner FROM clubs WHERE owner = ?;", (owner,)) as cursor: + async for row in cursor: + if row != None: + return("Du hast bereits einen Club erstellt") + async with db.execute("SELECT channel_name FROM clubs WHERE channel_name = ?;", (channel_name,)) as cursor: + async for row in cursor: + if row != None: + return("Es existiert bereits ein Channel mit diesem Namen") + return None + async def create_club(self, channel_name: str, owner: int, role_id: int): print(f"DB: received:\n channel_name: {channel_name}\n owner: {owner}\n role_id: {role_id}")

@@ -44,6 +83,8 @@ await db.commit()

except Error as e: print(e) return("Error") + +# ============================ add member ================================== # async def add_member(self, member: int, owner: int): print(f"DB: received:\n member: {member}\n owner: {owner}")

@@ -59,19 +100,14 @@ await db.commit()

except Error as e: print(e) return("Error") - - - - - async def select_clubs(self)-> list: - clubs = [] + async def select_club_by_owner(self, member: int): async with aiosqlite.connect(self.db_name) as db: - async with db.execute("SELECT * FROM clubs;") as cursor: + async with db.execute("SELECT role_id FROM clubs WHERE owner = ?;", (member,)) as cursor: async for row in cursor: - clubs.append(row) - return clubs + return(row[0]) +# ============================= list member clubs ========================== # async def select_clubs_of_member(self, member: int)-> list: clubs = []

@@ -86,21 +122,21 @@ except Error as e:

print(e) return clubs - async def select_club_by_owner(self, member: int): +# ============================= list random stuff ================================ # + + async def list_tables(self): async with aiosqlite.connect(self.db_name) as db: - async with db.execute("SELECT role_id FROM clubs WHERE owner = ?;", (member,)) as cursor: + async with db.execute("SELECT name FROM sqlite_master WHERE type = 'table';") as cursor: async for row in cursor: - return(row[0]) + print(row) - async def check_if_club_creatable(self, channel_name, owner): + + async def select_clubs(self)-> list: + clubs = [] async with aiosqlite.connect(self.db_name) as db: - async with db.execute("SELECT owner FROM clubs WHERE owner = ?;", (owner,)) as cursor: + async with db.execute("SELECT * FROM clubs;") as cursor: async for row in cursor: - if row != None: - return("Du hast bereits einen Club erstellt") - async with db.execute("SELECT channel_name FROM clubs WHERE channel_name = ?;", (channel_name,)) as cursor: - async for row in cursor: - if row != None: - return("Es existiert bereits ein Channel mit diesem Namen") - return None + clubs.append(row) + return clubs +
A logic.py

@@ -0,0 +1,85 @@

+import db as database + + + + +# ========================= INITIALIZATION ========================= # + +async def on_guild_join(guild_id): + db = database.Database(f"{guild_id}.db") + await db.create_tables() + + +# ========================= SETUP ========================== # + +async def setup(guild_id): + db = database.Database(f"{guild_id}.db") + booster_role_id = await db.get_discord_id("booster_role_id") + if booster_role_id == None: + booster_check = "❌" + else: + booster_check = "✅" + distributor_channel_id = await db.get_discord_id("distributor_channel_id") + if distributor_channel_id == None: + distributor_check = "❌" + else: + distributor_check = "✅" + new_channel_category_id = await db.get_discord_id("new_channel_category_id") + if new_channel_category_id == None: + category_check = "❌" + else: + category_check = "✅" + + return( \ + f"{booster_check} Setze die Booster Rolle mit `/setze_booster_rolle`\n" \ + f"{distributor_check} Setze den Verteiler Channel mit `/setze_verteiler_channel`\n" \ + f"{category_check} Setze die Kategorie, in der, die Clubs erstellt werden sollen mit `/setze_club_kategorie`" \ + ) + +async def add_booster_role(guild_id: int, booster_role_id: int)-> str: + db = database.Database(f"{guild_id}.db") + response = await db.add_id("booster_role_id", booster_role_id) + if response == "Error": + return(response) + else: + return("✅ Booster Rolle registriert") + +async def add_distributor_channel(guild_id: int, distributor_channel_id: int)-> str: + db = database.Database(f"{guild_id}.db") + response = await db.add_id("distributor_channel_id", distributor_channel_id) + if response == "Error": + return(response) + else: + return("✅ Verteiler Channel registriert") + +async def add_club_category(guild_id: int, category_id: int)-> str: + db = database.Database(f"{guild_id}.db") + response = await db.add_id("new_channel_category_id", category_id) + if response == "Error": + return(response) + else: + return("✅ Club Kategorie registriert") + + +# ========================== ADD CLUB ======================== # + +async def add_club(channel_name_without_emoji, emoji, role_name, color, owner_id): + # ------------------------- check if club creatable ------------------------ # + existing_club_of_owner = db.get_club_by_owner(owner_id) + if existing_club_of_owner == None: + return("Du hast bereits einen Club") + + combined_channel_name = f"「{emoji}」{channel_name_without_emoji}" + existing_club = db.check_if_channel_exists(combined_channel_name) + if existing_club != None: + return("Es gibt bereits einen Club mit diesem Kanalnamen") + + existing_club_role = bot.check_if_role_exists(role_name) + if existing_club_role != None: + return("Es existiert bereits ein Club mit diesem Rollennamen") + + existing_role + + # role id needed + +
M test.pytest.py

@@ -1,11 +1,8 @@

import db +import bot import asyncio async def main(): - my_db = db.Database("1170646611236487208.db") - await my_db.create_tables() - await my_db.list_tables() - print(await my_db.select_clubs()) - await my_db.check_if_club_creatable("asdsad", 621759962280099840) + print(await bot.get_existing_roles()) asyncio.run(main())