From a85a6bc71c1019787ef46e22ae6985ee5e95c3ae Mon Sep 17 00:00:00 2001 From: Gefaehrbert <138185406+degnedict@users.noreply.github.com> Date: Sat, 25 May 2024 18:38:25 +0200 Subject: [PATCH] Update bot.py --- bot.py | 49 ++++++++++++++++--------------------------------- 1 file changed, 16 insertions(+), 33 deletions(-) diff --git a/bot.py b/bot.py index cdf9927..36f44a5 100644 --- a/bot.py +++ b/bot.py @@ -2,51 +2,34 @@ import os import discord from discord.ext import commands -# Konfiguriere die Bot-Instanz intents = discord.Intents.default() intents.guilds = True intents.voice_states = True bot = commands.Bot(command_prefix='!', intents=intents) -# Bot-Token und Kategorie-IDs aus Umgebungsvariablen -TOKEN = os.getenv('BOT_TOKEN') -CATEGORY_IDS = os.getenv('CATEGORY_IDS').split(',') -JOINTOCREATE_CHANNEL_NAME = os.getenv('JOINTOCREATE_CHANNEL_NAME', 'Join to Create') - -# Liste zur Speicherung der erstellten Kanäle -created_channels = [] -join_to_create_channels = [] +token = os.getenv('BOT_TOKEN') +join_to_create_channel_id = int(os.getenv('JOINTOCREATE_CHANNEL_ID')) @bot.event async def on_ready(): - print('Bot is ready!') - - # Erstellen der JoinToCreate-Kanäle in den angegebenen Kategorien - for category_id in CATEGORY_IDS: - category = bot.get_channel(int(category_id)) - join_to_create_channel = await category.create_voice_channel(name=JOINTOCREATE_CHANNEL_NAME) - join_to_create_channels.append(join_to_create_channel.id) - print(f'JoinToCreate Channel created in category {category_id} with ID: {join_to_create_channel.id}') + print('Ready!') @bot.event async def on_voice_state_update(member, before, after): - global created_channels - - # Prüfen, ob der Benutzer in einen JoinToCreate-Kanal wechselt - if after.channel and after.channel.id in join_to_create_channels: - # Erstelle einen neuen Sprachkanal - category = after.channel.category - new_channel = await category.create_voice_channel(name=f'🔊 {member.display_name}\'s Channel') + if after.channel and after.channel.id == join_to_create_channel_id: + new_channel = await after.channel.guild.create_voice_channel( + name=f"🔊 {member.display_name}'s Channel", + category=after.channel.category, + reason='Created by JoinToCreate bot' + ) await member.move_to(new_channel) + # Mark the channel as created by the bot by adding a custom attribute + new_channel.created_by_bot = True - # Markiere den Kanal als vom Bot erstellt - created_channels.append(new_channel.id) + if before.channel and len(before.channel.members) == 0 and before.channel.id != join_to_create_channel_id: + # Check if the channel was created by the bot before deleting it + if hasattr(before.channel, 'created_by_bot') and before.channel.created_by_bot: + await before.channel.delete(reason='No members left in the bot-created channel') - # Prüfen, ob ein Kanal leer geworden ist und ob er vom Bot erstellt wurde - if before.channel and before.channel.id in created_channels and len(before.channel.members) == 0: - await before.channel.delete() - created_channels.remove(before.channel.id) - -# Starte den Bot -bot.run(TOKEN) +bot.run(token)