discord.py – edit the interaction message after a timeout in discord.ui.Select

  discord, discord.py, python, python-3.x

How can I access the interaction message and edit it?

discord.ui.Select

class SearchMenu(discord.ui.Select):

    def __init__(self, ctx, bot, data):
        self.ctx = ctx
        self.bot = bot
        self.data = data
        self.player = Player

        values = []
        for index, track in enumerate(self.data[:9]):
            values.append(
                discord.SelectOption(
                    label=track.title, 
                    value=index + 1, 
                    description=track.author, 
                    emoji=f"{index + 1}U0000fe0fU000020e3"
                )
            )
    
        values.append(discord.SelectOption(label='Cancel', description='Exit the search menu.', emoji="🔒"))
        super().__init__(placeholder='Click on the Dropdown.', min_values=1, max_values=1, options=values)

    async def callback(self, interaction: discord.Interaction):

        if self.values[0] == "Cancel":
            
            embed = Embed(emoji=self.ctx.emoji.whitecheck, description="This interaction has been deleted.")
            return await interaction.message.edit(embed=embed, view=None)

discord.ui.View

class SearchMenuView(discord.ui.View):

    def __init__(self, options, ctx, bot):
        super().__init__(timeout=60.0)
        self.ctx = ctx

        self.add_item(SearchMenu(ctx, bot, options))

    async def interaction_check(self, interaction: discord.Interaction):
        if interaction.user != self.ctx.author:
            embed = Embed(description=f"Sorry, but this interaction can only be used by {self.ctx.author.name}.")
            await interaction.response.send_message(embed=embed, ephemeral=True)
            return False
        else:
            return True

    async def on_timeout(self):

        embed = Embed(emoji=self.ctx.emoji.whitecross, description="Interaction has timed out. Please try again.")
        await self.message.edit(embed=embed, view=None)

If I try to edit the interaction like this I am getting
-> AttributeError: ‘SearchMenuView’ object has no attribute ‘message’

After 60 seconds the original message should be replaced with the embed in the timeout.

Source: Python Questions

LEAVE A COMMENT