Switch to Python #2
23
Dockerfile
23
Dockerfile
@@ -1,17 +1,14 @@
|
|||||||
# Verwende die neueste LTS-Version von Node.js
|
FROM python:3.9-slim
|
||||||
FROM node:18-alpine
|
|
||||||
|
|
||||||
# Erstelle und setze das Arbeitsverzeichnis
|
# Set the working directory
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /app
|
||||||
|
|
||||||
# Kopiere package.json und package-lock.json (falls vorhanden)
|
# Copy the requirements.txt and the script to the container
|
||||||
COPY package*.json ./
|
COPY requirements.txt .
|
||||||
|
COPY bot.py .
|
||||||
|
|
||||||
# Installiere die Abhängigkeiten
|
# Install dependencies
|
||||||
RUN npm install
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
# Kopiere den Rest des Anwendungsverzeichnisses
|
# Run the bot
|
||||||
COPY . .
|
CMD ["python", "bot.py"]
|
||||||
|
|
||||||
# Definiere die Startkommandozeile für den Container
|
|
||||||
CMD [ "npm", "start" ]
|
|
||||||
|
|||||||
52
bot.py
Normal file
52
bot.py
Normal file
@@ -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)
|
||||||
@@ -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
|
|
||||||
35
index.js
35
index.js
@@ -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);
|
|
||||||
13
package.json
13
package.json
@@ -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"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
discord.py
|
||||||
Reference in New Issue
Block a user