Skip to content

Quick Start

Let’s build a small, useful Discord bot with Lunibee and Bun.

Terminal window
mkdir my-bot
cd my-bot
bun init
bun add lunibee

Create a .env file:

Terminal window
DISCORD_TOKEN="your-bot-token-here"

Never commit your real bot token to Git.

import { Client, IntentBits, command, option } from "lunibee";
const bot = new Client({
token: process.env.DISCORD_TOKEN!,
intents: [IntentBits.guilds, IntentBits.guildMessages, IntentBits.messageContent],
});
// One definition: runs as /ping and as !ping.
bot.commands.add(
command({
name: "ping",
description: "Check that the bot is alive",
prefix: true,
async run({ reply }) {
await reply("Pong! πŸ“");
},
}),
command({
name: "kick",
description: "Kick a member",
permissions: ["KickMembers"],
prefix: true,
options: {
user: option.user({ required: true }),
reason: option.string({ max: 500 }),
},
async run({ bot, options, guildId, reply }) {
// options.user is an id, options.reason is string | undefined
await bot.member(guildId!, options.user).kick(options.reason);
await reply(`Kicked <@${options.user}>.`);
},
}),
);
bot.once("ready", async (user) => {
console.log(`🐝 Logged in as ${user.username} (${user.id})`);
await bot.commands.deploy();
});
bot.commands.listen({ prefix: "!" });
await bot.login();

prefix: true needs the MessageContent intent (and GuildMessages or DirectMessages); bot.commands.listen({ prefix }) warns when one is missing.

Terminal window
bun run src/index.ts

During development:

Terminal window
bun --watch src/index.ts

Send !ping or run /ping in a server the bot is in.

Once the bot is running, these are the most useful next steps:

As the bot grows, start from a project that finds one file per command and event at runtime: bunx @lunibee/cli init my-bot (see the CLI). bunx lunibee create component button confirm scaffolds a component.

Reach Discord by ids, with one rule for every read: peek() is the cache, get() is the cache then Discord, fetch() is Discord.

const member = await bot.member(guildId, userId).get();
await bot.member(guildId, userId).timeout("10m", "spam");
await bot.channel(channelId).send("Hello!");
const channel = await bot.channel(channelId).get();
if (channel.is("text")) await channel.send({ content: "Typed as a text channel." });
await bot.api.guilds(guildId).members(userId).patch({ nick: "x" }, { reason: "rename" });

See Handles, Commands and bot.api.