简体   繁体   English

如何使用 python 更改 discord 机器人的音量?

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

I want the user to be able to change the volume of my discord music bot.我希望用户能够更改我的 discord 音乐机器人的音量。 I have tried doing it, but it does not seem to work.我试过这样做,但它似乎不起作用。 I already defined vc as "something" outside, and then used it to play music in try and except.我已经将 vc 定义为外部的“某物”,然后用它在 try 和 except 中播放音乐。 I wonder if that is causing the problem.我想知道这是否导致了问题。

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 is expecting a float between 0 and 1.0. PCMVolumeTransformer期望介于 0 和 1.0 之间的浮点数。

The initial setting of the PCMVolumeTransformer should include the volume and should be placed right after your vc.play() . PCMVolumeTransformer的初始设置应该包括音量,并且应该放在vc.play()之后。 Like vc.source = discord.PCMVolumeTransformer(vc.source, volume=1.0)vc.source = discord.PCMVolumeTransformer(vc.source, volume=1.0)

Then in your message processing you can try something like:然后在您的消息处理中,您可以尝试以下操作:

** Updated to avoid using a global for the voice ('vc') connection by adding a voice connect function. ** 更新以避免通过添加语音连接 function 来使用全局语音 ('vc') 连接。 Please note that this function is only for the volume message.请注意,此 function 仅用于音量消息。 The original connection to play the audio is separate is still in use.播放音频的原始连接是单独的,仍在使用中。

    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

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

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