why nothing happens when I execute commands on the discord bot

Viewed 116

i got an problem with this code here... i am starting with programming and this discord bot is my first bigger project and i want to lern it

# bot.py
import os
import random
import discord
import time
import sys
import logging

from discord.ext import commands
from dotenv import load_dotenv

# importing your Discord Token
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

# create bot var and prefix
bot = commands.Bot(command_prefix='h!')

logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding = 'utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)

def restart_bot():
    python = sys.executable()
    os.execl(python, python, * sys.argv)

@bot.event
async def on_ready():
    print(f'{bot.user.name} has connected to Discord!')
    channel = discord.utils.get(bot.get_all_channels(), name='testl')
    await channel.send("Ich bin wieder da")

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.errors.CheckFailure):
        await ctx.send('Du hast die benötigten Rechte nicht!')

@bot.command(name='69', help='Just nice!')
async def six_nine(ctx):
    await ctx.send('nice!')

@bot.command(name='roll-dice', help='(würfel, augen) simuliert einen würfel')
async def roll_dice(ctx, number_of_dices, number_of_sides):
    dice = [
        str(random.choice(range(1, number_of_sides)))
        for _ in range(number_of_dices)
    ]
    await ctx.send(', '.join(dice))

@bot.command(name='relaod', help='startet einmal den bot neu')
async def bot_reload(ctx):
    await ctx.message.delete()
    message = await ctx.send("Starte neu, gib mir 5 Sekunden")
    restart_bot()


@bot.command(name='create-voice', help='Erstellt einen Voice channel')
@commands.has_role('Admin')
async def create_channel(ctx, channel_name="Default Channel"):
    guild = ctx.guild
    existing_channel = discord.utils.get(guild.channels, name=channel_name)
    if not existing_channel:
        print(f'Erstelle den Spach-channel "{channel_name}"')
        await guild.create_voice_channel(channel_name)

if __name__ == "__main__":
    bot.run(TOKEN)

it worked all, until i added the reload/restart function after that nothing works anymore, the bot starts, and the on_ready() function is called, but in discord i cannot issue any command if you need the log file let me know

logger = logging.getLogger('discord')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename='discord.log', encoding = 'utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)

    def restart_bot():
        python = sys.executable()
        os.execl(python, python, * sys.argv)
    @bot.command(name='relaod', help='startet einmal den bot neu')
    async def bot_reload(ctx):
        await ctx.message.delete()
        message = await ctx.send("Starte neu, gib mir 5 Sekunden")
        restart_bot()
    @bot.command(name='relaod', help='startet einmal den bot neu')
    async def bot_reload(ctx):
        await ctx.message.delete()
        message = await ctx.send("Starte neu, gib mir 5 Sekunden")
        restart_bot()

thats all the code i added before it stoped working

1 Answers

I don't understand much of the code, but if you aren't trying already, execute the code in a new window and then close the current window. This can be done via:

import os
os.system('YourBot.py')
os.exit()

I am not 100% sure that this will work but you can give it a throw.

Related