繁体   English   中英

如何让我的 Discord 机器人在特定 mp3 文件的时间长度内停留在语音通道中?

[英]How do I make my Discord bot stay in the voice channel for the length of time of a specific mp3 file?

我一直在为我的个人不和谐服务器开发一个机器人,到目前为止,它播放的 mp3 都是固定长度的,所以我告诉机器人根据 mp3 的时间长短休眠一段时间。 现在使用我的新命令将播放不同长度的 mp3,我不知道从哪里开始。 您可以在我的代码中看到我如何让它进入睡眠状态,但这将不再起作用,因为 mp3 可能很长或很短。 我试图找出一种方法来通过 python 获取有关 mp3 的信息,并告诉它在 mp3 的长度内休眠。 有人可以帮我解决这个问题或指出我正确的方向吗? 谢谢。

过去,我尝试使用 vc.wait 之类的命令,但机器人一直加入然后立即离开。 sleep 命令非常适合我过去的使用,所以我只是使用它。

@client.command(pass_context=True)
async def tts(ctx):
    #get message content and send to google text to speech api
    msg = ctx.message.content
    tts = gTTS(msg[5:])

    #here is where the mp3 is made and saved
    tts.save('message.mp3')

    #find length of mp3

    channel = ctx.message.author.voice.channel
    vc = await channel.connect()
    source = FFmpegPCMAudio('./message.mp3')
    player = vc.play(source)

    #the method I've been using for mp3s, increasing or decreasing manually 
#per command based on mp3 length. This is the command that I now need to be 
#dynamic. Replace 15 with length of mp3.
    await asyncio.sleep(15)
    await vc.disconnect(force=True)

我不期待任何事情,但我希望能在正确的方向上找到一些帮助或推动,以便当场找到 mp3 的长度并相应地调整机器人在语音通道中花费的时间。 谢谢你。 对不起,我的代码太草率了,这只是一个个人项目,我没想到有人会看到它。

根据此处找到的语音客户端文档,您可以在vc.play(source)创建一个回调函数,可能是这样的:

async def playCallback(error):
    await vc.disconnect(force=True)
.
.
.

player = vc.player(source, after=playCallback)

让我知道这是否有效 - 编码愉快!

通过更改这些行:

source = FFmpegPCMAudio('./message.mp3')
player = vc.play(source)    

使用这些行:

vc.play(discord.FFmpegPCMAudio('./message.mp3'), after=lambda e: print('done', e))

播放完音频文件后,bot会自动断开连接。

暂无
暂无

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

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