You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python开发Discord音乐机器人play命令报url必填参数缺失错误

Discord.py play命令缺失url参数报错解决方案

核心报错原因

  • 函数参数列表多了不必要的self参数:如果该命令没有定义在discord.py的Cog插件类中,self是多余参数。discord.py调用命令时会自动将上下文ctx作为第一个参数传入,你多写self后,你输入的url会被框架识别为self的入参,实际需要的url参数就处于未传入状态,直接触发本次报错。
  • 装饰器使用错误:如果该命令确实是写在Cog类内部,你误用了@bot.command装饰器,类内命令需要用@commands.command(name='play')装饰,否则也会出现参数匹配异常。
  • 额外拼写错误:代码中await ctx.voice_client.moce_to(voice_channel)的moce_to是笔误,正确写法为move_to,后续运行到该逻辑时会触发属性不存在报错。

修复后代码示例

非Cog类的全局命令版本

@bot.command(name='play')
async def play(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.move_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)

Cog类内的命令版本

# 仅展示命令部分,完整Cog类结构需要自行补齐
@commands.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.move_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)

其他注意事项

目前youtube-dl已经停止维护,你可以替换为yt-dlp获得更好的视频链接解析兼容性,用法和youtube-dl完全一致,直接替换库名即可。

内容的提问来源于stack exchange,提问作者SoldØut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 02:36:05