简体   繁体   English

没有调用StreamPlayer“之后”功能

[英]StreamPlayer “after” function isn't being called

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. 我正在创建一个可以播放音乐的Discord机器人,但我有点受阻,因为当该机器人完成播放音乐时,它应该离开当前的语音通道。 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. 但这并没有发生,尽管我遵循了“ 常见问题”页面上的指示,但是根本没有调用我的after函数vc.disconnect

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. 问题是vc.disconnect是协程。 You need to handle it differently, as the after call doesn't await the coroutine since the voice player is just a Thread . 您需要以不同的方式处理它,因为语音播放器只是一个Thread ,因此after调用不会等待协程。

According to the docs , this is how it should be handled: 根据docs ,这是应该如何处理的:

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: 同样如docs所述,以下警告:

Warning 警告

This function is only part of 3.5.1+ and 3.4.4+. 此功能只是3.5.1+和3.4.4+的一部分。 If you are not using these Python versions then use discord.compat. 如果您没有使用这些Python版本,请使用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: 这意味着,如果您正在运行Python 3.4.0-3.4.3或3.5.0, my_after需要将my_after更改为此:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM