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

使用Discord斜杠命令上传MP3文件报错(discord.py)

Discord斜杠命令上传MP3文件报错修复方案

我能通过前缀命令成功上传MP3文件:

@bot.command()
async def upload(ctx):
    await ctx.send(file=discord.File("song.mp3"))

但使用斜杠命令时出现错误,代码如下:

@bot.tree.command(description="test upload mp3")
async def upload(interaction:discord.Interaction):
    await interaction.response.send_message(file=discord.File("song.mp3"))

以下是可行的修复方案:

  • 验证文件路径:确保song.mp3与脚本在同一目录,或使用绝对路径定位文件:

    import os
    # 获取脚本所在目录的绝对路径
    file_path = os.path.join(os.path.dirname(__file__), "song.mp3")
    await interaction.response.send_message(file=discord.File(file_path))
    
  • 处理大文件超时问题:若MP3文件体积较大,先发送延迟响应避免超时,再用跟进消息发送文件:

    @bot.tree.command(description="test upload mp3")
    async def upload(interaction: discord.Interaction):
        await interaction.response.defer()
        await interaction.followup.send(file=discord.File("song.mp3"))
    
  • 检查权限配置:确认机器人在目标服务器拥有「上传文件」权限,且目标频道允许发送附件。

  • 更新discord.py版本:旧版本可能存在斜杠命令附件上传的bug,执行命令更新:

    pip install -U discord.py
    

内容的提问来源于stack exchange,提问作者Srivatsav

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 00:57:02