Can't deploy working discord bot

Question:
I have a working discord bot in workspace but it allways run into error if i try to deploy it with autoscale option
Repl link:
https://efe-discordbot.replit.app

import discord
import requests
import mysql.connector
import os

intents = discord.Intents.default()
intents.guilds = True
intents.members = True
intents.reactions = True  # Erlaubt dem Bot, auf Reaktionen zu reagieren
client = discord.Client(intents=intents)

# MySQL-Datenbankverbindung herstellen
mydb = mysql.connector.connect(
    host=os.environ['HOST'],
    user=os.environ['USERNAME'],
    password=os.environ['PASSWORD'],
    database=os.environ['DATABASE']
)
mycursor = mydb.cursor()

# Discord Bot-Token (ersetze 'YOUR_DISCORD_BOT_TOKEN' durch deinen Bot-Token)
TOKEN = os.environ['TOKEN']

# Mojang API-URL
MOJANG_API_URL = 'https://api.mojang.com/users/profiles/minecraft/'

# ID des Channels, in dem der Registrierungs-Button erstellt werden soll
REGISTRATION_CHANNEL_ID = 1227533989389144146  # Hier die richtige Channel-ID einfügen

# Funktion zum Erstellen des Registrierungs-Buttons
async def create_registration_button():
    channel = client.get_channel(REGISTRATION_CHANNEL_ID)
    message = await channel.send("Klicke auf den Button, um dich zu registrieren:")
    await message.add_reaction("🔵")  # Blauer Kreis als Button

# Discord Bot bereit
@client.event
async def on_ready():
    print(f'{client.user} ist bereit!')
    await create_registration_button()

# Event-Handler für Reaktionen auf Nachrichten
@client.event
async def on_reaction_add(reaction, user):
    if user == client.user:
        return

    if reaction.message.channel.id == REGISTRATION_CHANNEL_ID and reaction.emoji == "🔵":
        await handle_registration(user)

# Funktion zum Verarbeiten der Registrierung
async def handle_registration(user):
    # Benutzer eine private Nachricht senden
    await user.send('Bitte gib deinen Minecraft-Namen ein:')

    def check(m):
        return m.author == user and isinstance(m.channel, discord.DMChannel)

    try:
        # Auf Antwort des Benutzers warten
        response = await client.wait_for('message', timeout=60.0, check=check)

        # Minecraft-Namen über Mojang API verifizieren
        minecraft_name = response.content.strip()
        response = requests.get(f'{MOJANG_API_URL}{minecraft_name}')
        if response.status_code == 200:
            data = response.json()
            uuid = data['id']

            # Überprüfen, ob die discord_id bereits in der Datenbank vorhanden ist
            sql_check = "SELECT * FROM registered_users WHERE discord_id = %s"
            val_check = (user.id,)
            mycursor.execute(sql_check, val_check)
            existing_user = mycursor.fetchone()

            if existing_user:
                # Benutzerdaten in MySQL-Datenbank aktualisieren
                sql_update = "UPDATE registered_users SET minecraft_name = %s, uuid = %s WHERE discord_id = %s"
                val_update = (minecraft_name, uuid, user.id)
                mycursor.execute(sql_update, val_update)
                mydb.commit()
                await user.send('Deine Registrierungsdaten wurden erfolgreich aktualisiert!')
            else:
                # Benutzerdaten in MySQL-Datenbank einfügen
                sql_insert = "INSERT INTO registered_users (discord_id, minecraft_name, uuid) VALUES (%s, %s, %s)"
                val_insert = (user.id, minecraft_name, uuid)
                mycursor.execute(sql_insert, val_insert)
                mydb.commit()
                await user.send('Danke für die Registrierung!')
        else:
            await user.send('Ungültiger Minecraft-Namen. Bitte versuche es erneut.')
    except discord.errors.Forbidden:
        await user.send('Du musst private Nachrichten von diesem Server aktivieren, um dich zu registrieren.')
    except TimeoutError:
        await user.send('Zeitüberschreitung. Bitte versuche es erneut.')

# Bot mit dem Token starten
client.run(TOKEN)

The Bot is also working in the deployment process for some seconds but stuck in “Promote” state and switch to error after a minute
image

hostingpid1: an open port was not detected

Hi @CyboRax , welcome back!
Try deploying your bot as Reserved VM, Background worker. It should work then.
Hope this helps!

4 Likes

Is there also a solution for Autoscale deployment?

Generally, deploying bots as autoscale is unstable at best, and doesn’t work at all at worst, so no, not really.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.