Skip to content

@tank/rust-discord-bot

1.1.0

Description

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.

Triggered by

discord bot rustserenity-rspoise discordtwilight-rsslash command rustdiscord gateway
Download
Unsafe
tank install @tank/rust-discord-bot

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