首页 > 解决方案 > 我的不和谐机器人音乐命令似乎不起作用。我导入了 PyNacl 和 youtube_dl,但它仍然不起作用

问题描述

我的代码可能有一些错误,但在这里。我只是希望有办法解决这个问题,因为我一直在尝试制作一些音乐命令,但它们都没有奏效。我不确定为什么这段代码不起作用。它没有给我任何错误。先感谢您。

@client.command(pass_context=True)
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("Wait for the current playing music to end or use the 'stop' command")
        return

    voiceChannel = discord.utils.get(ctx.guild.voice_channels, name='General')
    await voiceChannel.connect()
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)

    ydl_opts = {
        'format': '249/250/251',
        '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")
    voice.play(discord.FFmpegPCMAudio("song.mp3"))

标签: pythondiscord.py

解决方案


推荐阅读