Update bot.py

This commit is contained in:
Gefaehrbert
2024-05-25 18:38:25 +02:00
parent 743f917bc3
commit a85a6bc71c

49
bot.py
View File

@@ -2,51 +2,34 @@ import os
import discord import discord
from discord.ext import commands from discord.ext import commands
# Konfiguriere die Bot-Instanz
intents = discord.Intents.default() intents = discord.Intents.default()
intents.guilds = True intents.guilds = True
intents.voice_states = True intents.voice_states = True
bot = commands.Bot(command_prefix='!', intents=intents) bot = commands.Bot(command_prefix='!', intents=intents)
# Bot-Token und Kategorie-IDs aus Umgebungsvariablen token = os.getenv('BOT_TOKEN')
TOKEN = os.getenv('BOT_TOKEN') join_to_create_channel_id = int(os.getenv('JOINTOCREATE_CHANNEL_ID'))
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 = []
@bot.event @bot.event
async def on_ready(): async def on_ready():
print('Bot is ready!') print('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}')
@bot.event @bot.event
async def on_voice_state_update(member, before, after): async def on_voice_state_update(member, before, after):
global created_channels if after.channel and after.channel.id == join_to_create_channel_id:
new_channel = await after.channel.guild.create_voice_channel(
# Prüfen, ob der Benutzer in einen JoinToCreate-Kanal wechselt name=f"🔊 {member.display_name}'s Channel",
if after.channel and after.channel.id in join_to_create_channels: category=after.channel.category,
# Erstelle einen neuen Sprachkanal reason='Created by JoinToCreate bot'
category = after.channel.category )
new_channel = await category.create_voice_channel(name=f'🔊 {member.display_name}\'s Channel')
await member.move_to(new_channel) 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 if before.channel and len(before.channel.members) == 0 and before.channel.id != join_to_create_channel_id:
created_channels.append(new_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 bot.run(token)
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)