Discord bot stops | Discord.py

Question:

When the bot connects to the voice channel, the program stops and goes no further!
I found this out by displaying the number 1 and 2 in the console:

The console output number 1, then the bot connects to the voice channel and the number 2 is not displayed (the program just stops)

The most interesting thing is that before this bot was working and the music was playing, but with another restart bot began to stop at the moment of connection to the voice channel!

import discord
from discord.ext import commands
import asyncio
import os

intents = discord.Intents.all()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix='/', intents=intents)

@bot.event
async def on_ready():
    voice_channel = bot.get_channel(VoiceChannelIdHere)
    ffmpegPath = r"C:ffmpeg-master-latest-win64-gplffmpeg-master-latest-win64-gplbinffmpeg.exe"
    while True:
        for filename in os.listdir("Music"):
            if not discord.utils.get(bot.voice_clients, guild = discord.utils.get(bot.guilds, name="Sui Ape DAO")):
                print(1)
                vc = await voice_channel.connect()
                print(2)

                vc.source = discord.PCMVolumeTransformer(vc.source, volume = 0.4)
                vc.play(discord.FFmpegPCMAudio(executable = ffmpegPath, source = f"Music\{filename}"))

                while vc.is_playing():
                    await asyncio.sleep(1)

        await asyncio.sleep(4)

if __name__ == "__main__":
    print("Radio")
    bot.run('MyTokenHere')

I found people with the same problem on stackoverflow, but I could not find an answer to help me

Asked By: FDFD FDFD

||

Answers:

Try catching the error and see if it returns anything useful for debugging.

import discord
from discord.ext import commands
import asyncio
import os

intents = discord.Intents.all()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix='/', intents=intents)

@bot.event
async def on_ready():
    voice_channel = bot.get_channel(VoiceChannelIdHere)
    ffmpegPath = r"C:ffmpeg-master-latest-win64-gplffmpeg-master-latest-win64-gplbinffmpeg.exe"
    while True:
        if not discord.utils.get(bot.voice_clients, guild = discord.utils.get(bot.guilds, name="Sui Ape DAO")):
            try:
                print(1)
                vc = await voice_channel.connect()
                print(2)

                vc.source = discord.PCMVolumeTransformer(vc.source, volume = 0.4)
                vc.play(discord.FFmpegPCMAudio(executable = ffmpegPath, source = f"Music\{filename}"))

                while vc.is_playing():
                    await asyncio.sleep(1)

            except Exception as e:
                print(f"Error while connecting to voice channel: {e}")

        await asyncio.sleep(4)

if __name__ == "__main__":
    print("Radio")
    bot.run('MyTokenHere')

I tried this on my discord server and it returns this error for me but you might be getting something else:

Error while connecting to voice channel: expected AudioSource not NoneType.

I also had to run this in order for it to print(2)

pip install -U discord.py[voice]
Answered By: Datertec
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.