【问题标题】:How do I make my discord bot play an audio file right when it joins a voice channel, like the airhorn solutions bot?如何让我的不和谐机器人在加入语音通道时立即播放音频文件,比如 airhorn 解决方案机器人?
【发布时间】:2018-01-02 22:58:09
【问题描述】:
@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)

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

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    您必须创建一个 StreamPlayer 并使用播放器操作来播放音频。这是我编写的一些示例代码,用于使用我的不和谐机器人播放 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.')
    

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 2020-09-05
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 2021-05-23
      • 2023-03-18
      • 2020-08-30
      • 1970-01-01
      相关资源
      最近更新 更多