我希望用户能够更改我的不和谐音乐机器人的音量。我已经尝试过这样做,但是它似乎没有用。我已经在外面将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!")
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