Search code examples
pythondiscorddiscord.py

AttributeError: 'Interaction' object has no attribute 'edit' in discord.py


I'm working on a Discord bot using the discord.py module in Python, and I'm trying to use the edit method on an Interaction object. However, I'm getting an AttributeError with the message "'Interaction' object has no attribute 'edit'" when I try to call this method.

I've already upgraded to the latest version of discord.py using pip, and I'm importing the correct version of the module in my code. I've also checked the documentation for the discord.py module, and it appears that the edit method should be available on the Interaction object.

This code also used to work perfectly fine when hosted on another machine.

Simplified version of Botinfo.py:

import discord
from discord.ext import commands
import time
from main import start_time
from main import emergenymessage

class Botinfo(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.slash_command(name="botinfo", description="Show information and statistics about the bot.")
    async def stats(self, ctx):
        global message, shard_id
        from main import start_time
        message = await ctx.respond(embed=discord.Embed(colour=0x86DB98, title="Collecting Stats...", description="This should only take a second or two!"))
        end_time = time.time()

        # Shortened this bit

        await message.edit(content=f"{emergenymessage}", embed=embed, view=view)

    @commands.Cog.listener()
    async def on_interaction(self, interaction):
        global message, shard_id
        if interaction.custom_id == "reping":
            await interaction.response.defer()

            message = await message.edit(embed=discord.Embed(colour=0x86DB98, title="Collecting Stats...", description="This should only take a second or two!"))

            # Shortened significantly

            await message.edit(content=f"{emergenymessage}", embed=embed, view=view)


def setup(bot):
    bot.add_cog(Botinfo(bot))

ERROR:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/bot.py", line 1114, in invoke_application_command
    await ctx.command.invoke(ctx)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/commands/core.py", line 375, in invoke
    await injected(ctx)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/discord/commands/core.py", line 132, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: AttributeError: 'Interaction' object has no attribute 'edit'

Solution

  • As shown here in the documentation, discord.Interaction has no edit attribute or method. You may be looking for edit_original_message().