gpt4 book ai didi

python - 有没有办法让我的不和谐机器人在播放完歌曲后断开与语音 channel 的连接?

转载 作者:行者123 更新时间:2023-12-03 08:45:00 25 4
gpt4 key购买 nike

我想知道是否有办法让我的不和谐机器人在播放 YouTube 视频的音频后离开语音 channel 。我尝试使用 sleep(视频的持续时间),但为了获取和下载要播放的视频,我使用了 pafy,它给出了视频的持续时间,但在00:00:00 格式,算作字符串,而不是整数。我将代码更改为在 after=lamda e:await vc.disconnect 处断开连接,但它给了我一个错误,指出 'await' Outside async function。我播放音乐的代码如下:

channel = message.author.voice.channel
vc = await channel.connect()
url = contents
url = url.strip("play ")
video = pafy.new(url)
await message.channel.send("Now playing **%s**" % video.title)

audio = video.getbestaudio()
audio.download()
duration = video.duration
player = vc.play(discord.FFmpegPCMAudio('%s.webm' % video.title), after=lambda e: await vc.disconnect)

最佳答案

这里有一种在歌曲播放完毕后断开连接的方法(删除 after=)。

添加在vc.play()之后

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

关于python - 有没有办法让我的不和谐机器人在播放完歌曲后断开与语音 channel 的连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61830803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com