I need help with a command

Hello, I’m currently in the process of creating a bot with a specific feature of generating images via a custom API. however, I’ve encountered a persistent issue AttributeError: 'InteractionResponse' object has no attribute 'send' that appears to pervade every attempt at rectification. despite exhausting all imaginable solutions, I’m afraid my efforts have been to no avail. I’d deeply appreciate your invaluable expertise to aid me in rectifying this issue.

Code:

@_bot.tree.command(name="image", description="Generate an image based on a prompt.")
@app_commands.choices(
    model=[
        app_commands.Choice(name="SDXL", value="sdxl"),
        app_commands.Choice(name="Kandinsky 2.2", value="kandinsky-2.2"),
        app_commands.Choice(name="Kandinsky 2", value="kandinsky-2"),
        app_commands.Choice(name="Dall-E", value="dall-e"),
        app_commands.Choice(name="Stable Diffusion 2.1", value="stable-diffusion-2.1"),
        app_commands.Choice(name="Stable Diffusion 1.5", value="stable-diffusion-1.5"),
    ]
)
@app_commands.choices(
    size=[
        app_commands.Choice(name="Small", value="256x256"),
        app_commands.Choice(name="Medium", value="512x512"),
        app_commands.Choice(name="Large", value="1024x1024"),
    ]
)
@app_commands.describe(
    prompt="A prompt for the image.",
    size="The size of the generated image.",
)
async def generation(
    ctx: discord.Interaction,
    prompt: str,
    model: app_commands.Choice[str],
    size: app_commands.Choice[str],
):

    embed = discord.Embed(
        title="Image",
        description="Generate an image based on a prompt.",
        color=discord.Color.from_rgb(0, 0, 200),
    )
    _image = conversation.image(
        model.value,
        size.value,
        prompt,
    )
    _file = discord.File(
      "./data/image.png", 
      filename="output.png", 
      spoiler=False, 
      description=prompt
    )
    await ctx.response.send(file=_file, embed=embed)

You’re trying to call a method that doens’t exist on the object.
IIRC Interaction object does not have a respond.send method. You should use either ctx.response.send_message() or await ctx.send().

So try to change the last line from your code to:

await ctx.response.send_message(file=_file, embed=embed)

.
.
.

Ps.: if conversation.image()is async you must include the await before.

