Quick Start
Letβs build a small, useful Discord bot with Lunibee and Bun.
1. Create the project
Section titled β1. Create the projectβmkdir my-botcd my-botbun initbun add lunibee2. Add your token
Section titled β2. Add your tokenβCreate a .env file:
DISCORD_TOKEN="your-bot-token-here"Never commit your real bot token to Git.
3. Create src/index.ts
Section titled β3. Create src/index.tsβ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.
4. Run it
Section titled β4. Run itβbun run src/index.tsDuring development:
bun --watch src/index.tsSend !ping or run /ping in a server the bot is in.
What next?
Section titled βWhat next?β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" });