skills/bun-guides-ecosystem-discordjs/SKILL.md
Create a Discord bot
npx skillsauth add jarle/bun-skills Bun Create a Discord botInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Discord.js works out of the box with Bun. Let's write a simple bot. First create a directory and initialize it with bun init.
mkdir my-bot
cd my-bot
bun init
Now install Discord.js.
bun add discord.js
Before we go further, we need to go to the Discord developer portal, login/signup, create a new Application, then create a new Bot within that application. Follow the official guide for step-by-step instructions.
Once complete, you'll be presented with your bot's private key. Let's add this to a file called .env.local. Bun automatically reads this file and loads it into process.env.
<Note>This is an example token that has already been invalidated.</Note>
DISCORD_TOKEN=YOUR_BOT_TOKEN_HERE
Be sure to add .env.local to your .gitignore! It is dangerous to check your bot's private key into version control.
node_modules
.env.local
Now let's actually write our bot in a new file called bot.ts.
// import discord.js
import { Client, Events, GatewayIntentBits } from "discord.js";
// create a new Client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
// listen for the client to be ready
client.once(Events.ClientReady, c => {
console.log(`Ready! Logged in as ${c.user.tag}`);
});
// login with the token from .env.local
client.login(process.env.DISCORD_TOKEN);
Now we can run our bot with bun run. It may take a several seconds for the client to initialize the first time you run the file.
bun run bot.ts
Ready! Logged in as my-bot#1234
You're up and running with a bare-bones Discord.js bot! This is a basic guide to setting up your bot with Bun; we recommend the official discord.js docs for complete information on the discord.js API.
development
Using TypeScript with Bun, including type definitions and compiler options
development
Learn how to write tests using Bun's Jest-compatible API with support for async tests, timeouts, and various test modifiers
testing
Learn how to use snapshot testing in Bun to save and compare output between test runs
testing
Learn about Bun test's runtime integration, environment variables, timeouts, and error handling