编辑按钮类中的特定按钮。

huangapple go评论96阅读模式
英文:

Editing specific button in Button class

问题

在discord.py中,我已经创建了一个包含5个按钮的类,并且我希望当用户与特定按钮互动时,其中一个按钮能够更改/编辑,而无需对其他4个按钮进行任何更改。

例如:
如果我有5个按钮分别是one、two、Play、three和four。
当有人与该按钮互动时,"On" 应该变成 "Stop"。

英文:

In discord.py, I've made a class of 5 buttons and I want one of those buttons to change/edit whenever the user interacted with that specific button without having to make any changes to the other 4 buttons.

For example:
If i have 5 buttons one, two, Play, three, four respectively.
On should become Stop when someone interacted with that button

答案1

得分: 2

以下是您要翻译的内容:

如何禁用按钮

discord.ui.Button() 类有一个 disabled 参数。当设置为 True 时,这会禁用按钮。当视图被编辑时,消息应该会更新。

如何更改按钮的标签

discord.ui.Button() 类有一个 label 参数。它可以在实例化时设置为字符串,并在按下时更改。

代码示例

class disable_buttons(discord.ui.View):  # 视图对象
    def __init__(self):
        super().__init__()
        for i in range(5):  # 添加五个按钮
            self.add_item(self.button_disable(str(i)))

    class button_disable(discord.ui.Button):  # 按钮类
        def __init__(self, label):
            super().__init__(label=label)  # 设置标签和超级初始化类

        async def callback(self, interaction: discord.Interaction):
            self.disabled = True  # 禁用按钮  
            await interaction.message.edit(view=self.view)  # 更新消息
            await interaction.response.send_message("成功!")  # 响应

@tree.command(name="button_test", description="测试按钮", guild=discord.Object(
    id=insertguildidhere))
async def button_test(interaction):  # 用于向响应添加按钮的命令
    await interaction.response.send_message("成功!", view=disable_buttons())  
    # disable_buttons 是要添加到消息的视图类

注意:如果要更改 label 参数,您可以执行相同的操作,但要编辑回调函数以更改 self.label

输出

编辑按钮类中的特定按钮。

资源

英文:

How to disable a button

The discord.ui.Button() class has a disabled parameter. When set to True this disables the button. When the view is edited, the message should be updated.

How to change the label of a button

The The discord.ui.Button() class has a label parameter. It can be set to a string when instanced and changed when pressed.

Example Code

class disable_buttons(discord.ui.View):  # View object
    def __init__(self):
        super().__init__()
        for i in range(5):  # Add five buttons
            self.add_item(self.button_disable(str(i)))

    class button_disable(discord.ui.Button):  # Button class
        def __init__(self, label):
            super().__init__(label=label)  # set label and super init class

        async def callback(self, interaction: discord.Interaction):
            self.disabled = True  # disable button  
            await interaction.message.edit(view=self.view)  # update message
            await interaction.response.send_message("Worked!")  # respond

@tree.command(name="button_test", description="Test buttons", guild=discord.Object(
    id=insertguildidhere))
async def button_test(interaction):  # command to add buttons to response
    await interaction.response.send_message("Worked!", view=disable_buttons())  
    # disable_buttons is the view class to add to the message

note: if you want to change the label parameter you would do the same thing but edit the callback function to change the self.label

Output

编辑按钮类中的特定按钮。

Resources

huangapple
  • 本文由 发表于 2023年5月22日 09:19:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定