Custom Commands homeCustom Commands
  • Blog
  • Privacy
  • Premium
  • Support
Back to blog
// blog

How to create views in custom commands bot

Lets learn how view works

Md Shahriyar Alam

2 years ago

·1 min read

View is a collection of multiple components that work togather, lets learn how we can create a view in custom commands bot

Create a view

py
class AgreeView(View):
  def __init__(self):
    self.agree = False # initialize agree with false
  
  @button(label="Yes", style="success")
  async def yes(self, interaction, button):
    await defer(interaction, action="update")
    respond_interaction(interaction, empty=True)

    self.agree = True # sets agree = True because yes button clicked
    self.stop() # stops the view when this button is pressed
    

  @button(label="No", style="danger")
  async def stopper(self, interaction, button):
    await defer(interaction, action="update")
    respond_interaction(interaction, components=None, content="Cancelled")

    self.stop() # stops the view when this button is pressed

view = CustomView()
respond_interaction(interaction, content="Do you agree?", components=view) # send the view

exit_code = await view.wait(scope=scope)  # wait untill the view is stopped

if exit_code == "timeout": # if the view times out
  set_error("Timeout")

After that you can access view.agree anywhere and execute code based on conditions

Loading comments…

{}

Need help?

Have a question, a suggestion, or stuck on something? Reach out — we're happy to help.

DiscordJoin support server
Custom CommandsCustom Commands

The #1 custom commands Discord bot — build commands, events and databases with zero boilerplate.

Links

HomeBlogPrivacySupport

Contact

[email protected]

2 Frederick StreetLondon, WC1X 0ND

{ / } custom commands

© WEiRDSOFT LTD. All rights reserved.