I want the user to be able to change the volume of my discord music bot. 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. 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.
The initial setting of the PCMVolumeTransformer
should include the volume and should be placed right after your vc.play()
. Like 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. Please note that this function is only for the volume message. 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