index — Deutsche-Haus-Bot @ 56cf04d640af6732092845022d2f693e6911f594

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

added logging and removed old debug prints
crispy-caesus 114518720+crispy-caesus@users.noreply.github.com
Wed, 23 Oct 2024 13:07:09 +0200
commit

56cf04d640af6732092845022d2f693e6911f594

parent

938b6f9946a582a8af2e0e595a07a0790ebba6c3

2 files changed, 26 insertions(+), 18 deletions(-)

jump to
M bot.pybot.py

@@ -18,9 +18,14 @@ member_converter = discord.ext.commands.MemberConverter()

# ======================= INITIALIZATION ========================== # +@bot.event +async def on_ready(): + print(f'LOG: bot has logged in as {bot.user}') + @bot.listen() async def on_guild_join(guild): await logic.on_guild_join(guild.id) + print(f"LOG: guild {guild} joined") # ======================= SETUP ========================= #

@@ -38,6 +43,7 @@ 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]))) + print(f"LOG: {ctx.author} added {booster_rolle} as booster role") @bot.slash_command(description="Setzt den Verteiler Channel intern im Bot") async def setze_verteiler_channel(ctx, verteiler_channel_id):

@@ -45,6 +51,7 @@ 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))) + print(f"LOG: {ctx.author} added {verteiler_channel_id} as distributor channel id") @bot.slash_command(description="Setzt die Kategorie, in der die Clubs erstellt werden sollen") async def setze_club_kategorie(ctx, kategorie_id):

@@ -52,19 +59,16 @@ 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))) + print(f"LOG: {ctx.author} added {kategorie_id} as booster role") """ -@bot.slash_command(description="Füge neue Rollen zu der Datenbank hinzu") -@discord.ext.commands.has_role() -async def -""" - @bot.slash_command() async def get_existing_roles(ctx): results = ctx.guild.roles print(results) +""" @bot.slash_command()

@@ -89,6 +93,9 @@ return

else: await ctx.guild.create_role(name = response[0], color = response[1], mentionable = False) + print(f"LOG: club {kanalname} created by {ctx.author}") + + role = await role_converter.convert(ctx, rollenname) await ctx.author.add_roles(role)

@@ -97,7 +104,8 @@ await ctx.respond(await logic.add_club(ctx.guild.id, kanalname, emoji, rollenname, role.id, ctx.author.id, 3))

db = database.Database(f"{ctx.guild.id}.db") await db.add_member(ctx.author.id, ctx.author.id) - + + print(f"LOG: role {role} added to {ctx.author}") # ==================== EDIT CLUBS ====================== #

@@ -121,12 +129,12 @@ response = await db.select_role_id_by_owner(ctx.author.id)

if response == None: await ctx.respond("Du hast keinen Club") return - print(f"{type(response)}: {response}") role = discord.utils.get(ctx.guild.roles, id=response) await member.add_roles(role) await ctx.respond(":white_check_mark:") + print(f"LOG: added {member} to {role}") @bot.command(description="Sends the bot's latency.") # this decorator makes a slash command

@@ -147,8 +155,8 @@

if after.channel.id == await db.get_discord_id("distributor_channel_id"): - print(f"{user.name} Joined The {after.channel.name} VC") - print(after.channel.members) + print(f"LOG: {user.name} joined {after.channel.name}") + #print(f"LOG: channel members: {after.channel.members}")

@@ -158,8 +166,6 @@ message = "Welchen Club-Kanal willst du öffnen?"

for i in range(len(db_response)): message += f"\n**{i+1}.** {db_response[i][0]}" - print(message) - await after.channel.send(message) def check(m):

@@ -184,11 +190,11 @@ channel_name = db_response[int(response.content)-1][0]

category = discord.utils.get(after.channel.guild.categories, id=await db.get_discord_id("new_channel_category_id")) if category is None: - print("Kategorie nicht gefunden") + print("ERROR: Can't find set new_channel_category on server") role = discord.utils.get(after.channel.guild.roles, id=db_response[int(response.content)-1][1]) if role is None: - print("Rolle nicht gefunden") + print("ERROR: Can't find club role") bot_member = after.channel.guild.me club_owner = discord.utils.get(after.channel.guild.members, id=await db.get_owner_by_club_id(db_response[int(response.content)-1][2]))

@@ -227,19 +233,23 @@ voice_channel = await category.create_voice_channel(

name = channel_name, overwrites=overwrites ) + print(f"LOG: new channel {voice_channel} created") distributor_vcs.append(voice_channel.id) + print(f"LOG: The List of existing club channels is now: {distributor_vcs}") done = True else: await after.channel.send(":x: Nicht zulässige Zahl") if user.voice: await user.move_to(voice_channel) + print(f"LOG: moved {user} into {voice_channel}") if before.channel and before.channel.id in distributor_vcs: if len(before.channel.members) == 0: await before.channel.delete(reason="Niemand ist mehr verbunden") distributor_vcs.remove(before.channel.id) + print(f"LOG: deleted {before.channel} because it was empty") bot.run(bot_token.token)
M db.pydb.py

@@ -53,7 +53,7 @@ VALUES(?,?);""", (id_type, discord_id))

await db.commit() except Error as e: print(e) - return("Error") + return(":x: Error!") return(None) # =========================== CREATE CLUB =========================== #

@@ -87,7 +87,7 @@ return row

async def create_club(self, channel_name: str, owner: int, role_id: int, role_name: str): - print(f"DB: received:\n channel_name: {channel_name}\n owner: {owner}\n role_id: {role_id}") + print(f"DB: create_club received:\n channel_name: {channel_name}\n owner: {owner}\n role_id: {role_id}") args = (channel_name, owner, role_id, role_name) sql = """INSERT INTO clubs (channel_name,owner_id,role_id,role_name) VALUES(?,?,?,?);"""

@@ -116,7 +116,7 @@

# ============================ add member ================================== # async def add_member(self, member: int, owner: int): - print(f"DB: received:\n member: {member}\n owner: {owner}") + print(f"DB: add_member received:\n member: {member}\n owner: {owner}") sql = """INSERT INTO members (user_id, club_id) VALUES(?,?);""" try:

@@ -177,11 +177,9 @@ clubs = []

async with aiosqlite.connect(self.db_name) as db: async with db.execute("SELECT club_id FROM members WHERE user_id = ?;", (user_id,)) as cursor: async for club_id in cursor: - print(f"{type(club_id[0])}: {club_id[0]}") async with db.execute("SELECT channel_name, role_id, id FROM clubs WHERE id = ?;", (club_id[0], )) as cursor2: async for club in cursor2: clubs.append(club) - print(clubs) return clubs