【发布时间】:2020-11-24 19:57:33
【问题描述】:
所以作为我开始编程后的第一个“主要”项目,我决定制作一个 Discord Bot。这里的问题是我的“跳过”命令。不知怎么的,但我不明白是怎么回事。
def play_next(ctx):
if len(songs_list) >= 2:
print(songs_list,"before del")
del songs_list[0]
print(songs_list[0], "new song")
try:
if os.path.isfile("song.mp3"):
os.remove("song.mp3")
except PermissionError:
print("permissionerror")
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([songs_list[0]])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"), after=lambda e: play_next(ctx))
voice.isplaying()
还有跳过命令:
@client.command(pass_context=True)
async def skip(ctx):
voice.stop()
voice.skip()
try:
os.remove("song.mp3")
except:
pass
play_next(ctx)
我知道这不是处理该问题的最佳方式,但我才刚刚开始编写代码,这就是我让它工作的方式。 在跳过命令中,当我不使用 voice.skip() 时,我会在控制台中打印出 PermissionError。
包含它后,我反而收到消息“VoiceClient”对象没有属性“skip”,但到目前为止一切正常。有人可以解释为什么吗? 我的意思是如果跳过不存在,那么它为什么会起作用?又是如何绕过 PermissionError 的?
【问题讨论】:
-
您使用哪种操作系统?在哪个位置存储您的文件?是用户可访问的目录吗?
-
Windows 10。python 文件与“song.mp3”位于同一目录中,即使没有管理员也可以访问所有内容。在我看来,该文件似乎仍在使用中。
-
当您关闭主进程时,
discord.FFmpegPCMAudio是否仍在处理文件?您可以在活动监视器中检查一些 python 进程 -
看来确实如此。我的意思是代码有效……但我不知道为什么。
标签: python ffmpeg discord.py audio-player