#Compare buttons

1 messages · Page 1 of 1 (latest)

mellow hawk
#

How can I check if interaction.component is a button in a view?

#

I have this button and in View.interaction_check I want to check if this button was pressed

sudden verge
#

ig you could use custom id

mellow hawk
#

does it have any rules?

#

or best practices?

#

i remember someone made a 3rd party lib that helped with ids

#

making them persistent

sudden verge
#

It depends on what you have to do with the buttons, the most basic practice is to assign constant custom IDs for different buttons, buttons of the same view class will have the same IDs, other more complex uses involve the use of a prefix + some IDs to identify which user pressed the button etc (usually these custom IDs are used for persistent buttons sent as low level components)

sudden verge
mellow hawk
#

and the rest only by interaction's author

#
async def interaction_check(self, interaction: disnake.MessageInteraction) -> bool:
    if not self.message:
        self.message = interaction.message
    if (
        self.author in {None, interaction.author}
        or not self.shared
        and interaction.component in self.shared_buttons
    ):
        return True
    await interaction.response.send_message(
        _("interaction_not_allowed"), ephemeral=True
    )
    return False
#

interaction.component in self.shared_buttons is what returns False

sudden verge
#

Are your buttons all in the same view class?

mellow hawk
#

yep

#

thats a simple embed paginator

#

i want the author to be able to share and unshare some buttons

#

share as in allow other to click

#

so they press the Lock button and toggle that behavior

#

since all buttons have to have a custom id, disnake assigns them automatically right?

#

so interaction.component has a custom id, but the buttons in the view class dont

#

and ig thats why they're not considered equal

#

if there's a good convention for custom ids, let me know

#

custom_id (Optional[str]) – The ID of the button that gets received during an interaction. It is recommended not to set this parameter to prevent conflicts.

#

could you explain this part too?

sudden verge
#

sorry I was busy

empty valleyBOT
#

property custom_id```
The ID of the button that gets received during an interaction.

If this button is for a URL, it does not have a custom ID.
mellow hawk
#

but in the docs it says that all components must have an id

#

and i didnt specify anything

#

so it must be automatically generated if nothing is passed

sudden verge
#

then lemme check from source

mellow hawk
#

sadly buttons dont implement eq check

empty valleyBOT
#

disnake/ui/button.py lines 129 to 131

self._provided_custom_id = custom_id is not None
if url is None and custom_id is None:
    custom_id = os.urandom(16).hex()
mellow hawk
#

yea

sudden verge
mellow hawk
#

well its literally 1 func

#

i think i can manage such pr

sudden verge
#

btw you can do it too without an eq, assigning a custom id

mellow hawk
#

yeah did exactly that

#

gotta use first letter of func's name to not go over 100 chars lol