_image = await conversation.image(
2 Likes

Same error, and I did those exact things.

Are you trying to send an initial response or are you trying to edit the original response?

1 Like

I’m just trying to instantly send the image after its generated, this could be the error because I’m pretty sure you require a message before that.

Are you sure you are using the latest version from Discord.py?

I’m looking at the docs and it should solve the issue using ctx.responde.send_message.

Try ctrl+c / ctrl+v this session of your code and run again:

async def generation(
    ctx: discord.Interaction,
    prompt: str,
    model: app_commands.Choice[str],
    size: app_commands.Choice[str],
):

    embed = discord.Embed(
        title="Image",
        description="Generated an image based on the prompt.",
        color=discord.Color.from_rgb(0, 0, 200),
    )
    
    _image = await conversation.image(
        model.value,
        size.value,
        prompt,
    )

    _file = discord.File(
      "./data/image.png", 
      filename="output.png", 
      spoiler=False
    )

    await ctx.response.send_message(embed=embed, file=_file)
2 Likes

let me reinstall it, this could be my issue.

my help command works, but the image gen one doesn’t:

help command:

@_bot.tree.command(name="help", description="Receive a list of commands for Mesa.")
async def help(interaction: discord.Interaction):
    embed = discord.Embed(
        title="Help",
        description="Receive a list of commands for *Mesa*.",
        color=discord.Color.from_rgb(0, 0, 200),
    )
    embed.add_field(name="``/image <prompt> <model> <size>``", value="Coming **soon.**", inline=True)
    embed.add_field(
        name="``@Mesa <message>``",
        value="Receive an answer/response to your designated question.",
        inline=False,
    )
    await interaction.response.send_message(embed=embed, ephemeral=True)

I think it’s better if you debug the whole thing to see where the error is.

async def generation(
    ctx: discord.Interaction,
    prompt: str,
    model: app_commands.Choice[str],
    size: app_commands.Choice[str],
):

    print("Debug: Inside generation function")  # For example create a debug here

    try:
        print(dir(ctx.response))  # Debug to see available methods
    except Exception as e:
        print(f"Debug: Could not print dir(ctx.response) - {e}")

    try:
        embed = discord.Embed(
            title="Image",
            description="Generated an image based on the prompt.",
            color=discord.Color.from_rgb(0, 0, 200),
        )

        _image = await conversation.image(
            model.value,
            size.value,
            prompt,
        )

        _file = discord.File(
          "./data/image.png", 
          filename="output.png", 
          spoiler=False
        )

        await ctx.response.send_message(embed=embed, file=_file) 
        print("Debug: Message sent")  # Debug here too

    except Exception as e:
        print(f"Debug: An error occurred - {e}")  # And debug here too
1 Like

If “Inside generation function” doesn’t print, then the command is not even being triggered.

Debug: Inside generation function
['__annotations__', '__class__', '__class_getitem__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__orig_bases__', '__parameters__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_is_protocol', '_parent', '_response_type', 'autocomplete', 'defer', 'edit_message', 'is_done', 'pong', 'send_message', 'send_modal', 'type']
Debug: An error occurred - 404 Not Found (error code: 10062): Unknown interaction

Well send_message is there and exist, so we good.

He can’t identify the interaction…
It can be a timeout or you need to Defer the interaction.
Discord interactions have a 15-second window in which you must reply or defer the interaction. If none of this happens the 404 error occurs.

You can make an initial message, or defer the message.

Making an inital message will look like this:

async def generation(
    ctx: discord.Interaction,
    prompt: str,
    model: app_commands.Choice[str],
    size: app_commands.Choice[str],
):

    embed = discord.Embed(
        title="Image",
        description="Generating image...",
        color=discord.Color.from_rgb(0, 0, 200),
    )

    # Sending initial message
    await ctx.response.send_message(embed=embed)

    #I'm assuming it's async but try to remove await if it doens't work
    _image = await conversation.image(
        model.value,
        size.value,
        prompt,
    )

    # You have to create new embed for this
    new_embed = discord.Embed(
        title="Image",
        description="Generated an image based on the prompt.",
        color=discord.Color.from_rgb(0, 0, 200),
    )

    _file = discord.File(
      "./data/image.png", 
      filename="output.png", 
      spoiler=False, 
      description=prompt
    )

    # Here we edit the original message
    await ctx.response.edit_original_response(embeds=[new_embed], attachments=[_file])

If you prefer to defer the message you can look at discord.py docs.

1 Like
AttributeError: 'InteractionResponse' object has no attribute 'edit_original_response'

Discord hates me. :sob:

actually wait, let me try something rq.

Share your repl if it doens’t work

1 Like

I managed to fix it, but now I’m getting:

InteractionResponded: This interaction has already been responded to before

I solved it, it was actually really easy.

Hey @Sky. It’s great to hear that you’ve fixed your problem, but please provide an explanation to how you did, so that other people who have the same problem know the solution.

async def generation(
    interaction: discord.Interaction,
    prompt: str,
    model: app_commands.Choice[str],
    size: app_commands.Choice[str],
    # num: int
):
    embed = discord.Embed(
        title="Image Generating",
        description="Generating image...",
        color=discord.Color.from_rgb(0, 0, 200),
    )
  
    await interaction.response.send_message(embed=embed)
  
    resp = await conversation.image(
        model.value,
        size.value,
        prompt,
        # num
    )

    new_embed = discord.Embed(
        title="Image Generated",
        description=f"Prompt: `{prompt}`",
        color=discord.Color.from_rgb(0, 0, 200),
    )
    new_embed.set_image(url=resp)
  
    try:
      await interaction.edit_original_response(
        embeds=[new_embed], 
      )
    except Exception as e:
      await interaction.edit_original_response(content="Please try again.")

The issue was I kept doing interaction.response.edit_original_response, instead of the correct way interaction.edit_original_response. The reason this even occurred was because some of the other interactions include ‘response’ in them, so I naturally just assumed that did as well.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.