discord.py 2.0:无法从按钮矩阵获得交互响应

discord.py 2.0: unable to get interaction response from a matrix of buttons

我正在尝试从机器人获取按钮矩阵的单独输出。我做了一个创建矩阵的函数,然后我还没有找到与单个按钮交互的方法。

提前致谢:)

    import discord
    from discord.ext import commands
    from discord.ui import Button, View

    class MyButton(View):
    def __init__(self, label_list): #label_list is a list of string
        super().__init__()
        for i in range(len(label_list)):
            self.add_item(Button(label=label_list[i], custom_id=str(i)))
    
    #buttons callback?

    @bot.command()
    async def arraybutton(ctx, label_list):
        button = MyButton(label_list)
        await ctx.send("select:", view=button)

撇开我认为你的 label_list 是一个字符串而且我真的不明白它是如何工作的,我很确定按钮交互应该通过子类化 Button 并重新定义它们的回调来完成方法。

class MyButton(discord.ui.Button):
    async def callback(self, interaction: discord.Interaction):
        self.style = discord.ButtonStyle.red
        self.label = self.label + " was pressed by " + interaction.user.name
        await interaction.response.edit_message(view=self)
        

class MyView(View):
    def __init__(self, label_list):  # label_list is a list of string
        super().__init__()
        for i in range(len(label_list)):
            self.add_item(MyButton(label=label_list[i], custom_id=str(i)))

我一直在使用pycord documentation for Button/View UI stuff,它似乎是more-or-less与discord.py相同的代码,但我找不到discord.pyui 文档。