Create a callable button inside a view class discord.py

1k Views Asked by At

I want to create a callable button from a personalised button class.

I have seen on this video that you can call button such as:

@discord.ui.button(label="Hola")
    # Callback function that disable the button and reply to the user
    async def button_callback(self, button, interaction):
        button.disabled = True
        await interaction.response.edit_message(view=self)
        await interaction.followup.send('Hello!')

I'm trying to make something like this:

class MyView(View):
    @GreenButtons('Hello There!')
    async def button_callback(self, button, interaction):
        button.disabled = True
        await interaction.response.edit_message(view=self)
        await interaction.followup.send('General Kenobi!')

I've been thinking about adding a __call__ function, but it didn't work out quite well.

class GreenButtons(Button):
    def __init__(self, label):
        super().__init__(label=label, style=discord.ButtonStyle.green)

    def __call__(self, label):
        GreenButtons(label)

    async def callback(self, interaction):
        self.disabled = True
        await interaction.response.send_message(f"{self.label} clicked!")

In fact, when I did start the program, it was only showing 'Testing' without buttons :

async def test_view(self, ctx)
    my_view = XeMyView()
    await ctx.respond("Testing", view=my_view)

I'm not sure what I did wrong here. So, how can I make it work without creating every buttons inside every view class I'll have to create ?

0

There are 0 best solutions below