Skip to main content
Views are containers that hold interactive UI components like buttons and select menus.

View

The main class for creating interactive message components.
from discord.ui import View

class MyView(View):
    def __init__(self):
        super().__init__(timeout=180.0)

Constructor

*items
ViewItem
Initial items attached to this view.
timeout
float | None
default:"180.0"
Timeout in seconds from last interaction before no longer accepting input. If None, there is no timeout.
disable_on_timeout
bool
default:"False"
Whether to disable the view when the timeout is reached.
store
bool
default:"True"
Whether this view should be stored for callback listening. Setting to False ignores item callbacks and prevents value refreshing.

Attributes

timeout
float | None
Timeout from last interaction with the UI before no longer accepting input.
children
List[ViewItem]
The list of children attached to this view.
disable_on_timeout
bool
Whether to disable the view when the timeout is reached.
message
Message | None
The message that this view is attached to. None if the view has not been sent.
parent
Interaction | None
The parent interaction which this view was sent from. None if not sent using InteractionResponse.send_message.

Methods

Adds an item to the view.Parameters:
  • item (ViewItem): The item to add to the view.
Raises:
  • TypeError: An ViewItem was not passed.
  • ValueError: Maximum number of children exceeded (25) or row is full.
Returns: Self
button = Button(label="Click Me")
view.add_item(button)
Removes an item from the view.Parameters:
  • item (ViewItem | int | str): The item, item id, or item custom_id to remove.
Returns: Self
view.remove_item(button)
view.remove_item("custom_button_id")
Removes all items from the view.Returns: Self
view.clear_items()
A callback that checks whether the view should process item callbacks for an interaction.Parameters:
  • interaction (Interaction): The interaction that occurred.
Returns: bool - Whether the view children’s callbacks should be called.
async def interaction_check(self, interaction: discord.Interaction) -> bool:
    return interaction.user.id == self.author_id
A callback called when a view’s timeout elapses without being explicitly stopped.
async def on_timeout(self) -> None:
    self.disable_all_items()
    await self.message.edit(view=self)
A callback called when interaction_check returns False.Parameters:
  • interaction (Interaction): The interaction that occurred.
async def on_check_failure(self, interaction: discord.Interaction) -> None:
    await interaction.response.send_message("You cannot use this!", ephemeral=True)
A callback called when an item’s callback or interaction_check fails with an error.Parameters:
  • error (Exception): The exception that was raised.
  • item (ViewItem): The item that failed the dispatch.
  • interaction (Interaction): The interaction that led to the failure.
async def on_error(self, error: Exception, item: ViewItem, interaction: discord.Interaction) -> None:
    await interaction.response.send_message("An error occurred!", ephemeral=True)
Disables all buttons and select menus in the view.Parameters:
  • exclusions (List[ViewItem] | None): Items to not disable from the view.
Returns: Self
view.disable_all_items()
await message.edit(view=view)
Enables all buttons and select menus in the view.Parameters:
  • exclusions (List[ViewItem] | None): Items to not enable from the view.
Returns: Self
view.enable_all_items()
Stops listening to interaction events from this view. This operation cannot be undone.
view.stop()
Waits until the view has finished interacting.Returns: bool - True if the view timed out, False if finished normally.
timed_out = await view.wait()
Whether the view has finished interacting.Returns: bool
if view.is_finished():
    print("View is done")
Whether the view is set up as persistent. A persistent view has all components with a set custom_id and timeout set to None.Returns: bool
if view.is_persistent():
    print("This view will persist across bot restarts")
Converts a message’s components into a View.Parameters:
  • message (Message): The message with components to convert.
  • timeout (float | None): The timeout of the converted view. Defaults to 180.0.
Returns: View
view = View.from_message(message, timeout=300.0)

Item

Base class for all UI components.

Attributes

type
ComponentType
The underlying component’s type.
id
int | None
The item’s ID. Set by the user or automatically by Discord.

Methods

Whether the item can dispatch interactions.Returns: bool
Whether the item is persistent (has a custom_id set).Returns: bool

ViewItem

Base class for items used in Views.

Attributes

view
View | None
The parent view associated with this item.
width
int
The width of the item in the UI layout (1-5).

Methods

The callback associated with this UI item. Override in subclasses.Parameters:
  • interaction (Interaction): The interaction that triggered this item.
async def callback(self, interaction: discord.Interaction):
    await interaction.response.send_message("Button clicked!")

Example

import discord
from discord.ui import View, Button

class MyView(View):
    def __init__(self):
        super().__init__(timeout=60.0)
        
    @discord.ui.button(label="Click Me", style=discord.ButtonStyle.primary)
    async def button_callback(self, button: Button, interaction: discord.Interaction):
        await interaction.response.send_message("Button clicked!", ephemeral=True)

# Usage
view = MyView()
await ctx.send("Click the button!", view=view)

Build docs developers (and LLMs) love