A NextJS serverless-compatible library for creating Discord bots. Uses file based routing with templates.
You need to enable Interactions Endpoint URL under Selected App > General Information > Interactions Endpoint URL in your Discord Application. Enter the endpoint you selected in your API Routes (example: https://example.com/api/discord).
For local development, you can use Ngrok
Call addDiscordCompilation in your next config
import type { NextConfig } from "next";
import { addDiscordCompilation } from "next-discord-bot";
const config: NextConfig = {};
export default addDiscordCompilation(config, {
postCommands: true,
});Initialize a DiscordImporter in your instrumentation hook
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
const { DiscordImporter } = await import("next-discord-bot");
return DiscordImporter.init();
}
}Pick an API Route to handle Discord requests (example: src/app/api/discord/route.ts)
import DiscordClient from "next-discord-bot";
export const POST = new DiscordClient().server;src/discordcommands- [root command]
command.ts- [subcommand]
command.ts
- [subcommand group]
command.ts- [subcommand]
command.ts
Commands/Subcommands are named after their folder.
Each command has a root command.ts file exporting SlashRootCommandBuilder.
As per Discord API, each command can have subcommands with depth of up to two levels. Subcommands and Subcommand Groups are interchangable, they export SlashSubCommandBuilder.
Structure for your commands. SlashRootCommandBuilder defines all metadata for a command.
Each CommandBuilder can define an #execute method, they trigger from root to child and pass return values to each other.
Note: Only the lowest defined command can be invoked by itself by a user. More Info
import {
SlashRootCommandBuilder,
PermissionsField,
BitwisePermission
} from "next-discord-bot";
export default new SlashRootCommandBuilder({
description: "Ping command",
default_member_permissions: new PermissionFlagField(
BitwisePermission.ADMINISTRATOR,
),
execute: async (interaction) => {
const time = Date.now() - interaction.createdAt.getTime();
await interaction.reply({
content: `Ping! \`${time}ms\``,
ephemeral: true,
});
},
});import { SlashSubCommandBuilder } from "next-discord-bot";
function rollDice() {
return Math.floor(Math.random() * 6) + 1;
}
export default new SlashSubCommandBuilder({
description: "Roll a dice",
execute: async (interaction) => {
const roll = rollDice();
await interaction.reply(`🎲 You rolled **${roll}**`);
},
});- Library
- Separate routing and compilation
- Watchmode
- Compiled type-safety
- Slash Commands
- Slash Command Builders
- Structure
- Execution
- Options
- Slash Command Interactions
- Autocomplete
- Slash Command Builders
- User Commands
- User Command Interactions
- User Command Builders
- Message Commands
- Message Command Interactions
- Message Command Builders
- Components
- Component Interactions
- Component Builders
- Modals
- Modal Interactions
- Modal Builders