【发布时间】:2021-11-07 07:36:50
【问题描述】:
我正在尝试向我的机器人添加声音功能,但在播放声音时遇到了问题。我的控制台没有出现任何错误,所以我不确定问题出在哪里。如果我打印(声音)我得到无,所以我认为问题是没有声音,但我想知道为什么会这样。
更新:我添加了 voice.is_playing() 来检查音频是否正在播放,它返回 true 但没有发出音频。
@commands.command()
async def play(self, ctx, url : str):
voice_state = ctx.author.voice
channel = ctx.author.voice.channel
voice_channel = discord.utils.get(ctx.guild.voice_channels, name = str(channel))
if voice_state is None:
return await ctx.send("Enter voice channel")
await voice_channel.connect()
voice = discord.utils.get(self.client.voice_clients, guild = ctx.guild)
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
await ctx.send("Wait for song to end")
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'
}]
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith('.mp3'):
os.rename(file, 'song.mp3')
sound = voice.play(discord.FFmpegPCMAudio("song.mp3"))
print(sound) #returns none
【问题讨论】:
-
discord.py 页面上有音乐命令的官方示例。您可以查看here供您参考。
-
机器人是否连接到语音通道?
标签: python discord discord.py bots