Search code examples
pythonpython-3.xdiscorddiscord.py

How to modify a response message adding an image with discord.py


I send a response with text, image and select menu view, with the choice I edit_message with new text and new view (buttons), with the choice I edit_message with new view (select menu), with the choice I edit_message with text and with one of my choices I want to use another image so I edit_message and raise this error

await ctx.response.edit_message(embed = embed, file = discord.File(f"image/cartes/{collection}/{id_carte}.png"), ephemeral=True)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: InteractionResponse.edit_message() got an unexpected keyword argument 'file'

this is my code

    (...)
            
    async def select_callback(self, ctx : discord.Interaction, select) :
            if select.values[0] == "Bonbon" :
                inventaire = fonction_list("bonbon", id) # Récupérer l'inventaire de l'utilisateur sous forme de dictionnaire de tous les objets possédés
                (...)
                else :
                    recap_inventaire = discord.Embed(title = "Vous trouvez :")
                    for objet in inventaire :
                        recap_inventaire.add_field(name = "", value = f"- {inventaire[objet]} {objet}", inline = False)
                    
                    await utilisation_inv(ctx, id, inventaire, "bonbon", recap_inventaire)
    
    demande = discord.Embed(title = "Que cherchez vous dans le sac ?")
    demande.set_image(url = "attachment://sac.png")
    await ctx.response.send_message(embed = demande, file = discord.File("image/sac.png"), view = selection_inventaire(), ephemeral = True)
async def utilisation_inv(ctx : discord.Interaction, id : int, inventaire : dict, type : str, embed) :
    (...)
        async def select_callback(self, ctx : discord.Interaction, select) :
            if type == "bonbon" :
                cursor.execute("UPDATE bonbon SET `{}` = `{}` - 1 WHERE id = %s;".format(select.values[0], select.values[0]), (id,))
                await utilisation_bonbon(ctx, select.values[0])
    
    class bouton_utilisation(discord.ui.View) :
        @discord.ui.button(label="Utiliser", style=discord.ButtonStyle.green)
        async def utiliser_callback(self, interaction : discord.Interaction, button):
            for objet in list(inventaire) :
                option.append(discord.SelectOption(label = objet))
            
            await interaction.response.edit_message(view = select_utilisation())
        @discord.ui.button(label="Fermer", style=discord.ButtonStyle.red)
        (...)
    
    await ctx.response.edit_message(embed = embed, view = bouton_utilisation())
async def utilisation_bonbon(ctx : discord.Interaction, bonbon : str) :
    (...)
    elif bonbon == "Chocogrenouille" :
        await tirage_chocogrenouille(ctx, bonbon)
    elif (...)
(...)
await affichage_carte(ctx, id_carte, "collec_s1")
async def affichage_carte(ctx : discord.Interaction, id_carte : int, collection : str) :
    (...)
    embed = discord.Embed(title = info[0], color = couleur)
    embed.add_field(name = info[2], value = info[1], inline = False)
    embed.set_image(url = f"attachment://{id_carte}.png")
    await ctx.response.edit_message(embed = embed, file = discord.File(f"image/cartes/{collection}/{id_carte}.png"))

EDIT

I tried

attach = discord.Attachment(url = "attachment://test.png")
await ctx.response.edit_message(content = "Vous avez mangé un bonbon à hoquet, vous hoquetez pendant 5 minutes", attachments=[attach], embed = None, view = None)

and get

File "c:\Users\Bilbibalou\Documents\Git\Bot-discord\main.py", line 1912, in cmd_test
    attach = discord.Attachment(url = "attachment://test.png")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Attachment.__init__() got an unexpected keyword argument 'url'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Bilbibalou\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\discord\app_commands\tree.py", line 1248, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "C:\Users\Bilbibalou\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\discord\app_commands\commands.py", line 853, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bilbibalou\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\discord\app_commands\commands.py", line 842, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'test' raised an exception: TypeError: Attachment.__init__() got an unexpected keyword argument 'url'

then

attach = discord.Attachment(filename = "test.png")
await ctx.response.edit_message(content = "Vous avez mangé un bonbon à hoquet, vous hoquetez pendant 5 minutes", attachments=[attach], embed = None, view = None)
File "c:\Users\Bilbibalou\Documents\Git\Bot-discord\main.py", line 1912, in cmd_test
    attach = discord.Attachment(filename = "test.png", url = "attachment://test.png")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Attachment.__init__() got an unexpected keyword argument 'filename'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Bilbibalou\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\discord\app_commands\tree.py", line 1248, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "C:\Users\Bilbibalou\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\discord\app_commands\commands.py", line 853, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Bilbibalou\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\discord\app_commands\commands.py", line 842, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'test' raised an exception: TypeError: Attachment.__init__() got an unexpected keyword argument 'filename'

Solution

  • InteractionResponse.edit_message does not have a file kwarg, instead use attachment.