使用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
相关产品推荐
相关产品推荐

