Search code examples
pythondiscorddiscord.py

Upload mp3 using discord slash commands [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: error

Any help at all is appreaciated!


Solution

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