I have my first set of buttons on the first embed. They have the first on_button_click commands.Cog.listener() which triggers the corresponding function to display the second set of buttons you see on the second embed. I'm not sure how to go about instantiating a second on_button_click cog listener for the second set of buttons. Ideas?
#how to have a second on_button_click command listener?
1 messages · Page 1 of 1 (latest)
first set of buttons: https://paste.pythondiscord.com/sagimakeva
first set of buttons corresponding cog listener: https://paste.pythondiscord.com/hayicehece
second set of buttons: https://paste.pythondiscord.com/uzubebigus
my second set of buttons are built using a for loop to generate a button for each category in the discord. It displays correctly, but I'm not sure how to manipulate my first set of buttons corresponding cog listener to incorporate the second set of buttons, or how to have a second on_button_click cog listener.
I tried to change my first cog listener
@commands.Cog.listener()
async def on_button_click(self,inter: disnake.MessageInteraction):
button = inter.component
if button.custom_id == "CreateCat":
await self.create_category(inter)
elif button.custom_id == "CreateChan":
pass
elif button.custom_id == "CreateRole":
pass
elif button.custom_id == "CreateNote":
await self.send_dev_note(inter)
elif button.custom_id == "DeleteCat":
await self.delete_category(inter)
elif button.custom_id == "DeleteChan":
pass
elif button.custom_id == "DeleteRole":
pass
elif button.label == [cat.name for cat in inter.guild.categories]:
category_to_delete = disnake.utils.get(inter.guild.categories, name=button.label)
await category_to_delete.delete()
else:
return await inter.response.send_message("Invalid Operation: Contact Developers", delete_after=30)
```but it doesn't like that.
on_button_click listeners listen for ALL button clicks. The idea is to make sure that you're only handling the ones pertaining to your bot and the ones you want. You can extend your current function, or just add another listener that handles a different set of buttons. Just gotta make sure that each listener function is only listening and executing code when you want it to, otherwise it needs to just return without doing anything
ok so how can I get a second on_button_click to work correctly?
Just add a second one like you did the first one. And check custom_id, if the custom_id isn't one that you want, return. Otherwise, execute code based on the different custom_id for each button
oh ok. ty