如何使用 python 更改我的 discord bot 的音量?

How do I change the volume of my discord bot with python?

我希望用户能够更改我的 discord 音乐机器人的音量。我试过这样做,但似乎没有用。 我已经在外面定义了vc为"something",然后在try和except中用它来播放音乐。我想知道这是否导致了问题。

elif contents.startswith("volume"):
            volume = contents
            volume = volume.strip("volume ")
            volume = int(volume)

            if volume <= 100:
                volume = volume / 10
                vc.source = discord.PCMVolumeTransformer(vc.source)
                vc.source.volume = volume
            else:
                message.channel.send("Please give me a number between 0 and 100!")

PCMVolumeTransformer 需要一个介于 0 和 1.0 之间的浮点数。

PCMVolumeTransformer 的初始设置应包括音量,并且应紧跟在 vc.play() 之后。喜欢vc.source = discord.PCMVolumeTransformer(vc.source, volume=1.0)

然后在您的消息处理中,您可以尝试类似的操作:

** 已更新,通过添加语音连接功能避免使用全局语音 ('vc') 连接。请注意,此功能仅适用于音量信息。原来播放音频的连接是分开的,还在用

    if message.content.lower().startswith('volume '):
        new_volume = float(message.content.strip('volume '))
        voice, voice.source = await voice_connect(message)
        if 0 <= new_volume <= 100:
            new_volume = new_volume / 100
            voice.source.volume = new_volume
        else:
            await message.channel.send('Please enter a volume between 0 and 100')

@bot.command()
async def voice_connect(message):
    if message.author == bot.user:
        return

    channel = message.author.voice.channel
    voice = get(bot.voice_clients, guild=message.guild)

    if voice and voice.is_connected():
        return voice, voice.source
    else:
        voice = await channel.connect()
        voice.source = discord.PCMVolumeTransformer(voice.source, volume=1.0)
        print(f"The bot has connected to {channel}\n")

    return voice, voice.source