Skip to content

Multi Buttons Select

Simon Hjorthøj edited this page Dec 19, 2021 · 10 revisions

MultiButtons Select example

Inject the MultiButtonsService into your Module using DI instead. (Constructor / Public Property Injection).

Example: Creating Multi Buttons Select

In order to get a list of users you have to activate the "Privileged Gateway Intents" those being "PRESENCE INTENT" and "SERVER MEMBERS INTENT" they can be set here by choosing your bot and going to the Bots tab. Remember if you your bot is in 100 or more servers then it needs to get verification and whitelisting from discord for the intents to work. Read more here.

Place this in side of your Button Handler and set the "multiButtons" to what your customId is.

private async Task ButtonHandler(SocketMessageComponent interaction) {
    if (Regex.IsMatch(customId, "multiButtons[0-9]+")) await _buttonCommands.ChooseChildNameRange(interaction);
}

You need to give it the full list and it will figure out the rest. Again you can use it as I have with users or your own list.

public async Task ChooseChildNameRange(SocketMessageComponent interaction) {
    List<MultiButton> multiButtons = new();
    List<RestGuildUser> users = await _userService.GetSortedUserListAsync(((SocketGuildUser)interaction.User).Guild);
    foreach (RestGuildUser user in users) {
        MultiButton multiButton = new() {
            Title = user.Nickname ?? user.Username,
            Value = user.Id.ToString()
        };

        multiButtons.Add(multiButton);
    }

    var builder = _multiButtonsService.CreateSelectForMultiButtons(interaction, multiButtons);

    await interaction.FollowupAsync("Choose Person", component: builder.Build());
}

Example: Changing Multi Buttons Selection Customization

None of the MultiButtonsStyles has to be set what you see is their default values.

You can leave out all of them or some of them or change them at will.

public async Task ChooseChildNameRange(SocketMessageComponent interaction) {
    List<MultiButton> multiButtons = new();
    List<RestGuildUser> users = await _userService.GetSortedUserListAsync(((SocketGuildUser)interaction.User).Guild);
    foreach (RestGuildUser user in users) {
        MultiButton multiButton = new() {
            Title = user.Nickname ?? user.Username,
            Value = user.Id.ToString()
        };

        multiButtons.Add(multiButton);
    }

    SelectForMultiButtonsStyles selectForMultiButtonsStyles = new() {
        CustomID = "chooseRange",
        Placeholder = "Select Item",
        RagedLettersOnEndOfPlaceholder = true,
        OrderByTitle = true
    };

    var builder = _multiButtonsService.CreateSelectForMultiButtons(interaction, multiButtons, selectForMultiButtonsStyles);

    await interaction.FollowupAsync("Choose Person", component: builder.Build());
}

Example: Removing all multi buttons messages and the select message

public async Task OnSelectionChooseRange(SocketMessageComponent interaction) {
    await _multiButtonsService.RemoveMultiButtonsAndSelectAsync(interaction);
}