@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.
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.
The package exports the following modules:
⚠️There is no barrel file at the root.
MIT © Adrien Simonnet