Views

Each menu has a view, which represents a message, a set of message components attached to it, and any optional state attached to it. The state being, for example, how many times the user clicked a button or the current page index in a paginated display. So essentially, a view defines what the user currently sees.

Creating a View

This is a view that would just show "Hello!" when sent:

public class MyView : ViewBase
{
    // I want the message to always greet a user,
    // hence I just pass a hardcoded message to the base constructor.
    public MyView()
        : base(new LocalMessage().WithContent("Hello!"))
    { }
}

The ViewBase constructor takes in a "template message" parameter. In this context, this is the message that (including the components we'll add in the next step) will be sent by the menu and displayed to the user.

View Components

View components are message components such as buttons, selections, etc. that you can put in your view similarly to commands and their callbacks will be invoked whenever the user interacts with them.

Let's add a basic button sends a message mentioning the user when clicked.

[Button(Emoji = "👉", Label = "Click Me")]
public async ValueTask ClickMe(ButtonEventArgs e)
{
    var message = new LocalInteractionResponse()
        .WithContent(Mention.User(e.AuthorId));

    await e.Interaction.Response().SendMessageAsync(message);
}

Each view component callback must return a ValueTask and take in the appropriate EventArgs parameter, otherwise you'll get an exception.

Using the View

With a view set up, you can now display it by attaching it to a menu.

Next up: Creating a Menu.