Search Results for

    Show / Hide Table of Contents

    Message Components

    Message components are a framework for adding interactive elements to a message your app or bot sends. They're accessible, customizable, and easy to use.

    What is a Component

    Components are a new parameter you can use when sending messages with your bot. There are currently 2 different types of components you can use: Buttons and Select Menus.

    Creating components

    Lets create a simple component that has a button. First thing we need is a way to trigger the message, this can be done via commands or simply a ready event. Lets make a command that triggers our button message.

    [Command("spawner")]
    public async Task Spawn()
    {
        // Reply with some components
    }
    

    We now have our command, but we need to actually send the buttons with the command. To do that, lets look at the ComponentBuilder class:

    Name Description
    FromMessage Creates a new builder from a message.
    FromComponents Creates a new builder from the provided list of components.
    WithSelectMenu Adds a SelectMenuBuilder to the ComponentBuilder at the specific row.
    WithButton Adds a ButtonBuilder to the ComponentBuilder at the specific row.
    Build Builds this builder into a MessageComponent used to send your components.

    We see that we can use the WithButton function so lets do that. looking at its parameters it takes:

    • label - The display text of the button.
    • customId - The custom id of the button, this is whats sent by discord when your button is clicked.
    • style - The discord defined style of the button.
    • emote - An emote to be displayed with the button.
    • url - The url of the button if its a link button.
    • disabled - Whether or not the button is disabled.
    • row - The row the button will occupy.

    Since were just making a busic button, we dont have to specify anything else besides the label and custom id.

    var builder = new ComponentBuilder()
        .WithButton("label", "custom-id");
    

    Lets add this to our command:

    [Command("spawner")]
    public async Task Spawn()
    {
        var builder = new ComponentBuilder()
            .WithButton("label", "custom-id");
    
        await ReplyAsync("Here is a button!", components: builder.Build());
    }
    


    This page was last modified at 12/13/2021 04:47:54 -04:00 (UTC).

    Commit Message
    Author:    Quin Lynch
    Commit:    5a7e43a8cb33d0e04af3c754ba36926c5d2b936e
    
    V3 Final merge (#1966)
    
    * Update azure-pipelines.yml
    
    * Update deploy.yml
    
    * Remove version tag from proj
    
    * Update deploy.yml
    
    * Removed versions from project files
    
    * Removed style of the nuget badge and added logo (#201)
    
    The style was not properly added to it and the plastic version does not look good with the discord badge.
    I thought it would look better with a logo
    
    * Fix Type not be...

    Theme

    • Improve this Doc
    In This Article
    Back to top Discord.Net (c) 2015-2023 3.10.0