首页 > 解决方案 > 我的代码意外删除了 Discord.py 音乐机器人中的“song.mp3”文件

问题描述

我正在编写我的不和谐机器人,我试图让机器人不尝试连接两次。就像,如果它已经连接任何用户发送另一个“!播放”命令,我不希望它出错。但这不是我的问题。我或我的代码意外删除了我的“song.mp3”文件。我怎么拿回来。我关闭了pycharm,所以我不能做'CRTL + Z'。

@client.command()

async def play(ctx, url: str):

song_there = os.path.isfile("song.mp3")
try:
    if song_there:
        os.remove("song.mp3")
except PermissionError:
    await ctx.send("Şimdi Çalan şarkının bitmesini bekleyin. Yada '!stop' komutunu kullanın.")
    return
# connects the voice channel
connected = ctx.author.voice
audio = discord.FFmpegPCMAudio("song.mp3")

voiceChannel = discord.utils.get(ctx.guild.voice_channels, name='music')
if not discord.client.VoiceClient == None:  # None being the default value if the bot isnt in a channel (which is why the is_connected() is returning errors)
    await voiceChannel.connect()
    await ctx.send(f"Joined **{voiceChannel}**")
    discord.client.VoiceClient.play()
else:
    await ctx.send("I'm already connected!")



audio = discord.FFmpegPCMAudio("song.mp3")

# Dowloads the video from the link given

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")

标签: pythonffmpegdiscorddiscord.pymp3

解决方案


尝试在回收站中搜索它...


推荐阅读