上传 mp3 使用 Discord 斜杠命令 [discord.py]

huangapple go评论85阅读模式
英文:

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"))

错误:
上传 mp3 使用 Discord 斜杠命令 [discord.py]

非常感谢任何帮助!

英文:

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"))

Error:
上传 mp3 使用 Discord 斜杠命令 [discord.py]

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(...)
...

huangapple
  • 本文由 发表于 2023年7月3日 18:16:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定