Rich Button Implementation

Default implementation of button-based components.

Classes

class disnake_compass.impl.component.button.RichButton(*args, **kwargs)[source]

Bases: RichButton, ComponentBase, Protocol

The default implementation of a disnake-compass button.

This works similar to a dataclass, but with some extra things to take into account.

First and foremost, there are class variables for label, style, emoji and disabled. These set the corresponding attributes on the button class when they are sent to discord, and are meant to be overwritten by the user.

Next, fields can be defined similarly to dataclasses, by means of a name, a type annotation, and an optional components.field() to set the default or a custom parser.

Classes created in this way have auto-generated slots and an auto-generated __init__. The init-signature contains all the custom id fields as keyword-only arguments.

Attributes

disabled: bool

Whether or not this button is disabled.

A disabled button is greyed out on discord, and cannot be pressed. Disabled buttons can therefore not cause any interactions, either.

emoji: _AnyEmoji | None

The emoji that is to be displayed on this button.

Either or both this field or the emoji field must be set.

label: str | None

The label of the component.

Either or both this field or the emoji field must be set.

style: disnake.ButtonStyle

The style of the component.

This dictates how the button is displayed on discord.

Methods

await as_ui_component(manager=None, /)[source]

Convert this component into a component that can be sent by disnake.

Returns:

A component that can be sent by disnake, maintaining the parameters and custom id set on this rich component.

Return type:

WrappedComponent

abstractmethod await callback(inter, /)[source]

Run the component callback.

This should be implemented by the user in each concrete component type.

Parameters:

interaction – The interaction that caused this button to fire.

classmethod get_factory()[source]

Get the factory that built this component instance.

Note

Component base classes can be declared as a subclass of Protocols, which tells disnake_compass that this component is a ‘template’ and should not be instantiable. In this case, this attribute defaults to a NoopFactory.

In case a concrete component subclass is created, a matching ComponentFactory is automatically generated instead.

classmethod get_manager()[source]

Get the manager that was responsible for parsing this component instance.

await make_custom_id(manager, /)[source]

Make a custom id from this component given its current state.

The generated custom id will contain the full state of the component, such that it be used to entirely reconstruct the component later.

Because parameter to string conversion supports asynchronous callbacks, this has to be an async function instead of e.g. a property.

Note

As the logic for translating a component to and from a custom id resides inside the component manager, the component must be registered to a manager to use this method.

Parameters:

manager (ComponentManager | None) – The manager to use to make a custom id for this component. This is only relevant if you have multiple managers for the same component. Defaults to the root manager.

Returns:

The custom id representing the full state of this component.

Return type:

str

classmethod set_factory(factory)[source]

Set the factory that built this component instance.

classmethod set_manager(manager)[source]

Set the manager that was responsible for parsing this component instance.