使用discord.py斜杠命令发送文件失败问题求助
discord.py斜杠命令发送文件失败的解决方法
错误根源
你的代码里重复对文件对象进行了discord.File包装:先创建了file_to_send = discord.File("LoveCherryMotion.mp4"),调用send_message时又把这个已包装好的对象再传给discord.File(),这会触发参数类型错误,导致机器人无法正常处理文件发送请求。
修正后的代码
直接使用已创建的文件对象即可:
@bot.tree.command(name="lcm", description="Love Cherry Motion") async def lcm(interaction: discord.Interaction): file_to_send = discord.File("LoveCherryMotion.mp4") await interaction.response.send_message(file=file_to_send)
也可以简化成一步:
@bot.tree.command(name="lcm", description="Love Cherry Motion") async def lcm(interaction: discord.Interaction): await interaction.response.send_message(file=discord.File("LoveCherryMotion.mp4"))
额外排查点
- 确认
LoveCherryMotion.mp4文件和机器人脚本在同一目录,或者使用完整的绝对文件路径。 - 检查机器人在目标频道是否拥有发送附件的权限,频道本身是否允许上传文件。
内容的提问来源于stack exchange,提问作者sonhyeju.
相关产品推荐
相关产品推荐

