首页 > 解决方案 > Discord Python Bot 播放音乐 - url 是缺少的必需参数

问题描述

我目前正在使用 python 编写一个不和谐的机器人并遇到了一个问题。我尝试让我的机器人使用“播放”命令播放音乐。

我的代码:

@bot.command(name='play')
async def play(self, ctx, url : str):
    if ctx.author.voice is None:
        embed = discord.Embed(title=f'> Du musst in einem Voice Channel sein, um diesen Befehl zu verwenden.', description='Diese Nachricht wird nach 15 Sekunden automatisch gelöscht.', color=embed_color, timestamp=datetime.now().astimezone(tz=de))
        embed.add_field(name='⠀', value='⠀')
        embed.set_footer(text=f'Angefordert von {ctx.author.name} • {ctx.author.id}', icon_url=ctx.author.avatar_url)
        bot_msg = await ctx.send(embed=embed)
        await asyncio.sleep(15)
        await bot_msg.delete()
    voice_channel = ctx.author.voice.channel
    if ctx.voice_client is None:
        await voice_channel.connect()
    else:
        await ctx.voice_client.moce_to(voice_channel)
            
    ctx.voice_client.stop()
    FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
    YDL_OPTIONS = {'format':"bestaudio"}
    vc = ctx.voice_client
        
    with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
        info = ydl.extract_info(url, download=False)
        url2 = info['formats'][0]['url']
        source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
        vc.play(source)

但是在运行命令时出现以下错误:

discord.ext.commands.errors.MissingRequiredArgument:url 是缺少的必需参数。

这是为什么?

PS:我在不和谐频道中输入的内容:“.play https://www.youtube.com/watch?v=V0woPCb5xk8” 所以我显然给了它一个可以使用的网址......:c

标签: pythondiscorddiscord.pybots

解决方案


推荐阅读