【问题标题】:discord.py - edit the interaction message after a timeout in discord.ui.Selectdiscord.py - 在 discord.ui.Select 中超时后编辑交互消息
【发布时间】:2021-11-14 20:09:04
【问题描述】:

如何访问交互消息并对其进行编辑?

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}\U0000fe0f\U000020e3"
                )
            )
    
        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)

如果我尝试像这样编辑交互,我会得到 -> AttributeError: 'SearchMenuView' 对象没有属性'message'

60 秒后,原始消息应替换为超时中的嵌入。

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:
    view = MyView()
    view.message = await channel.send('...', view=view)
    

    之后,您可以在 on_timeout(或您无权访问interaction.message 的其他地方)中使用self.message 对其进行编辑。

    来源:https://discord.com/channels/336642139381301249/669155775700271126/860883838657495040

    【讨论】:

      猜你喜欢
      • 2021-12-29
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2022-01-17
      • 2021-03-19
      • 1970-01-01
      • 2021-07-31
      相关资源
      最近更新 更多