如何使用discord.py修改添加图像的响应消息[重复]

问题描述 投票:0回答:1

我发送带有文本、图像和选择菜单视图的响应,选择 I edit_message 带有新文本和新视图(按钮),选择 I edit_message 带有新视图(选择菜单),选择 I edit_message 带有文本和根据我的选择之一,我想使用另一张图像,因此我编辑消息并引发此错误

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'

这是我的代码

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

编辑

我试过了

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)

并得到

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'

然后

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'
python python-3.x discord discord.py
1个回答
0
投票

InteractionResponse.edit_message
没有
file
kwarg,请使用
attachment

© www.soinside.com 2019 - 2024. All rights reserved.