#Can someone help with a Python 3 bot code error?

49 messages · Page 1 of 1 (latest)

tawny wagon
#

does the server where you are using the bot have a set voice channel timeout?

#

and does the bot output anythin in the console or the log when it goes silent?

#

there also is a possibility that after some time an exception in your play_song_loop function accours wich silently stops it.

lunar adder
#

That is turned off

lunar adder
tawny wagon
lunar adder
#

Ide? I just run a command via CMD

#

And then I select the .py file that has all the info in that file

tawny wagon
lunar adder
#

Yeah it just says, bot is online, playing etc

#

But the funny thing is that I let this CMD open 24/7

#

I can't close the CMD or the bot leaves the vc haha

#

I literally have to let a PC run day and night, which sucks too, I'd love to host it online somewhere if that's possible

#

But if it would stop going silent, that would be an immense help already

tawny wagon
lunar adder
#

Yes, one moment ma'am

#

That's the error I see in the window

tawny wagon
#

seems like it disconnected from the voice channel

lunar adder
#

Yes, and I wonder why

#

There's no way it gets disconnected by another bot or a permission

#

It says nextcord.errors so I'm wondering what that means

tawny wagon
#

okay basically the error is that nextcord tried to run a voice related action wich in this case would be playing a song while it wasnt connected to audio

#

i think somtimes discord connection to vc has problems here and there so you could try to implement a code that handels the case where its not connected to a voice channel

lunar adder
#

Yes but I wonder why, because when I start it from scratch, the bot joins and plays perfectly fine so I don't know why it would stop all the sudden or get disconnected. I have a really stable internet connection as well and it happened too when I have my computer on ethernet cable, my speeds are like 50 up and 900 down

#

I'm wondering if it's having an issue looping the file, when it's done to have it start again, that it goes wrong there

tawny wagon
#

you could try replacing this with your play_song_loop function. its modified so it tries to reconnect once it disconnects plus i added some debug prints

`async def play_song_loop(voice_client: nextcord.VoiceClient):
while True:
try:
if not voice_client.is_connected():
print("Bot is not connected to a voice channel. Reconnecting...")
guild = voice_client.guild
vc = guild.get_channel(vc_id) # type: ignore
voice_client = await join_vc(guild, vc) # type: ignore
if not voice_client:
print("Failed to reconnect to voice channel.")
return

        voice_client.play(nextcord.FFmpegPCMAudio(executable=ffmpeg_path, source=song_file))
        while voice_client.is_playing():
            await asyncio.sleep(1)

    except Exception as e:
        print(f"Error playing song: {e}")

    finally:
        if not voice_client.is_connected():
            print("Bot got disconnected from the voice channel. Reconnecting...")
            guild = voice_client.guild
            vc = guild.get_channel(vc_id)  # type: ignore
            voice_client = await join_vc(guild, vc)  # type: ignore
            if not voice_client:
                print("Failed to reconnect to voice channel.")
                return`
lunar adder
#

Is that the entire code?

#

Is it possible that you send me the entire code I can replace in the .py file?

tawny wagon
lunar adder
#

So where do I put that in the .py file?

tawny wagon
lunar adder
#

I'm sorry but I have zero knowledge on this, are you able to make the final code with your part included that I just have to run via CMD?

#

But I'm learning as we speak haha

tawny wagon
lunar adder
#

Yeah sure, if you can highlight them where to put them I'll throw them back in

tawny wagon
#

im only changing one function the rest of the code stays the same.

lunar adder
#

That's the code I copied and I didn't take anything out, all the information like vc ID, server ID is in a separate file called config.json

tawny wagon
lunar adder
#

That's the separate json file

tawny wagon
#

ill send you the code in a pastebin rq

#

this should fix the issue of it still trying to do the audio when it looses connection (it can loose connection because of a large range of things even when the internet connection is good but with the modified code it will try to reconnect everytime it looses the connection)

lunar adder
#

Thank you so much for this

#

I'll give that a try and I'll let you know