如何使用 python 更改不和谐机器人的音量?

Eey*_*yeo 4 python volume discord

我希望用户能够更改我的不和谐音乐机器人的音量。我尝试过这样做,但似乎不起作用。我已经将 vc 定义为外面的“东西”,然后用它在 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!")
Run Code Online (Sandbox Code Playgroud)

Dav*_*ere 5

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
Run Code Online (Sandbox Code Playgroud)