Table of Contents

Responding to Interactions

This guide explains how to handle interactions and prepare appropriate responses.

To create a response, use the InteractionCallback class. It supports various response types, such as messages, modals, and autocomplete suggestions. Once created, the callback must be passed to the Interaction.SendResponseAsync method to send it to Discord.

await interaction.SendResponseAsync(callback);

The NetCord.Services package provides additional utilities to simplify this process.

Responding with a Message

You can respond with a message by using InteractionCallback.Message.

callback = InteractionCallback.Message("Here is a sample message interaction callback!");

For advanced message options, see Sending Messages.

Responding with Deferral

Deferring is useful when performing long-running operations before sending a message. Deferrals give you up to 15 minutes to complete the operation. After deferring, you can either modify the initial response (the deferral) or send a follow-up message, both work the same way.

await interaction.ModifyResponseAsync(message => message.Content = "The response was modified!");
await interaction.SendFollowupMessageAsync("The response was provided via follow-up!");

The NetCord.Services package also provides shortcuts for deferral handling.

For advanced message options, see Sending Messages.

Deferred Message

For application commands, use InteractionCallback.DeferredMessage to send a deferral response. This shows a loading state to the user while you prepare the message.

callback = InteractionCallback.DeferredMessage();

You can specify MessageFlags.Ephemeral to make the response visible only to the user who triggered the interaction.

callback = InteractionCallback.DeferredMessage(MessageFlags.Ephemeral);

Deferred Modify Message

For component interactions, use InteractionCallback.DeferredModifyMessage. This type of deferral doesn't display a loading state to the user.

callback = InteractionCallback.DeferredModifyMessage;

Responding with Message Modification

For message component interactions, you can modify the message they are attached to using InteractionCallback.ModifyMessage.

callback = InteractionCallback.ModifyMessage(message => message.Content = "New content!");

Responding with a Modal

Modals are interactive forms users can fill out. Use InteractionCallback.Modal to create a modal callback. Each modal can include up to five text inputs, but no other component types are supported.

callback = InteractionCallback.Modal(new("intro", "Introduce Yourself")
{
    new TextInputProperties("name", TextInputStyle.Short, "First Name"),
    new TextInputProperties("bio", TextInputStyle.Paragraph, "Your Bio"),
});

It is also supported to provide additional options for each input, such as placeholder text, default value, and whether the input is required.

callback = InteractionCallback.Modal(new("intro", "Introduce Yourself")
{
    new TextInputProperties("name", TextInputStyle.Short, "First Name")
    {
        MinLength = 2,
        MaxLength = 32,
        Placeholder = "Enter your name",
    },
    new TextInputProperties("bio", TextInputStyle.Paragraph, "Your Bio")
    {
        MinLength = 10,
        Required = false,
        Value = "I love programming!",
    },
});

Responding with Autocomplete

Autocomplete provides a list of options while the user types a slash command. Use InteractionCallback.Autocomplete to respond to autocomplete interactions.

For string parameters:

callback = InteractionCallback.Autocomplete([new("Dog", "dog"), new("Cat", "cat")]);

For numeric parameters:

callback = InteractionCallback.Autocomplete([new("Frog", 0), new("Duck", 1)]);

It is also supported to provide name localizations for each option.

callback = InteractionCallback.Autocomplete(
[
    new("Lion", "lion")
    {
        NameLocalizations = new Dictionary<string, string> { ["pl"] = "Lew" },
    },
    new("Elephant", "elephant")
    {
        NameLocalizations = new Dictionary<string, string> { ["pl"] = "Słoń" },
    },
]);

Responding with a Pong

A "pong" callback is used to respond to ping HTTP interactions sent by Discord to verify the endpoint's availability. Use InteractionCallback.Pong to create this response.

callback = InteractionCallback.Pong;

If you're using the NetCord.Hosting.AspNetCore package, these ping interactions are handled automatically, and you don't need to provide the callback manually.