单击时如何编辑按钮样式。 (Pycord) 使用 edit_message()

How to edit a buttons style when it's clicked. (Pycord) using edit_message()

所以我正在制作一个小的 discord 机器人,我想让按钮在单击时从红色变为绿色我已经尝试了几种方法来实际执行此操作,但似乎不起作用我收到错误像 2 个参数等...我没有为按钮使用 class。

在 pycord 文档中它说 view= 但是 view= 什么?

我也试过将颜色放入变量中,然后在单击按钮时重新分配变量。

示例:

@client.command()
async def test(ctx):
    test = discord.Embed(
        title="test",
        colour=discord.Colour.random()
    )
    style = red
    buttontest = discord.ui.Button(
        label="test", 
        style=discord.ButtonStyle.style, 
        disabled=False
    )

    async def se_callback(interation):

        style = green

        await interation.response.edit_message(view=style)
        # This is what I've tried but doesn't work. #

    buttontest.callback = se_callback

    view.item_add(buttontest)

    await ctx.reply(embed=sample, view=view)

这是我格式化按钮的方式

代码:

@client.command()
async def test(ctx):
    test = discord.Embed(
        title="test",
        colour=discord.Colour.random()
    )

    buttontest = discord.ui.Button(
        label="test", 
        style=discord.ButtonStyle.red, 
        disabled=False
    )

    async def se_callback(interation):

        buttontest.style = discord.ButtonStyle.green

        await interation.response.edit_message(view=buttontest.style)
        # This is what I've tried but doesn't work. #

    buttontest.callback = se_callback

    view.item_add(buttontest)

    await ctx.reply(embed=sample, view=view)

所有交互(斜杠命令除外)都需要一个视图。

你可以通过

得到这个
from discord.ui import View

然后你可以创建一个变量来保存视图。 Ex view = View()

然后您可以将按钮添加到视图中,例如 view = View(buttontest) 或您现在正在做的事情。然后你可以做 view=view

这是一个非常好的视频,解释了如何在 Pycord 中使用按钮。 https://www.youtube.com/watch?v=kNUuYEWGOxA&scrlybrkr=6facec35