python - 如何让我的 discord 机器人在加入语音 channel 时立即播放音频文件,例如 airhorn solutions 机器人?

标签 python python-3.x discord.py

@client.command(pass_context=True)
async def joinvoice(ctx):
    """Joins user's voice channel"""
    author = ctx.message.author
    voice_channel = author.voice_channel
    vc = await client.join_voice_channel(voice_channel)

这就是我目前让机器人加入语音 channel 的方式,我如何让它在加入时播放音频文件?我几乎没有经验,到目前为止,整个机器人都是通过提问和大量谷歌搜索来编码的。非常感谢任何帮助,谢谢!

最佳答案

您必须创建一个 StreamPlayer 并使用播放器操作来播放音频。这是我编写的一些示例代码,用于与我的 discord 机器人一起播放 vuvuzela:

@client.command(
    name='vuvuzela',
    description='Plays an awful vuvuzela in the voice channel',
    pass_context=True,
)
async def vuvuzela(context):
    # grab the user who sent the command
    user = context.message.author
    voice_channel = user.voice.voice_channel
    channel = None
    if voice_channel != None:
        channel = voice_channel.name
        await client.say('User is in channel: ' + channel)
        vc = await client.join_voice_channel(voice_channel)
        player = vc.create_ffmpeg_player('vuvuzela.mp3', after=lambda: print('done'))
        player.start()
        while not player.is_done():
            await asyncio.sleep(1)
        player.stop()
        await vc.disconnect()
    else:
        await client.say('User is not in a channel.')

关于python - 如何让我的 discord 机器人在加入语音 channel 时立即播放音频文件,例如 airhorn solutions 机器人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45346957/

相关文章:

python - for循环保存到数组但跳过保存元素

python - Django 休息框架 : Pagination w/ListModelMixin

python - Scrapy 正在跟踪并抓取不允许的链接

python-3.x - 创建环境Python-3.5错误

python - 如何检查是否从客户端收到字节?

python - 每个 Cog 的前缀不同?

python - 列出没有共同公会的 friend ,discord.py 重写

python - 如何让我的 Python Discord 机器人检查消息是否由机器人本身发送?

Python: += 不支持的操作数类型: 'int' 和 'NoneType

python - 在 python 子进程中捕获 logger.info 输出?