代码之家  ›  专栏  ›  技术社区  ›  As2Bax

discord.py-单击按钮后,如何编辑最后一条消息?(斜线命令)

  •  0
  • As2Bax  · 技术社区  · 2 年前

    我再一次。 我想做一个建议命令。当有人执行/建议命令时,你可以按确认按钮来执行。我想知道如何编辑斜杠命令上的前一条消息,而不仅仅是制作一条新消息。(临时消息)

    代码:

    #Custom View Class
    class MyView(discord.ui.View):
        def __init__(self):
            super().__init__()
    
        #setting up button (you can add multiple such)
        @discord.ui.button(label='Confirm', style=discord.ButtonStyle.green)
        async def asdf(self, interaction: discord.Interaction, button: discord.ui.Button):
            # on interaction
            await ctx.response.edit_message("whae")
    
    
    @bot.tree.command(name="suggest", description="Make a suggestion with this command.")
    async def hello(ctx: discord.Interaction, title: str, description: str):
        # add the view to a message
        embed = discord.Embed(title=f"Is this information correct?", description=f"sdfsdfsdfsd", color=discord.Colour.random())
        embed.set_thumbnail(url=f"{ctx.user.avatar.url}")
        embed.add_field(name=f"Title", value=f"{title}", inline=False)
        embed.add_field(name="Description", value=f"{description}")
        embed.set_footer(text=f"USER ID: {ctx.user.id}")
        await ctx.response.send_message(embed=embed, view=MyView(), ephemeral=True)
    

    有人知道怎么做吗?我搜索了20分钟,他们都给了我错误。谢谢:)

    1 回复  |  直到 2 年前
        1
  •  0
  •   Rexy    2 年前

    首先,让我们更清楚地了解一下这段代码:

    class View(discord.ui.View):
        def __init__(self):
            super().__init__()
    
        @discord.ui.button(label='Confirm', style=discord.ButtonStyle.green)
        async def asdf(self, interaction: discord.Interaction, button: discord.ui.Button):
            await interaction.response.edit_message(content="whae",embed=None,view=None)
    
    
    @bot.tree.command(name="suggest", description="Make a suggestion with this command.")
    async def hello(interaction: discord.Interaction, title: str, description: str):
        embed = discord.Embed(title=f"Is this information correct?", description=f"sdfsdfsdfsd", color=discord.Colour.random())
        embed.set_thumbnail(url=f"{interaction.user.avatar.url}")
        embed.add_field(name=f"Title", value=f"{title}", inline=False)
        embed.add_field(name="Description", value=f"{description}")
        embed.set_footer(text=f"USER ID: {interaction.user.id}")
        await interaction.response.send_message(embed=embed, view=View(), ephemeral=True)
    

    因此,这些变化:

    • (对我来说最重要的变化)不要把交互称为ctx,这真的很令人困惑。不一致交互就是交互,命令。上下文是ctx。不管怎样,它都有效,但看起来丑陋而不切实际。

    • 在编辑消息函数中,我对要更改的内容进行了声明

    就是这样。