I am creating a discord bot that can play music but I'm a bit stuck because when the bot finishes playing music is should leave the current voice channel. However this isn't happening, instead my after
function vc.disconnect
isn't being called at all though I've followed the instructions on the FAQ page.
vc = await bot.join_voice_channel(ctx.message.author.voice_channel)
player = await vc.create_ytdl_player(arg.split(' ')[1], after=vc.disconnect)
player.start()
The problem is that vc.disconnect
is a coroutine. You need to handle it differently, as the after
call doesn't await the coroutine since the voice player is just a Thread
.
According to the docs , this is how it should be handled:
def my_after():
coro = vc.disconnect()
fut = asyncio.run_coroutine_threadsafe(coro, client.loop)
try:
fut.result()
except:
pass
player = await voice.create_ytdl_player(url, after=my_after)
player.start()
Also as noted by the docs , the following warning:
Warning
This function is only part of 3.5.1+ and 3.4.4+. If you are not using these Python versions then use
discord.compat.
.
Meaning that if you're running Python 3.4.0-3.4.3 or 3.5.0, your my_after
needs to be changed to this:
def my_after():
from discord.compat import run_coroutine_threadsafe
coro = vc.disconnect()
fut = run_coroutine_threadsafe(coro, client.loop)
try:
fut.result()
except:
pass
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.