#Add callback to button that exists conditionally

1 messages · Page 1 of 1 (latest)

fleet otter
#

I have a complex View where a variety of Buttons and Selects may or may not exist. I want to bind a function as the callback to a button at the same time as it's created using View.add_item().

I couldn't find any way to do this in the API or documentation but I'm sure somebody has figured this out before.

fair wasp
#

Check condition, add component, or don't add component?

hot beacon
#

A shot in the dark as I haven't done this, but could a listener that checks for interaction of the custom id of said button/selects that may or mat not exist work?

crude pumice
#

Uh, why not?

fleet otter
#

My solution was to create a modified Button object which implements this feature;

class CallbackButton(disnake.ui.Button):
  def __init__(self, callback:Callable[[disnake.MessageInteraction], None], **kwargs) -> None:
    super().__init__(**kwargs)
    self.callback = callback
crude pumice
#

Wouldn't you want the callback to be asynchronous though

fleet otter
#

This implementation seems to work just fine with async functions

magic pivot
#

they were mostly referring to the fact that your callback typehint is not asynchronous