0
votes

i make a command for my discord bot, that play some sound from video on youtube. This function work good, if you call her one time, but on the second time i get error that my bot already connected into this voice channel. so i tried to disconnect the bot after he play the video, but it didn't work.

The Function:

@bot.command(pass_context=True)
async def video(ctx, msg):
    author = ctx.message.author
    voice_channel = author.voice_channel
    vc = await bot.join_voice_channel(voice_channel)
    player = await vc.create_ytdl_player(url)
    player.start()   
    await vc.disconnect() #not good, the video not playing

error without await vc.disconnect() :

    yield from injected(*ctx.args, **ctx.kwargs)
  File "/usr/local/lib/python3.6/dist-packages/discord/ext/commands/core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ClientException: Already connected to a voice channel in this server

thank for helpers ^^ have a nice day!

1

1 Answers

0
votes

You should have a look at how danny does it in the examples. Otherwise your issues can be addressed by checking if the bot is already connected to a servers voice channel and second not disconnecting once you do player.start() since this does not block for the stream player to finish playing at which point you disconnect from the voice channel and the stream just dies.