Skip to content

@tank/rust-discord-bot

1.1.0

Build high-performance Discord bots in Rust with serenity, poise, and twilight. Covers framework selection, slash/prefix commands, components, intents, event handling, state management, voice/audio (Songbird), performance, and deployment. Triggers: discord bot rust, serenity-rs, poise discord, twilight-rs, slash command rust, discord gateway, songbird voice, discord music bot, discord embed, discord button, poise framework, serenity event, discord sharding, discord sqlx


name: "@tank/rust-discord-bot" description: | Build high-performance Discord bots in Rust using serenity, poise, and twilight. Covers framework selection (serenity vs poise vs twilight), project scaffolding (workspace, Cargo.toml, feature flags), slash commands and prefix commands (poise macros, parameters, autocomplete, cooldowns, subcommands), message components (buttons, select menus, modals), embeds, gateway intents and event handling, state management and database integration (sqlx, DashMap, moka caching), voice and audio (Songbird), performance optimization (tokio tuning, sharding, zero-copy, Cargo release profiles), and deployment (Docker, systemd, tracing). Synthesizes serenity-rs v0.12.5 docs, poise v0.6.1 docs, twilight-rs v0.17.1 docs, Discord API reference (2026), and production patterns from Discord-TTS, robbb, Bathbot, and rustbot.

Trigger phrases: "discord bot rust", "rust discord", "serenity-rs", "serenity bot", "poise discord", "poise command", "twilight discord", "twilight-rs", "discord bot", "rust bot", "slash command rust", "discord gateway", "discord intents", "songbird voice", "discord music bot", "discord interaction rust", "discord embed rust", "discord button rust", "discord modal rust", "discord select menu", "poise framework", "serenity event handler", "discord sharding rust", "discord sqlx", "discord deployment rust", "discord bot docker", "discord bot systemd"

Rust Discord Bot Development

Build Discord bots in Rust for maximum performance and reliability. The ecosystem centers on three libraries: serenity (API wrapper), poise (command framework), and twilight (modular low-level). Most bots use poise + serenity.

Core Philosophy

  1. Poise is the default — Use poise + serenity for all new bots. Only drop to raw serenity for custom interaction flows. Only use twilight for large-scale multi-service architectures.
  2. Slash commands first — Prefer slash commands over prefix commands. Prefix commands require the privileged MESSAGE_CONTENT intent.
  3. Server owns state — Shared state lives in the Data struct, accessed via ctx.data(). Use DashMap for concurrent collections, sqlx for persistence, moka for TTL caching.
  4. Async everything — Never block the tokio runtime. Use spawn_blocking for CPU-heavy work. Use Semaphore to bound concurrent tasks.
  5. Intents are permissions — Only request gateway intents the bot needs. Privileged intents (GUILD_MEMBERS, MESSAGE_CONTENT, PRESENCES) require Discord verification at 100+ guilds.

Quick-Start

"I want to build a Discord bot from scratch"

StepActionReference
1Choose framework (poise recommended)references/framework-selection.md
2Scaffold project (Cargo.toml, main.rs, .env)references/project-setup.md
3Define commands with #[poise::command]references/commands-and-interactions.md
4Configure gateway intentsreferences/event-handling.md
5Add database and shared statereferences/state-and-data.md
6Optimize for productionreferences/performance-and-concurrency.md
7Deploy with Docker or systemdreferences/deployment-and-ops.md

"I need to add a slash command"

StepAction
1Write async fn with #[poise::command(slash_command)]
2Add parameters as typed function arguments
3Register in commands: vec![my_command()]
4Commands auto-register on startup via register_globally
-> See references/commands-and-interactions.md

"I want to add voice/music support"

StepAction
1Add songbird and symphonia to Cargo.toml
2Enable GUILD_VOICE_STATES intent
3Register Songbird with .register_songbird()
4Join channel, play audio with handler.enqueue_input()
-> See references/voice-and-audio.md

"I need buttons, select menus, or modals"

StepAction
1Build components with CreateButton, CreateSelectMenu, or CreateModal
2Send with ctx.send() including .components()
3Await interaction with .await_component_interaction()
4Respond within 3 seconds (or defer)
-> See references/commands-and-interactions.md

Decision Trees

Framework Selection

SignalUse
New bot, want to ship fastPoise + Serenity
Custom interaction flows, webhook-onlyRaw Serenity
10,000+ guilds, microservice architectureTwilight
Voice/music botPoise + Serenity + Songbird

Database Selection

ScaleUse
Prototype / small bot (<100 guilds)SQLite via sqlx
Production / growing botPostgreSQL via sqlx
No persistence neededIn-memory only (DashMap)

Intent Selection

FeatureRequired Intents
Slash commands onlyGatewayIntents::empty()
Basic guild eventsGatewayIntents::non_privileged()
Prefix commands+ MESSAGE_CONTENT (privileged)
Welcome messages+ GUILD_MEMBERS (privileged)
Voice bot+ GUILD_VOICE_STATES
Status tracking+ GUILD_PRESENCES (privileged)

Anti-Patterns

Don'tDo InsteadWhy
Use standard frameworkUse poiseStandard framework deprecated in v0.12.1
Request GatewayIntents::all()Request only needed intentsFails verification at 100+ guilds
Block the tokio runtimeUse spawn_blockingFreezes all event processing
Clone strings unnecessarilyBorrow &strAllocations hurt throughput
Use Arc<RwLock<HashMap>>Use DashMap3-5x faster for concurrent access
Skip error handling in on_errorHandle all FrameworkError variantsSilent failures in production
Register guild commands in prodUse register_globallyGuild commands are for development
Ignore rate limitsLet serenity handle themBot gets banned by Discord

Reference Files

FileContents
references/framework-selection.mdSerenity vs Poise vs Twilight decision matrix, Cargo.toml configs, feature flags, crate ecosystem, companion crates
references/project-setup.mdDirectory structure (simple and workspace), main.rs bootstrapping, command registration, configuration, .env handling
references/commands-and-interactions.mdPoise command macros, slash/prefix/context menu commands, parameter types, autocomplete, subcommands, permission checks, embeds, buttons, select menus, modals
references/event-handling.mdGateway intents (standard and privileged), poise event handler, FullEvent variants, raw serenity EventHandler, presence/activity, component interaction handling
references/state-and-data.mdData struct pattern, database integration (sqlx, PostgreSQL, SQLite), migrations, DashMap, moka caching, serenity cache, TypeMap, background tasks
references/performance-and-concurrency.mdTokio runtime tuning, memory optimization, sharding, concurrency patterns (Semaphore, channels, select), Cargo release profiles, SIMD JSON, gateway compression
references/voice-and-audio.mdSongbird setup, join/leave voice, audio playback (files, URLs, yt-dlp), queue management, voice events, volume control, voice receive
references/deployment-and-ops.mdDocker multi-stage builds, systemd services, structured logging (tracing), error handling strategies, graceful shutdown, cross-compilation, CI/CD, monitoring

Command Palette

Search skills, docs, and navigate Tank