@typed-discord/interactions
    Preparing search index...

    @typed-discord/interactions

    About

    @typed-discord/interactions is a strongly typed library for efficiently handling Discord interactions. It provides types, classes, and a router to handle interactions.

    ⚠️ This project is in its initial release phase and breaking changes may occur at any time.

    Quick Start

    This example shows how to create a simple router with buttons and modals.

    import { MessageComponentTypes, TextInputStyleTypes, ButtonStyleTypes } from "@typed-discord/rest/types"
    import { ChannelMessage, DeferredUpdateMessage, Modal, UpdateMessage } from "@typed-discord/interactions/responses"
    import { createRouter } from "@typed-discord/interactions/router"

    const router = createRouter({
    onGlobalSlashCommand(name) {
    return new ChannelMessage({
    content: name,
    components: [{
    type: MessageComponentTypes.ACTION_ROW,
    components: [{
    type: MessageComponentTypes.BUTTON,
    style: ButtonStyleTypes.PRIMARY,
    customId: "edit_message",
    label: "Edit message"
    }, {
    type: MessageComponentTypes.BUTTON,
    style: ButtonStyleTypes.SECONDARY,
    customId: "new_message",
    label: "New message"
    }, {
    type: MessageComponentTypes.BUTTON,
    style: ButtonStyleTypes.DANGER,
    customId: "delete_message",
    label: "Delete message"
    }]
    }]
    });
    },
    onButtonClick(customId) {
    switch (customId) {
    case "edit_message": return new Modal({
    title: "Edit message",
    customId: "edit_message",
    components: [{
    type: MessageComponentTypes.LABEL,
    label: "Message content",
    component: {
    type: MessageComponentTypes.TEXT_INPUT,
    style: TextInputStyleTypes.PARAGRAPH,
    customId: "content"
    }
    }]
    });
    case "new_message": return new ChannelMessage({
    content: "Created a new message!"
    });
    case "delete_message": return new DeferredUpdateMessage(webhook => {
    webhook.deleteOriginalInteractionResponse();
    });
    default: return new ChannelMessage({
    content: "Unexpected customId."
    });
    }
    },
    onModalSubmitFromMessage(customId, components) {
    switch (customId) {
    case "edit_message": return new UpdateMessage({
    content: components["content"] as string
    });
    default: return new ChannelMessage({
    content: "Unexpected customId."
    });
    }
    },
    });

    Pass the previously created router to the discord.js listener.

    import { Client, GatewayDispatchEvents } from "discord.js";

    const client = new Client({ intents: [] });
    client.login(process.env.DISCORD_TOKEN);

    client.ws.on(GatewayDispatchEvents.InteractionCreate, router);

    Respond to the original HTTP request with a 202 and no body, then pass the received interaction to the router.

    res.writeHead(202).end();
    router(interaction);

    Responding directly to the request with a 200 code (without going through the REST API) will be possible soon.

    Modules

    The package exports the following modules:

    • /types provides the types for the interactions, mostly used internally
    • /router provides methods for handling an interaction by calling the corresponding handler and responding to it
    • /responses provides builders for all interaction callback types
    • /webhook provides methods for sending follow-up messages and editing initial responses
    • /modal_inference is an experimental module to infer the types of submitted modals from those sent

    ⚠️There is no barrel file at the root.

    MIT © Adrien Simonnet