Next.js Discord Bot
Slash commands served from Next.js route handlers. Deploy it, register your commands, then add the bot to a server.
Build a Discord Bot with Next.js
This boilerplate turns a Next.js app into a Discord bot that answers slash commands over HTTP. You write commands in TypeScript; it handles signature verification, dispatch and command registration.
No Server to Keep Running
Discord sends each slash command to a Next.js route handler as an HTTP request, and the bot replies in the response. There is no gateway connection and no process to keep alive.
Deploys Free on Vercel
It is a regular Next.js app, so it runs on Vercel’s free tier or any host that runs Next.js route handlers.
One File per Command
Each slash command is a single TypeScript file built with
defineCommand. Add it to the command list and register it.Verified Requests
The bot checks every request against your application’s Ed25519 public key before any command runs, and the bot token never reaches the browser.
How It Works
Deploy
Clone the repository, copy the application ID, public key and bot token from the Discord Developer Portal into your environment variables, choose a register commands key, and deploy.
Set the Interactions Endpoint URL
In the Developer Portal, set it to
https://your-domain/api/discord-bot/interactions. Discord sends a signed ping to check it before saving.Register Your Commands
Enter your register commands key in the form above, or run
npm run register. Discord only shows commands once you register them.Add the Bot to a Server
Use the Add to Server button. Your slash commands are ready to use.
Add a Slash Command
Create a file in src/bot/commands/, add it to the list in src/bot/commands/index.ts, then register your commands again.
import { SlashCommandBuilder } from "@discordjs/builders";
import { InteractionResponseType } from "discord-api-types/v10";
import { defineCommand } from "@/bot/define-command";
export default defineCommand({
data: new SlashCommandBuilder()
.setName("ping")
.setDescription("Checks that the bot is online"),
execute: () => ({
type: InteractionResponseType.ChannelMessageWithSource,
data: { content: "pong!" },
}),
});Questions
- Does it need a server running 24/7?
- No. The bot only runs when Discord sends it a slash command, so a serverless host is enough and there is nothing to keep online.
- What can it handle?
- Slash commands. Reading every message in a channel or joining voice needs a persistent gateway connection, which this HTTP-only design deliberately avoids.
- How fast does a command have to reply?
- Discord expects a reply within 3 seconds, so keep each command’s work short.
- Is it free to use?
- Yes. The code is MIT licensed, and it runs within Vercel’s free tier.