Switch to Python #2

Merged
bene merged 4 commits from python into main 2024-05-25 17:04:36 +00:00
7 changed files with 44 additions and 70 deletions

1
.dockerignore Normal file
View File

@@ -0,0 +1 @@
*.env

View File

@@ -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" ]

32
bot.py Normal file
View File

@@ -0,0 +1,32 @@
import os
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.guilds = True
intents.voice_states = True
bot = commands.Bot(command_prefix='!', intents=intents)
token = os.getenv('BOT_TOKEN')
join_to_create_channel_id = int(os.getenv('JOINTOCREATE_CHANNEL_ID'))
@bot.event
async def on_ready():
print('Ready!')
@bot.event
async def on_voice_state_update(member, before, after):
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)
if before.channel and len(before.channel.members) == 0 and before.channel.id != join_to_create_channel_id:
if before.channel.name.startswith('🔊'):
await before.channel.delete(reason='No members left in the bot-created channel')
bot.run(token)

View File

@@ -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

View File

@@ -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);

View File

@@ -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
View File

@@ -0,0 +1 @@
discord.py