Bot UI Kit¶
The library has helpers to help create component-based UIs.
Shortcut decorators¶
- @discord.ui.button(*, label=None, custom_id=None, disabled=False, style=('secondary', 2), emoji=None, row=None, id=None)[source]¶
A decorator that attaches a button to a component.
The function being decorated should have three parameters,
selfrepresenting thediscord.ui.View,discord.ui.ActionRowordiscord.ui.Section, thediscord.ui.Buttonbeing pressed, and thediscord.Interactionyou receive.Note
Premium and link buttons cannot be created with this decorator. Consider creating a
Buttonobject manually instead. These types of buttons do not have a callback associated since Discord doesn’t handle them when clicked.- Parameters:
label (Optional[
str]) – The label of the button, if any.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.style (
ButtonStyle) – The style of the button. Defaults toButtonStyle.grey.disabled (
bool) – Whether the button is disabled or not. Defaults toFalse.emoji (Optional[Union[
str,GuildEmoji,AppEmoji,PartialEmoji]]) – The emoji of the button. This can be in string form or aPartialEmojior a fullGuildEmojiorAppEmoji.row (Optional[
int]) –The relative row this button belongs to. A Discord component can only have 5 rows. By default, items are arranged automatically into those 5 rows. If you’d like to control the relative positioning of the row then passing an index is advised. For example, row=1 will show up before row=2. Defaults to
None, which is automatic ordering. The row number must be between 0 and 4 (i.e. zero indexed).Warning
This parameter does not work in
ActionRow.
- Return type:
Callable[[Callable[[Any,Button[TypeVar(V, bound= BaseView, covariant=True)],Interaction],Coroutine[Any,Any,Any]]],Button[TypeVar(V, bound= BaseView, covariant=True)]]
- @discord.ui.select(select_type=('string_select', 3), *, placeholder=None, custom_id=None, min_values=1, max_values=1, options=..., channel_types=..., disabled=False, row=None, id=None, default_values=None)[source]¶
A decorator that attaches a select menu to a component.
The function being decorated should have three parameters,
selfrepresenting thediscord.ui.View,discord.ui.ActionRowordiscord.ui.Section, thediscord.ui.Selectbeing pressed and thediscord.Interactionyou receive.In order to get the selected items that the user has chosen within the callback use
Select.values.Changed in version 2.3: Creating select menus of different types is now supported.
- Parameters:
select_type (
discord.ComponentType) – The type of select to create. Must be one ofdiscord.ComponentType.string_select,discord.ComponentType.user_select,discord.ComponentType.role_select,discord.ComponentType.mentionable_select, ordiscord.ComponentType.channel_select.placeholder (Optional[
str]) – The placeholder text that is shown if nothing is selected, if any.custom_id (
str) – The ID of the select menu that gets received during an interaction. It is recommended not to set this parameter to prevent conflicts.row (Optional[
int]) – The relative row this select menu belongs to. A Discord component can only have 5 rows. By default, items are arranged automatically into those 5 rows. If you’d like to control the relative positioning of the row then passing an index is advised. For example, row=1 will show up before row=2. Defaults toNone, which is automatic ordering. The row number must be between 0 and 4 (i.e. zero indexed).min_values (
int) – The minimum number of items that must be chosen for this select menu. Defaults to 1 and must be between 0 and 25.max_values (
int) – The maximum number of items that must be chosen for this select menu. Defaults to 1 and must be between 1 and 25.options (List[
discord.SelectOption]) – A list of options that can be selected in this menu. Only valid for thediscord.ComponentType.string_selecttype.channel_types (List[
discord.ChannelType]) – The channel types that should be selectable. Only valid for thediscord.ComponentType.channel_selecttype. Defaults to all channel types.disabled (
bool) – Whether the select is disabled or not. Defaults toFalse.id (Optional[
int]) – The select menu’s ID.default_values (Optional[Sequence[Union[
discord.SelectDefaultValue,discord.abc.Snowflake]]]) –The default values of this select. Only applicable if
select_typeis notdiscord.ComponentType.string_select.This can be either
discord.SelectDefaultValueinstances or models, which will be converted intodiscord.SelectDefaultValueinstances.Below, is a table defining the model instance type and the default value type it will be mapped:
Model Type
Default Value Type
depending on
discord.Object.type, it will be mapped to any aboveIf you pass a model that is not defined in the table,
TypeErrorwill be raised.Note
The
discord.abc.GuildChannelprotocol includesdiscord.TextChannel,discord.VoiceChannel,discord.StageChannel,discord.ForumChannel,discord.Thread,discord.MediaChannel. This list is not exhaustive, and is bound to change based of the new channel types Discord adds.Added in version 2.7.
- Return type:
Callable[[Callable[[Any,Select[TypeVar(V, bound= BaseView, covariant=True),TypeVar(ST, bound= Snowflake | str, covariant=True),Any],Interaction],Coroutine[Any,Any,Any]]],Select[TypeVar(V, bound= BaseView, covariant=True),TypeVar(ST, bound= Snowflake | str, covariant=True),Any]]
- @discord.ui.string_select(*, placeholder=None, custom_id=None, min_values=1, max_values=1, options=..., disabled=False, row=None, id=None)[source]¶
A shortcut for
discord.ui.select()with select typediscord.ComponentType.string_select.Added in version 2.3.
- @discord.ui.user_select(*, placeholder=None, custom_id=None, min_values=1, max_values=1, disabled=False, row=None, id=None, default_values=None)[source]¶
A shortcut for
discord.ui.select()with select typediscord.ComponentType.user_select.Added in version 2.3.
- @discord.ui.role_select(*, placeholder=None, custom_id=None, min_values=1, max_values=1, disabled=False, row=None, id=None, default_values=None)[source]¶
A shortcut for
discord.ui.select()with select typediscord.ComponentType.role_select.Added in version 2.3.
- @discord.ui.mentionable_select(*, placeholder=None, custom_id=None, min_values=1, max_values=1, disabled=False, row=None, id=None, default_values=None)[source]¶
A shortcut for
discord.ui.select()with select typediscord.ComponentType.mentionable_select.Added in version 2.3.
- @discord.ui.channel_select(*, placeholder=None, custom_id=None, min_values=1, max_values=1, disabled=False, channel_types=..., row=None, id=None, default_values=None)[source]¶
A shortcut for
discord.ui.select()with select typediscord.ComponentType.channel_select.Added in version 2.3.
Objects¶
- defadd_item
- defclear_items
- defcopy_text
- defdisable_all_items
- defenable_all_items
- asyncinteraction_check
- defis_components_v2
- defis_dispatching
- defis_finished
- defis_persistent
- asyncon_check_failure
- asyncon_error
- asyncon_timeout
- defremove_item
- defstop
- asyncwait
- class discord.ui.BaseView(*items, timeout=180.0, disable_on_timeout=False, store=True)[source]¶
The base class for UI views used in messages.
Added in version 2.7.
- Parameters:
- add_item(item)[source]¶
Adds an item to the view.
- Parameters:
item (
ViewItem) – The item to add to the view.- Raises:
ValueError – Maximum number of children has been exceeded
- Return type:
Self
- remove_item(item)[source]¶
Removes an item from the view. If an
intorstris passed, the item will be removed by ViewItemidorcustom_idrespectively.
- await interaction_check(interaction)[source]¶
This function is a coroutine.
A callback that is called when an interaction happens within the view that checks whether the view should process item callbacks for the interaction.
This is useful to override if, for example, you want to ensure that the interaction author is a given user.
The default implementation of this returns
True.If this returns
False,on_check_failure()is called.Note
If an exception occurs within the body then the check is considered a failure and
on_error()is called.- Parameters:
interaction (
Interaction) – The interaction that occurred.- Returns:
Whether the view children’s callbacks should be called.
- Return type:
- await on_timeout()[source]¶
This function is a coroutine.
A callback that is called when a view’s timeout elapses without being explicitly stopped.
- Return type:
- await on_check_failure(interaction)[source]¶
This function is a coroutine. A callback that is called when a
BaseView.interaction_check()returnsFalse. This can be used to send a response when a check failure occurs.- Parameters:
interaction (
Interaction) – The interaction that occurred.- Return type:
- await on_error(error, item, interaction)[source]¶
This function is a coroutine.
A callback that is called when an item’s callback or
interaction_check()fails with an error.The default implementation prints the traceback to stderr.
- 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.
- Return type:
- is_components_v2()[source]¶
Whether the view contains V2 components.
A view containing V2 components cannot be sent alongside message content or embeds.
- Return type:
- is_persistent()[source]¶
Whether the view is set up as persistent.
A persistent view has all their components with a set
custom_idand atimeoutset toNone.- Return type:
- stop()[source]¶
Stops listening to interaction events from this view.
This operation cannot be undone.
- Return type:
- await wait()[source]¶
Waits until the view has finished interacting.
A view is considered finished when
stop()is called, or it times out.- Returns:
If
True, then the view timed out. IfFalsethen the view finished normally.- Return type:
- disable_all_items(*, exclusions=None)[source]¶
Disables all buttons and select menus in the view.
- Parameters:
exclusions (Optional[List[
ViewItem]]) – A list of items in self.children to not disable from the view.- Return type:
Self
- enable_all_items(*, exclusions=None)[source]¶
Enables all buttons and select menus in the view.
- Parameters:
exclusions (Optional[List[
ViewItem]]) – A list of items in self.children to not enable from the view.- Return type:
Self
- copy_text()[source]¶
Returns the text of all
TextDisplayitems in this View. Equivalent to the Copy Text option on Discord clients.- Return type:
- clsView.from_dict
- clsView.from_message
- defadd_item
- defclear_items
- defcopy_text
- defdisable_all_items
- defenable_all_items
- defget_item
- asyncinteraction_check
- defis_components_v2
- defis_dispatching
- defis_finished
- defis_persistent
- asyncon_check_failure
- asyncon_error
- asyncon_timeout
- defremove_item
- defstop
- asyncwait
- class discord.ui.View(*items, timeout=180.0, disable_on_timeout=False, store=True)[source]¶
Represents a legacy UI view for V1 components
ButtonandSelect.This object must be inherited to create a UI within Discord.
Added in version 2.0.
Changed in version 2.7: Now inherits from
BaseView- Parameters:
*items (
ViewItem) – The initial items attached to this view.timeout (Optional[
float]) – Timeout in seconds from last interaction with the UI before no longer accepting input. Defaults to 180.0. IfNonethen there is no timeout.store (Optional[
bool]) – Whether this view should be stored for callback listening. Setting it toFalsewill ignore item callbacks and prevent their values from being refreshed. Defaults toTrue.
- timeout¶
Timeout from last interaction with the UI before no longer accepting input. If
Nonethen there is no timeout.- Type:
Optional[
float]
- disable_on_timeout¶
Whether to disable the view when the timeout is reached. Defaults to
False.- Type:
- message¶
The message that this view is attached to. If
Nonethen the view has not been sent with a message.- Type:
Optional[
Message]
- parent¶
The parent interaction which this view was sent from. If
Nonethen the view was not sent usingInteractionResponse.send_message().- Type:
Optional[
Interaction]
- Parameters:
disable_on_timeout (
bool)
- classmethod from_message(message, /, *, timeout=180.0)[source]¶
Converts a message’s components into a
View.The
Message.componentsof a message are read-only and separate types from those in thediscord.uinamespace. In order to modify and edit message components they must be converted into aViewfirst.
- classmethod from_dict(data, /, *, timeout=180.0)[source]¶
Converts a list of component dicts into a
View.
- add_item(item)[source]¶
Adds an item to the view. Attempting to add a
ActionRowwill add its children instead.- Parameters:
item (
ViewItem) – The item to add to the view.- Raises:
ValueError – Maximum number of children has been exceeded (25) or the row the item is trying to be added to is full.
- Return type:
Self
- remove_item(item)[source]¶
Removes an item from the view. If an
intorstris passed, the item will be removed by Itemidorcustom_idrespectively.
- is_components_v2()[source]¶
Whether the view contains V2 components.
A view containing V2 components cannot be sent alongside message content or embeds.
This is always
FalseforView.- Return type:
- copy_text()¶
Returns the text of all
TextDisplayitems in this View. Equivalent to the Copy Text option on Discord clients.- Return type:
- disable_all_items(*, exclusions=None)¶
Disables all buttons and select menus in the view.
- Parameters:
exclusions (Optional[List[
ViewItem]]) – A list of items in self.children to not disable from the view.- Return type:
Self
- enable_all_items(*, exclusions=None)¶
Enables all buttons and select menus in the view.
- Parameters:
exclusions (Optional[List[
ViewItem]]) – A list of items in self.children to not enable from the view.- Return type:
Self
- get_item(custom_id)¶
Gets an item from this structure. Roughly equal to utils.get(self.children, …). If an
intis provided, the item will be retrieved byid, otherwise bycustom_id. This method will also search nested items.
- await interaction_check(interaction)¶
This function is a coroutine.
A callback that is called when an interaction happens within the view that checks whether the view should process item callbacks for the interaction.
This is useful to override if, for example, you want to ensure that the interaction author is a given user.
The default implementation of this returns
True.If this returns
False,on_check_failure()is called.Note
If an exception occurs within the body then the check is considered a failure and
on_error()is called.- Parameters:
interaction (
Interaction) – The interaction that occurred.- Returns:
Whether the view children’s callbacks should be called.
- Return type:
- is_persistent()¶
Whether the view is set up as persistent.
A persistent view has all their components with a set
custom_idand atimeoutset toNone.- Return type:
- await on_check_failure(interaction)¶
This function is a coroutine. A callback that is called when a
BaseView.interaction_check()returnsFalse. This can be used to send a response when a check failure occurs.- Parameters:
interaction (
Interaction) – The interaction that occurred.- Return type:
- await on_error(error, item, interaction)¶
This function is a coroutine.
A callback that is called when an item’s callback or
interaction_check()fails with an error.The default implementation prints the traceback to stderr.
- 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.
- Return type:
- await on_timeout()¶
This function is a coroutine.
A callback that is called when a view’s timeout elapses without being explicitly stopped.
- Return type:
- stop()¶
Stops listening to interaction events from this view.
This operation cannot be undone.
- Return type:
- clsDesignerView.from_dict
- clsDesignerView.from_message
- defadd_item
- defclear_items
- defcopy_text
- defdisable_all_items
- defenable_all_items
- defget_item
- asyncinteraction_check
- defis_components_v2
- defis_dispatching
- defis_finished
- defis_persistent
- asyncon_check_failure
- asyncon_error
- asyncon_timeout
- defremove_item
- defstop
- asyncwait
- class discord.ui.DesignerView(*items, timeout=180.0, disable_on_timeout=False, store=True)[source]¶
Represents a UI view compatible with v2 components.
This object must be inherited to create a UI within Discord.
Added in version 2.7.
- Parameters:
*items (
ViewItem) – The initial items attached to this view.timeout (Optional[
float]) – Timeout in seconds from last interaction with the UI before no longer accepting input. Defaults to 180.0. IfNonethen there is no timeout.store (Optional[
bool]) – Whether this view should be stored for callback listening. Setting it toFalsewill ignore item callbacks and prevent their values from being refreshed. Defaults toTrue.
- timeout¶
Timeout from last interaction with the UI before no longer accepting input. If
Nonethen there is no timeout.- Type:
Optional[
float]
- disable_on_timeout¶
Whether to disable the view’s items when the timeout is reached. Defaults to
False.- Type:
- message¶
The message that this view is attached to. If
Nonethen the view has not been sent with a message.- Type:
Optional[
Message]
- parent¶
The parent interaction which this view was sent from. If
Nonethen the view was not sent usingInteractionResponse.send_message().- Type:
Optional[
Interaction]
- Parameters:
disable_on_timeout (
bool)
- classmethod from_message(message, /, *, timeout=180.0)[source]¶
Converts a message’s components into a
DesignerView.The
Message.componentsof a message are read-only and separate types from those in thediscord.uinamespace. In order to modify and edit message components they must be converted into aViewfirst.
- classmethod from_dict(data, /, *, timeout=180.0)[source]¶
Converts a list of component dicts into a
DesignerView.- Parameters:
- Returns:
The converted view. This always returns a
Viewand not one of its subclasses.- Return type:
- add_item(item)[source]¶
Adds an item to the view.
- Parameters:
item (
ViewItem) – The item to add to the view.- Raises:
ValueError – Maximum number of items has been exceeded (40)
- Return type:
Self
- is_components_v2()[source]¶
Whether the view contains V2 components.
A view containing V2 components cannot be sent alongside message content or embeds.
- Return type:
- clear_items()¶
Removes all items from this view.
- Return type:
Self
- copy_text()¶
Returns the text of all
TextDisplayitems in this View. Equivalent to the Copy Text option on Discord clients.- Return type:
- disable_all_items(*, exclusions=None)¶
Disables all buttons and select menus in the view.
- Parameters:
exclusions (Optional[List[
ViewItem]]) – A list of items in self.children to not disable from the view.- Return type:
Self
- enable_all_items(*, exclusions=None)¶
Enables all buttons and select menus in the view.
- Parameters:
exclusions (Optional[List[
ViewItem]]) – A list of items in self.children to not enable from the view.- Return type:
Self
- get_item(custom_id)¶
Gets an item from this structure. Roughly equal to utils.get(self.children, …). If an
intis provided, the item will be retrieved byid, otherwise bycustom_id. This method will also search nested items.
- await interaction_check(interaction)¶
This function is a coroutine.
A callback that is called when an interaction happens within the view that checks whether the view should process item callbacks for the interaction.
This is useful to override if, for example, you want to ensure that the interaction author is a given user.
The default implementation of this returns
True.If this returns
False,on_check_failure()is called.Note
If an exception occurs within the body then the check is considered a failure and
on_error()is called.- Parameters:
interaction (
Interaction) – The interaction that occurred.- Returns:
Whether the view children’s callbacks should be called.
- Return type:
- is_persistent()¶
Whether the view is set up as persistent.
A persistent view has all their components with a set
custom_idand atimeoutset toNone.- Return type:
- await on_check_failure(interaction)¶
This function is a coroutine. A callback that is called when a
BaseView.interaction_check()returnsFalse. This can be used to send a response when a check failure occurs.- Parameters:
interaction (
Interaction) – The interaction that occurred.- Return type:
- await on_error(error, item, interaction)¶
This function is a coroutine.
A callback that is called when an item’s callback or
interaction_check()fails with an error.The default implementation prints the traceback to stderr.
- 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.
- Return type:
- await on_timeout()¶
This function is a coroutine.
A callback that is called when a view’s timeout elapses without being explicitly stopped.
- Return type:
- remove_item(item)¶
Removes an item from the view. If an
intorstris passed, the item will be removed by ViewItemidorcustom_idrespectively.
- stop()¶
Stops listening to interaction events from this view.
This operation cannot be undone.
- Return type:
- class discord.ui.Item[source]¶
Represents the base UI item that all UI components inherit from.
Added in version 2.0.
- class discord.ui.ViewItem[source]¶
Represents an item used in Views.
The following are the original items supported in
discord.ui.View:And the following are new items under the “Components V2” specification for use in
discord.ui.DesignerView:Additionally,
discord.ui.ActionRowshould be used indiscord.ui.DesignerViewto supportdiscord.ui.Buttonanddiscord.ui.Select.Added in version 2.7.
- property view: V | None¶
Gets the parent view associated with this item.
The view refers to the structure that holds this item. This is typically set automatically when the item is added to a view.
- Returns:
The parent view of this item, or
Noneif the item is not attached to any view.- Return type:
Optional[
BaseView]
- await callback(interaction)[source]¶
This function is a coroutine.
The callback associated with this UI item.
This can be overridden by subclasses.
- Parameters:
interaction (
Interaction) – The interaction that triggered this UI item.
- class discord.ui.ModalItem[source]¶
Represents an item used in Modals.
discord.ui.InputTextis the original item supported indiscord.ui.Modal.The following are newly available in
discord.ui.DesignerModal:And
discord.ui.Labelshould be used indiscord.ui.DesignerModalto support the following items: -discord.ui.InputText-discord.ui.Select-discord.ui.FileUploadAdded in version 2.7.
- defadd_button
- defadd_item
- defadd_select
- defdisable_all_items
- defenable_all_items
- defget_item
- defremove_item
- class discord.ui.ActionRow(*items, id=None)[source]¶
Represents a UI Action Row used in
discord.ui.DesignerView.The items supported are as follows:
Added in version 2.7.
- Parameters:
- remove_item(item)[source]¶
Removes an item from the action row. If an int or str is passed, it will remove by Item
idorcustom_idrespectively.
- get_item(id)[source]¶
Get an item from this action row. Roughly equivalent to utils.get(row.children, …). If an
intis provided, the item will be retrieved byid, otherwise bycustom_id.
- add_button(*, style=('secondary', 2), label=None, disabled=False, custom_id=None, url=None, emoji=None, sku_id=None, id=None)[source]¶
Adds a
Buttonto the action row.To append a pre-existing
Button, use theadd_item()method instead.- Parameters:
style (
discord.ButtonStyle) – The style of the button.custom_id (Optional[
str]) – The custom ID of the button that gets received during an interaction. If this button is for a URL, it does not have a custom ID.url (Optional[
str]) – The URL this button sends you to.disabled (
bool) – Whether the button is disabled or not.label (Optional[
str]) – The label of the button, if any. Maximum of 80 chars.emoji (Optional[Union[
PartialEmoji,GuildEmoji,AppEmoji,str]]) – The emoji of the button, if any.sku_id (Optional[Union[
int]]) – The ID of the SKU this button refers to.id (Optional[
int]) – The button’s ID.
- Return type:
Self
- add_select(select_type=('string_select', 3), *, custom_id=None, placeholder=None, min_values=1, max_values=1, options=None, channel_types=None, disabled=False, id=None, default_values=None)[source]¶
Adds a
Selectto the action row.To append a pre-existing
Select, use theadd_item()method instead.- Parameters:
select_type (
discord.ComponentType) – The type of select to create. Must be one ofdiscord.ComponentType.string_select,discord.ComponentType.user_select,discord.ComponentType.role_select,discord.ComponentType.mentionable_select, ordiscord.ComponentType.channel_select.custom_id (
str) – The custom ID of the select menu that gets received during an interaction. If not given then one is generated for you.placeholder (Optional[
str]) – The placeholder text that is shown if nothing is selected, if any.min_values (
int) – The minimum number of items that must be chosen for this select menu. Defaults to 1 and must be between 1 and 25.max_values (
int) – The maximum number of items that must be chosen for this select menu. Defaults to 1 and must be between 1 and 25.options (List[
discord.SelectOption]) – A list of options that can be selected in this menu. Only valid for selects of typediscord.ComponentType.string_select.channel_types (List[
discord.ChannelType]) – A list of channel types that can be selected in this menu. Only valid for selects of typediscord.ComponentType.channel_select.disabled (
bool) – Whether the select is disabled or not. Defaults toFalse.id (Optional[
int]) – The select menu’s ID.default_values (Optional[Sequence[Union[
discord.SelectDefaultValue,discord.abc.Snowflake]]]) –The default values of this select. Only applicable if
select_typeis notdiscord.ComponentType.string_select.These can be either
discord.SelectDefaultValueinstances or models, which will be converted intodiscord.SelectDefaultValueinstances.
- Return type:
Self
- disable_all_items(*, exclusions=None)[source]¶
Disables all items in the row.
- Parameters:
exclusions (Optional[List[
ViewItem]]) – A list of items in self.children to not disable.- Return type:
Self
- enable_all_items(*, exclusions=None)[source]¶
Enables all items in the row.
- Parameters:
exclusions (Optional[List[
ViewItem]]) – A list of items in self.children to not enable.- Return type:
Self
- property width¶
Return the sum of the items’ widths.
- property id: int | None¶
Gets this item’s ID.
This can be set by the user when constructing an Item. If not, Discord will automatically provide one when the item’s parent is sent.
- Returns:
The ID of this item, or
Noneif the user didn’t set one.- Return type:
Optional[
int]
- property view: V | None¶
Gets the parent view associated with this item.
The view refers to the structure that holds this item. This is typically set automatically when the item is added to a view.
- Returns:
The parent view of this item, or
Noneif the item is not attached to any view.- Return type:
Optional[
BaseView]
- class discord.ui.Button(*, style=('secondary', 2), label=None, disabled=False, custom_id=None, url=None, emoji=None, sku_id=None, row=None, id=None)[source]¶
Represents a UI button.
Added in version 2.0.
- Parameters:
style (
discord.ButtonStyle) – The style of the button.custom_id (Optional[
str]) – 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.url (Optional[
str]) – The URL this button sends you to.disabled (
bool) – Whether the button is disabled or not.label (Optional[
str]) – The label of the button, if any. Maximum of 80 chars.emoji (Optional[Union[
PartialEmoji,GuildEmoji,AppEmoji,str]]) – The emoji of the button, if available.sku_id (Optional[Union[
int]]) – The ID of the SKU this button refers to.row (Optional[
int]) –The relative row this button belongs to. A Discord component can only have 5 rows. By default, items are arranged automatically into those 5 rows. If you’d like to control the relative positioning of the row then passing an index is advised. For example, row=1 will show up before row=2. Defaults to
None, which is automatic ordering. The row number must be between 0 and 4 (i.e. zero indexed).Warning
This parameter does not work in
ActionRow.id (Optional[
int]) – The button’s ID.
- property style: ButtonStyle¶
The style of the button.
- property custom_id: str | None¶
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.
- property emoji: PartialEmoji | None¶
The emoji of the button, if available.
- property width: int¶
Gets the width of the item in the UI layout.
The width determines how much horizontal space this item occupies within its row.
- Returns:
The width of the item. Buttons have a width of 1.
- Return type:
- await callback(interaction)¶
This function is a coroutine.
The callback associated with this UI item.
This can be overridden by subclasses.
- Parameters:
interaction (
Interaction) – The interaction that triggered this UI item.
- property id: int | None¶
Gets this item’s ID.
This can be set by the user when constructing an Item. If not, Discord will automatically provide one when the item’s parent is sent.
- Returns:
The ID of this item, or
Noneif the user didn’t set one.- Return type:
Optional[
int]
- property view: V | None¶
Gets the parent view associated with this item.
The view refers to the structure that holds this item. This is typically set automatically when the item is added to a view.
- Returns:
The parent view of this item, or
Noneif the item is not attached to any view.- Return type:
Optional[
BaseView]
- property row: int | None¶
Gets or sets the row position of this item within its parent view.
The row position determines the vertical placement of the item in the UI. The value must be an integer between 0 and 4 (inclusive), or
Noneto indicate that no specific row is set. This attribute is not compatible withdiscord.ui.DesignerView.- Returns:
The row position of the item, or
Noneif not explicitly set.- Return type:
Optional[
int]- Raises:
ValueError – If the row value is not
Noneand is outside the range [0, 4].
- defadd_default_value
- defadd_option
- defappend_default_value
- defappend_option
- asynccallback
- class discord.ui.Select(select_type=('string_select', 3), *, custom_id=None, placeholder=None, min_values=1, max_values=1, options=None, channel_types=None, disabled=False, row=None, id=None, required=None, default_values=None)[source]¶
Represents a UI select menu.
This is usually represented as a drop down menu.
In order to get the selected items that the user has chosen, use
Select.values.Added in version 2.0.
Changed in version 2.3: Added support for
discord.ComponentType.string_select,discord.ComponentType.user_select,discord.ComponentType.role_select,discord.ComponentType.mentionable_select, anddiscord.ComponentType.channel_select.Changed in version 2.7: Can now be sent in
discord.ui.DesignerModal.- Parameters:
select_type (
discord.ComponentType) –The type of select to create. Must be one of
discord.ComponentType.string_select,discord.ComponentType.user_select,discord.ComponentType.role_select,discord.ComponentType.mentionable_select, ordiscord.ComponentType.channel_select.The default is
discord.ComponentType.string_select, but if this is created using any of the provided aliases:StringSelect,RoleSelect,UserSelect,MentionableSelect, orChannelSelect, the default will be its respective select type.custom_id (
str) – The ID of the select menu that gets received during an interaction. If not given then one is generated for you.placeholder (Optional[
str]) – The placeholder text that is shown if nothing is selected, if any.min_values (
int) – The minimum number of items that must be chosen for this select menu. Defaults to 1 and must be between 1 and 25.max_values (
int) – The maximum number of items that must be chosen for this select menu. Defaults to 1 and must be between 1 and 25.options (List[
discord.SelectOption]) – A list of options that can be selected in this menu. Only valid for selects of typediscord.ComponentType.string_select.channel_types (List[
discord.ChannelType]) – A list of channel types that can be selected in this menu. Only valid for selects of typediscord.ComponentType.channel_select.disabled (
bool) – Whether the select is disabled or not. Only useable in views. Defaults toFalsein views.row (Optional[
int]) – The relative row this select menu belongs to. A Discord component can only have 5 rows. By default, items are arranged automatically into those 5 rows. If you’d like to control the relative positioning of the row then passing an index is advised. For example, row=1 will show up before row=2. Defaults toNone, which is automatic ordering. The row number must be between 0 and 4 (i.e. zero indexed). Does not work inActionRoworLabel.id (Optional[
int]) – The select menu’s ID.required (Optional[
bool]) –Whether the select is required or not. Only useable when added to
Labelfor modals. Defaults toTruein modals.Added in version 2.7.
default_values (Optional[Sequence[Union[
discord.SelectDefaultValue,discord.abc.Snowflake]]]) –The default values of this select. Only applicable if
select_typeis notdiscord.ComponentType.string_select.These can be either
discord.SelectDefaultValueinstances or models, which will be converted intodiscord.SelectDefaultValueinstances.Below, is a table defining the model instance type and the default value type it will be mapped:
Model Type
Default Value Type
depending on
discord.Object.type, it will be mapped to any aboveIf you pass a model that is not defined in the table,
TypeErrorwill be raised.Note
The
discord.abc.GuildChannelprotocol includesdiscord.TextChannel,discord.VoiceChannel,discord.StageChannel,discord.ForumChannel,discord.Thread,discord.MediaChannel. This list is not exhaustive, and is bound to change based of the new channel types Discord adds.Added in version 2.7.
- property placeholder: str | None¶
The placeholder text that is shown if nothing is selected, if any.
- property channel_types: list[ChannelType]¶
A list of channel types that can be selected in this menu.
- property options: list[SelectOption]¶
A list of options that can be selected in this menu.
- property default_values: list[SelectDefaultValue]¶
A list of the select’s default values. This is only applicable if the select type is not
discord.ComponentType.string_select.Added in version 2.7.
- add_default_value(*, id, type=...)[source]¶
Adds a default value to the select menu.
To append a pre-existing
discord.SelectDefaultValueuse theappend_default_value()method instead.Added in version 2.7.
- Parameters:
id (
int) – The ID of the entity to add as a default.type (
discord.SelectDefaultValueType) – The default value type of the ID. This is only required if the selecttypeisdiscord.ComponentType.mentionable_select.
- Raises:
TypeError – The select type is a mentionable_select and type was not provided, or the select type is string_select.
ValueError – The number of default select values exceeds 25.
- Return type:
Self
- append_default_value(value, /)[source]¶
Appends a default value to this select menu.
Added in version 2.7.
- Parameters:
value (Union[
discord.SelectDefaultValue,discord.abc.Snowflake]) –The default value to append to this select.
These can be either
discord.SelectDefaultValueinstances or models, which will be converted intodiscord.SelectDefaultvalueinstances.Below, is a table defining the model instance type and the default value type it will be mapped:
Model Type
Default Value Type
depending on
discord.Object.type, it will be mapped to any aboveIf you pass a model that is not defined in the table,
TypeErrorwill be raised.Note
The
discord.abc.GuildChannelprotocol includesdiscord.TextChannel,discord.VoiceChannel,discord.StageChannel,discord.ForumChannel,discord.Thread,discord.MediaChannel. This list is not exhaustive, and is bound to change based of the new channel types Discord adds.- Raises:
TypeError – The select type is string_select, which does not allow for default_values
ValueError – The number of default select values exceeds 25.
- Return type:
Self
- add_option(*, label, value=..., description=None, emoji=None, default=False)[source]¶
Adds an option to the select menu.
To append a pre-existing
discord.SelectOptionuse theappend_option()method instead.- Parameters:
label (
str) – The label of the option. This is displayed to users. Can only be up to 100 characters.value (
str) – The value of the option. This is not displayed to users. If not given, defaults to the label. Can only be up to 100 characters.description (Optional[
str]) – An additional description of the option, if any. Can only be up to 100 characters.emoji (Optional[Union[
str,GuildEmoji,AppEmoji,PartialEmoji]]) – The emoji of the option, if available. This can either be a string representing the custom or unicode emoji or an instance ofPartialEmoji,GuildEmoji, orAppEmoji.default (
bool) – Whether this option is selected by default.
- Raises:
ValueError – The number of options exceeds 25.
- Return type:
Self
- append_option(option)[source]¶
Appends an option to the select menu.
- Parameters:
option (
discord.SelectOption) – The option to append to the select menu.- Raises:
ValueError – The number of options exceeds 25.
- Return type:
Self
- property values: list[ST]¶
List[
str] | List[discord.Member|discord.User]] | List[discord.Role]] | List[discord.Member|discord.User|discord.Role]] | List[discord.abc.GuildChannel] | None: A list of values that have been selected by the user. This will beNoneif the select has not been interacted with yet.
- property width: int¶
Gets the width of the item in the UI layout.
The width determines how much horizontal space this item occupies within its row.
- Returns:
The width of the item. Select menus have a width of 5.
- Return type:
- property row: int | None¶
Gets or sets the row position of this item within its parent view.
The row position determines the vertical placement of the item in the UI. The value must be an integer between 0 and 4 (inclusive), or
Noneto indicate that no specific row is set. This attribute is not compatible withdiscord.ui.DesignerView.- Returns:
The row position of the item, or
Noneif not explicitly set.- Return type:
Optional[
int]- Raises:
ValueError – If the row value is not
Noneand is outside the range [0, 4].
- await callback(interaction)¶
This function is a coroutine.
The callback associated with this UI item.
This can be overridden by subclasses.
- Parameters:
interaction (
Interaction) – The interaction that triggered this UI item.
- property id: int | None¶
Gets this item’s ID.
This can be set by the user when constructing an Item. If not, Discord will automatically provide one when the item’s parent is sent.
- Returns:
The ID of this item, or
Noneif the user didn’t set one.- Return type:
Optional[
int]
- property modal: M | None¶
Gets the parent modal associated with this item. This is typically set automatically when the item is added to a modal.
- Returns:
The parent modal of this item, or
Noneif the item is not attached to any modal.- Return type:
Optional[
BaseModal]
- property view: V | None¶
Gets the parent view associated with this item.
The view refers to the structure that holds this item. This is typically set automatically when the item is added to a view.
- Returns:
The parent view of this item, or
Noneif the item is not attached to any view.- Return type:
Optional[
BaseView]
- class discord.ui.StringSelect¶
An alias for
Selectwithselect_typeasdiscord.ComponentType.string_select.
- class discord.ui.UserSelect¶
An alias for
Selectwithselect_typeasdiscord.ComponentType.user_select.
- class discord.ui.RoleSelect¶
An alias for
Selectwithselect_typeasdiscord.ComponentType.role_select.
- class discord.ui.MentionableSelect¶
An alias for
Selectwithselect_typeasdiscord.ComponentType.mentionable_select.
- class discord.ui.ChannelSelect¶
An alias for
Selectwithselect_typeasdiscord.ComponentType.channel_select.
- defadd_item
- defadd_text
- defcopy_text
- defdisable_all_items
- defenable_all_items
- defget_item
- defremove_item
- defset_accessory
- defset_thumbnail
- class discord.ui.Section(*items, accessory=None, id=None)[source]¶
Represents a UI section. Sections must have 1-3 (inclusive) items and an accessory set.
Added in version 2.7.
- Parameters:
*items (
ViewItem) – The initial items contained in this section, up to 3. Currently only supportsTextDisplay. Sections must have at least 1 item before being sent.accessory (Optional[
ViewItem]) – The section’s accessory. This is displayed in the top right of the section. Currently only supportsButtonandThumbnail. Sections must have an accessory attached before being sent.id (Optional[
int]) – The section’s ID.
- add_item(item)[source]¶
Adds an item to the section.
- Parameters:
item (
ViewItem) – The item to add to the section.- Raises:
ValueError – Maximum number of items has been exceeded (3).
- Return type:
Self
- remove_item(item)[source]¶
Removes an item from the section. If an
intorstris passed, the item will be removed by Itemidorcustom_idrespectively.
- get_item(id)[source]¶
Get an item from this section. Alias for utils.get(section.walk_items(), …). If an
intis provided, it will be retrieved byid, otherwise it will check the accessory’scustom_id.
- add_text(content, *, id=None)[source]¶
Adds a
TextDisplayto the section.- Parameters:
- Raises:
ValueError – Maximum number of items has been exceeded (3).
- Return type:
Self
- set_thumbnail(url, *, description=None, spoiler=False, id=None)[source]¶
Sets a
Thumbnailwith the provided URL as the section’saccessory.
- copy_text()[source]¶
Returns the text of all
TextDisplayitems in this section. Equivalent to the Copy Text option on Discord clients.- Return type:
- disable_all_items(*, exclusions=None)[source]¶
Disables all buttons and select menus in the section. At the moment, this only disables
accessoryif it is a button.- Parameters:
exclusions (Optional[List[
ViewItem]]) – A list of items in self.items to not disable from the view.- Return type:
Self
- enable_all_items(*, exclusions=None)[source]¶
Enables all buttons and select menus in the section. At the moment, this only enables
accessoryif it is a button.- Parameters:
exclusions (Optional[List[
ViewItem]]) – A list of items in self.items to not enable from the view.- Return type:
Self
- property id: int | None¶
Gets this item’s ID.
This can be set by the user when constructing an Item. If not, Discord will automatically provide one when the item’s parent is sent.
- Returns:
The ID of this item, or
Noneif the user didn’t set one.- Return type:
Optional[
int]
- property view: V | None¶
Gets the parent view associated with this item.
The view refers to the structure that holds this item. This is typically set automatically when the item is added to a view.
- Returns:
The parent view of this item, or
Noneif the item is not attached to any view.- Return type:
Optional[
BaseView]
- class discord.ui.TextDisplay(content, id=None)[source]¶
Represents a UI text display. A message can have up to 4000 characters across all
TextDisplayobjects combined.Added in version 2.7.
- Parameters:
- copy_text()[source]¶
Returns the content of this text display. Equivalent to the Copy Text option on Discord clients.
- Return type:
- property id: int | None¶
Gets this item’s ID.
This can be set by the user when constructing an Item. If not, Discord will automatically provide one when the item’s parent is sent.
- Returns:
The ID of this item, or
Noneif the user didn’t set one.- Return type:
Optional[
int]
- property modal: M | None¶
Gets the parent modal associated with this item. This is typically set automatically when the item is added to a modal.
- Returns:
The parent modal of this item, or
Noneif the item is not attached to any modal.- Return type:
Optional[
BaseModal]
- property view: V | None¶
Gets the parent view associated with this item.
The view refers to the structure that holds this item. This is typically set automatically when the item is added to a view.
- Returns:
The parent view of this item, or
Noneif the item is not attached to any view.- Return type:
Optional[
BaseView]
- class discord.ui.Thumbnail(url, *, description=None, spoiler=False, id=None)[source]¶
Represents a UI Thumbnail.
Added in version 2.7.
- Parameters:
url (
str) – The url of the thumbnail. This can either be an arbitrary URL or anattachment://URL to work with local files.description (Optional[
str]) – The thumbnail’s description, up to 1024 characters.spoiler (Optional[
bool]) – Whether the thumbnail has the spoiler overlay. Defaults toFalse.id (Optional[
int]) – The thumbnail’s ID.
- property media: UnfurledMediaItem¶
The thumbnail’s unerlying media item.
- property url: str¶
The URL of this thumbnail’s media. This can either be an arbitrary URL or an
attachment://URL.
- property id: int | None¶
Gets this item’s ID.
This can be set by the user when constructing an Item. If not, Discord will automatically provide one when the item’s parent is sent.
- Returns:
The ID of this item, or
Noneif the user didn’t set one.- Return type:
Optional[
int]
- property view: V | None¶
Gets the parent view associated with this item.
The view refers to the structure that holds this item. This is typically set automatically when the item is added to a view.
- Returns:
The parent view of this item, or
Noneif the item is not attached to any view.- Return type:
Optional[
BaseView]
- defadd_item
- defappend_item
- defremove_item
- class discord.ui.MediaGallery(*items, id=None)[source]¶
Represents a UI Media Gallery. Galleries may contain up to 10
MediaGalleryItemobjects.Added in version 2.7.
- Parameters:
*items (
MediaGalleryItem) – The initial items contained in this gallery, up to 10.id (Optional[
int]) – The gallery’s ID.
- items¶
The list of media items in this gallery.
- Type:
List[
MediaGalleryItem]
- property items: list[MediaGalleryItem]¶
The list of media items in this gallery.
- append_item(item)[source]¶
Adds a
MediaGalleryItemto the gallery.- Parameters:
item (
MediaGalleryItem) – The gallery item to add to the gallery.- Raises:
TypeError – A
MediaGalleryItemwas not passed.ValueError – Maximum number of items has been exceeded (10).
- Return type:
Self
- add_item(url, *, description=None, spoiler=False)[source]¶
Adds a new media item to the gallery.
- Parameters:
- Raises:
ValueError – Maximum number of items has been exceeded (10).
- Return type:
Self
- remove_item(index)[source]¶
Removes an item from the gallery.
- Parameters:
index (
int) – The index of the item to remove from the gallery.- Return type:
Self
- property id: int | None¶
Gets this item’s ID.
This can be set by the user when constructing an Item. If not, Discord will automatically provide one when the item’s parent is sent.
- Returns:
The ID of this item, or
Noneif the user didn’t set one.- Return type:
Optional[
int]
- property view: V | None¶
Gets the parent view associated with this item.
The view refers to the structure that holds this item. This is typically set automatically when the item is added to a view.
- Returns:
The parent view of this item, or
Noneif the item is not attached to any view.- Return type:
Optional[
BaseView]
- class discord.ui.File(url, *, spoiler=False, id=None)[source]¶
Represents a UI File.
Note
This component does not show media previews. Use
MediaGalleryfor previews instead.Added in version 2.7.
- Parameters:
- property file: UnfurledMediaItem¶
The file’s unerlying media item.
- property url: str¶
The URL of this file’s underlying media. This must be an
attachment://URL that references aFile.
- property id: int | None¶
Gets this item’s ID.
This can be set by the user when constructing an Item. If not, Discord will automatically provide one when the item’s parent is sent.
- Returns:
The ID of this item, or
Noneif the user didn’t set one.- Return type:
Optional[
int]
- property view: V | None¶
Gets the parent view associated with this item.
The view refers to the structure that holds this item. This is typically set automatically when the item is added to a view.
- Returns:
The parent view of this item, or
Noneif the item is not attached to any view.- Return type:
Optional[
BaseView]
- class discord.ui.Separator(*, divider=True, spacing=('small', 1), id=None)[source]¶
Represents a UI Separator.
Added in version 2.7.
- Parameters:
divider (
bool) – Whether the separator is a divider. Defaults toTrue.spacing (
SeparatorSpacingSize) – The spacing size of the separator. Defaults tosmall.id (Optional[
int]) – The separator’s ID.
- property id: int | None¶
Gets this item’s ID.
This can be set by the user when constructing an Item. If not, Discord will automatically provide one when the item’s parent is sent.
- Returns:
The ID of this item, or
Noneif the user didn’t set one.- Return type:
Optional[
int]
- property view: V | None¶
Gets the parent view associated with this item.
The view refers to the structure that holds this item. This is typically set automatically when the item is added to a view.
- Returns:
The parent view of this item, or
Noneif the item is not attached to any view.- Return type:
Optional[
BaseView]
- defadd_file
- defadd_gallery
- defadd_item
- defadd_row
- defadd_section
- defadd_separator
- defadd_text
- defcopy_text
- defdisable_all_items
- defenable_all_items
- defget_item
- defremove_item
- class discord.ui.Container(*items, colour=None, color=None, spoiler=False, id=None)[source]¶
Represents a UI Container.
The current items supported are as follows:
Added in version 2.7.
- Parameters:
- remove_item(item)[source]¶
Removes an item from the container. If an int or str is passed, it will remove by Item
idorcustom_idrespectively.
- get_item(id)[source]¶
Get an item from this container. Roughly equivalent to utils.get(container.items, …). If an
intis provided, the item will be retrieved byid, otherwise bycustom_id. This method will also search for nested items.
- add_row(*items, id=None)[source]¶
Adds an
ActionRowto the container.To append a pre-existing
ActionRow, useadd_item()instead.
- add_section(*items, accessory, id=None)[source]¶
Adds a
Sectionto the container.To append a pre-existing
Section, use theadd_item()method, instead.- Parameters:
- Return type:
Self
- add_text(content, id=None)[source]¶
Adds a
TextDisplayto the container.
- add_gallery(*items, id=None)[source]¶
Adds a
MediaGalleryto the container.To append a pre-existing
MediaGallery, useadd_item()instead.- Parameters:
*items (
MediaGalleryItem) – The media this gallery contains.id (Optiona[
int]) – The gallery’s ID.
- Return type:
Self
- add_file(url, spoiler=False, id=None)[source]¶
Adds a
TextDisplayto the container.
- add_separator(*, divider=True, spacing=('small', 1), id=None)[source]¶
Adds a
Separatorto the container.- Parameters:
divider (
bool) – Whether the separator is a divider. Defaults toTrue.spacing (
SeparatorSpacingSize) – The spacing size of the separator. Defaults tosmall.id (Optional[
int]) – The separator’s ID.
- Return type:
Self
- copy_text()[source]¶
Returns the text of all
TextDisplayitems in this container. Equivalent to the Copy Text option on Discord clients.- Return type:
- disable_all_items(*, exclusions=None)[source]¶
Disables all buttons and select menus in the container.
- Parameters:
exclusions (Optional[List[
ViewItem]]) – A list of items in self.items to not disable from the view.- Return type:
Self
- enable_all_items(*, exclusions=None)[source]¶
Enables all buttons and select menus in the container.
- Parameters:
exclusions (Optional[List[
ViewItem]]) – A list of items in self.items to not enable from the view.- Return type:
Self
- property id: int | None¶
Gets this item’s ID.
This can be set by the user when constructing an Item. If not, Discord will automatically provide one when the item’s parent is sent.
- Returns:
The ID of this item, or
Noneif the user didn’t set one.- Return type:
Optional[
int]
- property view: V | None¶
Gets the parent view associated with this item.
The view refers to the structure that holds this item. This is typically set automatically when the item is added to a view.
- Returns:
The parent view of this item, or
Noneif the item is not attached to any view.- Return type:
Optional[
BaseView]
- defadd_item
- asynccallback
- defget_item
- asyncon_error
- asyncon_timeout
- defremove_item
- defstop
- asyncwait
- class discord.ui.BaseModal(*children, title, custom_id=None, timeout=None, store=True)[source]¶
The base class for creating pop-up modals.
Added in version 2.7.
- Parameters:
- await callback(interaction)[source]¶
This function is a coroutine.
The coroutine that is called when the modal is submitted. Should be overridden to handle the values submitted by the user.
- Parameters:
interaction (
Interaction) – The interaction that submitted the modal.
- add_item(item)[source]¶
Adds a component to the modal.
- Parameters:
item (Union[
ModalItem]) – The item to add to the modal- Return type:
Self
- remove_item(item)[source]¶
Removes a component from the modal.
- Parameters:
item (
ModalItem) – The item to remove from the modal.- Return type:
Self
- await on_error(error, interaction)[source]¶
This function is a coroutine.
A callback that is called when the modal’s callback fails with an error.
The default implementation prints the traceback to stderr.
- Parameters:
error (
Exception) – The exception that was raised.interaction (
Interaction) – The interaction that led to the failure.
- Return type:
- await on_timeout()[source]¶
This function is a coroutine.
A callback that is called when a modal’s timeout elapses without being explicitly stopped.
- Return type:
- defadd_item
- asynccallback
- defget_item
- asyncon_error
- asyncon_timeout
- defremove_item
- defstop
- asyncwait
- class discord.ui.Modal(*children, title, custom_id=None, timeout=None, store=True)[source]¶
Represents a legacy UI modal for InputText components.
This object must be inherited to create a UI within Discord.
Added in version 2.0.
Changed in version 2.7: Now inherits from
BaseModal- Parameters:
children (Union[
InputText]) – The initial items that are displayed in the modal. Only supportsdiscord.ui.InputText; for newer modal features, seeDesignerModal.title (
str) – The title of the modal. Must be 45 characters or fewer.custom_id (Optional[
str]) – The ID of the modal that gets received during an interaction. Must be 100 characters or fewer.timeout (Optional[
float]) – Timeout in seconds from last interaction with the UI before no longer accepting input. IfNonethen there is no timeout.store (Optional[
bool]) – Whether this modal should be stored for callback listening. Setting it toFalsewill ignore its callback and prevent item values from being refreshed. Defaults toTrue.
- add_item(item)[source]¶
Adds an InputText component to the modal.
- Parameters:
item (
InputText) – The item to add to the modal- Return type:
Self
- remove_item(item)[source]¶
Removes an InputText from the modal.
- Parameters:
item (Union[class:InputText]) – The item to remove from the modal.
- Return type:
Self
- await callback(interaction)¶
This function is a coroutine.
The coroutine that is called when the modal is submitted. Should be overridden to handle the values submitted by the user.
- Parameters:
interaction (
Interaction) – The interaction that submitted the modal.
- get_item(custom_id)¶
Gets an item from this structure. Roughly equal to utils.get(self.children, …). If an
intis provided, the item will be retrieved byid, otherwise bycustom_id. This method will also search nested items.
- await on_error(error, interaction)¶
This function is a coroutine.
A callback that is called when the modal’s callback fails with an error.
The default implementation prints the traceback to stderr.
- Parameters:
error (
Exception) – The exception that was raised.interaction (
Interaction) – The interaction that led to the failure.
- Return type:
- defadd_item
- asynccallback
- defget_item
- asyncon_error
- asyncon_timeout
- defremove_item
- defstop
- asyncwait
- class discord.ui.DesignerModal(*children, title, custom_id=None, timeout=None, store=True)[source]¶
Represents a UI modal compatible with all modal features.
This object must be inherited to create a UI within Discord.
Added in version 2.7.
- Parameters:
children (Union[
ModalItem]) – The initial items that are displayed in the modal..title (
str) – The title of the modal. Must be 45 characters or fewer.custom_id (Optional[
str]) – The ID of the modal that gets received during an interaction. Must be 100 characters or fewer.timeout (Optional[
float]) – Timeout in seconds from last interaction with the UI before no longer accepting input. IfNonethen there is no timeout.store (Optional[
bool]) – Whether this modal should be stored for callback listening. Setting it toFalsewill ignore its callback and prevent item values from being refreshed. Defaults toTrue.
- add_item(item)[source]¶
Adds a component to the modal.
- Parameters:
item (Union[
ModalItem]) – The item to add to the modal- Return type:
Self
- await callback(interaction)¶
This function is a coroutine.
The coroutine that is called when the modal is submitted. Should be overridden to handle the values submitted by the user.
- Parameters:
interaction (
Interaction) – The interaction that submitted the modal.
- get_item(custom_id)¶
Gets an item from this structure. Roughly equal to utils.get(self.children, …). If an
intis provided, the item will be retrieved byid, otherwise bycustom_id. This method will also search nested items.
- await on_error(error, interaction)¶
This function is a coroutine.
A callback that is called when the modal’s callback fails with an error.
The default implementation prints the traceback to stderr.
- Parameters:
error (
Exception) – The exception that was raised.interaction (
Interaction) – The interaction that led to the failure.
- Return type:
- await on_timeout()¶
This function is a coroutine.
A callback that is called when a modal’s timeout elapses without being explicitly stopped.
- Return type:
- defget_item
- defset_file_upload
- defset_input_text
- defset_item
- defset_select
- class discord.ui.Label(label, item=None, *, description=None, id=None)[source]¶
Represents a UI Label used in
discord.ui.DesignerModal.The items currently supported are as follows:
Added in version 2.7.
- Parameters:
- property modal: M | None¶
Gets the parent modal associated with this item. This is typically set automatically when the item is added to a modal.
- Returns:
The parent modal of this item, or
Noneif the item is not attached to any modal.- Return type:
Optional[
BaseModal]
- get_item(id)[source]¶
Get the item from this label if it matches the provided id. If an
intis provided, the item will match byid, otherwise bycustom_id.
- set_input_text(*, style=('short', 1), custom_id=None, placeholder=None, min_length=None, max_length=None, required=True, value=None, id=None)[source]¶
Set this label’s item to an input text.
To set a pre-existing
InputText, use theset_item()method, instead.- Parameters:
style (
InputTextStyle) – The style of the input text field.custom_id (Optional[
str]) – The ID of the input text field that gets received during an interaction.placeholder (Optional[
str]) – The placeholder text that is shown if nothing is selected, if any. Must be 100 characters or fewer.min_length (Optional[
int]) – The minimum number of characters that must be entered. Defaults to 0 and must be less than 4000.max_length (Optional[
int]) – The maximum number of characters that can be entered. Must be between 1 and 4000.required (Optional[
bool]) – Whether the input text field is required or not. Defaults toTrue.value (Optional[
str]) – Pre-fills the input text field with this value. Must be 4000 characters or fewer.id (Optional[
int]) – The button’s ID.
- Return type:
Self
- set_select(select_type=('string_select', 3), *, custom_id=None, placeholder=None, min_values=1, max_values=1, options=None, channel_types=None, required=True, id=None, default_values=Ellipsis)[source]¶
Set this label’s item to a select menu.
- Parameters:
select_type (
discord.ComponentType) – The type of select to create. Must be one ofdiscord.ComponentType.string_select,discord.ComponentType.user_select,discord.ComponentType.role_select,discord.ComponentType.mentionable_select, ordiscord.ComponentType.channel_select.custom_id (
str) – The custom ID of the select menu that gets received during an interaction. If not given then one is generated for you.placeholder (Optional[
str]) – The placeholder text that is shown if nothing is selected, if any.min_values (
int) – The minimum number of items that must be chosen for this select menu. Defaults to 1 and must be between 1 and 25.max_values (
int) – The maximum number of items that must be chosen for this select menu. Defaults to 1 and must be between 1 and 25.options (List[
discord.SelectOption]) – A list of options that can be selected in this menu. Only valid for selects of typediscord.ComponentType.string_select.channel_types (List[
discord.ChannelType]) – A list of channel types that can be selected in this menu. Only valid for selects of typediscord.ComponentType.channel_select.required (
bool) – Whether the select is required or not. Defaults toTrue.id (Optional[
int]) – The select menu’s ID.default_values (Optional[Sequence[Union[
discord.SelectDefaultValue,discord.abc.Snowflake]]]) –The default values of this select. Only applicable if
select_typeis notdiscord.ComponentType.string_select.These can be either
discord.SelectDefaultValueinstances or models, which will be converted intodiscord.SelectDefaultValueinstances.
- Return type:
Self
- set_file_upload(*, custom_id=None, min_values=None, max_values=None, required=True, id=None)[source]¶
Set this label’s item to a file upload.
To set a pre-existing
FileUpload, use theset_item()method, instead.- Parameters:
custom_id (Optional[
str]) – The ID of the input text field that gets received during an interaction.min_values (Optional[
int]) – The minimum number of files that must be uploaded. Defaults to 0 and must be between 0 and 10, inclusive.max_values (Optional[
int]) – The maximum number of files that can be uploaded. Must be between 1 and 10, inclusive.required (Optional[
bool]) – Whether the file upload field is required or not. Defaults toTrue.id (Optional[
int]) – The file upload field’s ID.
- Return type:
Self
- class discord.ui.InputText(*, style=('short', 1), custom_id=None, label=None, placeholder=None, min_length=None, max_length=None, required=True, value=None, row=None, id=None)[source]¶
Represents a UI text input field.
Added in version 2.0.
- Parameters:
style (
InputTextStyle) – The style of the input text field.custom_id (Optional[
str]) – The ID of the input text field that gets received during an interaction.label (
str) – The label for the input text field. Must be 45 characters or fewer.placeholder (Optional[
str]) – The placeholder text that is shown if nothing is selected, if any. Must be 100 characters or fewer.min_length (Optional[
int]) – The minimum number of characters that must be entered. Defaults to 0 and must be less than 4000.max_length (Optional[
int]) – The maximum number of characters that can be entered. Must be between 1 and 4000.required (Optional[
bool]) – Whether the input text field is required or not. Defaults toTrue.value (Optional[
str]) – Pre-fills the input text field with this value. Must be 4000 characters or fewer.row (Optional[
int]) – The relative row this input text field belongs to. A modal dialog can only have 5 rows. By default, items are arranged automatically into those 5 rows. If you’d like to control the relative positioning of the row then passing an index is advised. For example, row=1 will show up before row=2. Defaults toNone, which is automatic ordering. The row number must be between 0 and 4 (i.e. zero indexed).
- property style: InputTextStyle¶
The style of the input text field.
- property placeholder: str | None¶
The placeholder text that is shown before anything is entered, if any.
- property min_length: int | None¶
The minimum number of characters that must be entered. Defaults to 0.
- property id: int | None¶
Gets this item’s ID.
This can be set by the user when constructing an Item. If not, Discord will automatically provide one when the item’s parent is sent.
- Returns:
The ID of this item, or
Noneif the user didn’t set one.- Return type:
Optional[
int]
- class discord.ui.FileUpload(*, custom_id=None, min_values=None, max_values=None, required=True, id=None)[source]¶
Represents a UI File Upload component.
Added in version 2.7.
- Parameters:
custom_id (Optional[
str]) – The ID of the input text field that gets received during an interaction.min_values (Optional[
int]) – The minimum number of files that must be uploaded. Defaults to 0 and must be between 0 and 10, inclusive.max_values (Optional[
int]) – The maximum number of files that can be uploaded. Must be between 1 and 10, inclusive.required (Optional[
bool]) – Whether the file upload field is required or not. Defaults toTrue.id (Optional[
int]) – The file upload field’s ID.
- property id: int | None¶
The ID of this component. If not provided by the user, it is set sequentially by Discord.
- property modal: M | None¶
Gets the parent modal associated with this item. This is typically set automatically when the item is added to a modal.
- Returns:
The parent modal of this item, or
Noneif the item is not attached to any modal.- Return type:
Optional[
BaseModal]
- property values: list[Attachment] | None¶
The files that were uploaded to the field.