英文:
Upload mp3 using discord slash commands [discord.py]
问题
我可以使用以下代码使我的机器人上传一个 .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"))
非常感谢任何帮助!
英文:
I am able to make my bot upload an .mp3
file using the following code:
@bot.command()
async def upload(ctx):
await ctx.send(file=discord.File("song.mp3"))
But when I try to do the same using slash commands, I get an error. Here is the code and the error image:
@bot.tree.command(description="test upload mp3")
async def upload(interaction:discord.Interaction):
await interaction.response.send_message(file=discord.File("song.mp3"))
Any help at all is appreaciated!
答案1
得分: 1
Ctx以普通消息的形式发送命令的响应,因此没有严格的时间要求或其他要求。 Discord的开发者门户 表明:
> 您必须在接收到事件后的3秒内发送初始响应。
这意味着我们必须在3秒内响应该交互。由于没有办法在上传过程中自动加速,我们可以推迟交互。推迟实际上告诉Discord我们已经收到了交互并正在处理它。使用方法如下。
...
await interaction.response.defer()
await interaction.followup.send(...)
...
英文:
Ctx sends a response for a command as a normal message, so there isn't any strict timings or whatever. Discords developer portal states that:
> You must send an initial response within 3 seconds of receiving the event.
So this means we must respond to the interaction within 3 seconds. Since there isn't any way we can magically speed up the upload, we can defer the interaction. Deferring essentially tells discord that we have received the interaction and are working on it. It would be used as below.
...
await interaction.response.defer()
await interaction.followup.send(...)
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论