From 743f917bc3889ecb8714b1f4a9c5a523f485a373 Mon Sep 17 00:00:00 2001 From: Gefaehrbert <138185406+degnedict@users.noreply.github.com> Date: Sat, 18 May 2024 04:13:47 +0200 Subject: [PATCH 1/4] initial --- Dockerfile | 23 +++++++++----------- bot.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 9 -------- index.js | 35 ------------------------------- package.json | 13 ------------ requirements.txt | 1 + 6 files changed, 63 insertions(+), 70 deletions(-) create mode 100644 bot.py delete mode 100644 docker-compose.yml delete mode 100644 index.js delete mode 100644 package.json create mode 100644 requirements.txt diff --git a/Dockerfile b/Dockerfile index 19ddb21..0fefda2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,14 @@ -# Verwende die neueste LTS-Version von Node.js -FROM node:18-alpine +FROM python:3.9-slim -# Erstelle und setze das Arbeitsverzeichnis -WORKDIR /usr/src/app +# Set the working directory +WORKDIR /app -# Kopiere package.json und package-lock.json (falls vorhanden) -COPY package*.json ./ +# Copy the requirements.txt and the script to the container +COPY requirements.txt . +COPY bot.py . -# Installiere die Abhängigkeiten -RUN npm install +# Install dependencies +RUN pip install --no-cache-dir -r requirements.txt -# Kopiere den Rest des Anwendungsverzeichnisses -COPY . . - -# Definiere die Startkommandozeile für den Container -CMD [ "npm", "start" ] +# Run the bot +CMD ["python", "bot.py"] diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..cdf9927 --- /dev/null +++ b/bot.py @@ -0,0 +1,52 @@ +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 = [] + +@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}') + +@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') + await member.move_to(new_channel) + + # Markiere den Kanal als vom Bot erstellt + created_channels.append(new_channel.id) + + # 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) diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index 2768604..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,9 +0,0 @@ -version: '3.8' - -services: - join2create: - image: git.degnedict.de/bene/join2create:latest - environment: - - BOT_TOKEN=YourBotToken - - JOINTOCREATE_CHANNEL_ID=Your-Channel-ID - restart: unless-stopped diff --git a/index.js b/index.js deleted file mode 100644 index ce1ebea..0000000 --- a/index.js +++ /dev/null @@ -1,35 +0,0 @@ -const { Client, GatewayIntentBits, ChannelType } = require('discord.js'); -const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates] }); - -const token = process.env.BOT_TOKEN; -const joinToCreateChannelId = process.env.JOINTOCREATE_CHANNEL_ID; - -// Set to true if a channel was created by the bot -const CREATED_BY_BOT = 'created_by_bot'; - -client.once('ready', () => { - console.log('Ready!'); -}); - -client.on('voiceStateUpdate', (oldState, newState) => { - if (newState.channelId === joinToCreateChannelId) { - newState.guild.channels.create({ - name: `🔊 ${newState.member.displayName}'s Channel`, - type: ChannelType.GuildVoice, - parent: newState.channel.parentId, - reason: 'Created by JoinToCreate bot', // Add a reason when creating the channel - }).then(channel => { - newState.member.voice.setChannel(channel); - channel[CREATED_BY_BOT] = true; // Mark the channel as created by the bot - }).catch(console.error); - } - - if (oldState.channel && oldState.channel.members.size === 0 && oldState.channel.id !== joinToCreateChannelId) { - // Check if the channel was created by the bot before deleting it - if (oldState.channel[CREATED_BY_BOT]) { - oldState.channel.delete().catch(console.error); - } - } -}); - -client.login(token); diff --git a/package.json b/package.json deleted file mode 100644 index 4820c26..0000000 --- a/package.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "join2create", - "version": "1.0.0", - "description": "Simple Discord bot with join to create feature", - "main": "index.js", - "scripts": { - "start": "node index.js" - }, - "dependencies": { - "discord.js": "^14.0.0" - } - } - \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..503dba9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +discord.py \ No newline at end of file -- 2.49.1 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 2/4] 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) -- 2.49.1 From f44c8a778f7e49cf1e6c996ae6cae837618a95d5 Mon Sep 17 00:00:00 2001 From: Gefaehrbert <138185406+degnedict@users.noreply.github.com> Date: Sat, 25 May 2024 18:54:50 +0200 Subject: [PATCH 3/4] Update bot.py --- bot.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/bot.py b/bot.py index 36f44a5..f57a4c3 100644 --- a/bot.py +++ b/bot.py @@ -24,12 +24,10 @@ async def on_voice_state_update(member, before, after): 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 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: + # Check if the channel was created by the bot by checking its name + if before.channel.name.startswith('🔊'): await before.channel.delete(reason='No members left in the bot-created channel') bot.run(token) -- 2.49.1 From 573783f843594c407b02e81f15d8882440b15b20 Mon Sep 17 00:00:00 2001 From: Bene Date: Sat, 25 May 2024 19:03:11 +0200 Subject: [PATCH 4/4] Switched to Python --- .dockerignore | 1 + bot.py | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..4f509e5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +*.env \ No newline at end of file diff --git a/bot.py b/bot.py index f57a4c3..1c146a3 100644 --- a/bot.py +++ b/bot.py @@ -26,7 +26,6 @@ async def on_voice_state_update(member, before, after): await member.move_to(new_channel) 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 by checking its name if before.channel.name.startswith('🔊'): await before.channel.delete(reason='No members left in the bot-created channel') -- 2.49.1