【发布时间】:2021-10-30 12:34:32
【问题描述】:
您好,我是初学者,我的代码有问题。当我第一次使用该命令时,机器人进入语音通道并且它运行良好,但是当再次使用该命令时它不再起作用。每次使用该命令时,我都尝试断开机器人的连接并且它可以工作,但这不是我真正想要的,如果机器人已经连接到频道,我需要它像 kdbot 那样播放音频。有人可以帮帮我吗?
@bot.command()
async def tts(ctx,*, text:str):
global gTTS
language = "es-us"
user = ctx.author
speech = gTTS(text=text,lang=language,slow=False)
speech.save("audio.mp3")
channel = user.voice.channel
try:
vc = await channel.connect()
except:
print("Already connected")
#vc = discord.VoiceClient(bot,channel)
#await vc.connect(reconnect=True,timeout=4)
vc.play(discord.FFmpegPCMAudio('audio.mp3'), after=None)
counter = 0
cwd = os.getcwd()
duration = audio_len(cwd + "/audio.mp3")
while not counter >= duration:
await asyncio.sleep(1)
counter += 1
#await vc.disconnect()
【问题讨论】:
标签: python discord.py text-to-speech