disruption_types
Comprehensive type definitions for the Discord API and Gateway. This crate provides 180+ strongly-typed structures for all Discord entities, events, and gateway operations.
Part of the Disruption Discord API wrapper ecosystem.
Overview
disruption_types is the foundation of the Disruption ecosystem, providing type-safe representations of Discord's API structures. All types are serializable/deserializable with serde, making it easy to work with Discord's JSON API.
Features
- 180+ Type Definitions: Comprehensive coverage of Discord API structures
- Strongly Typed: Compile-time safety for all Discord entities
- Serde Integration: Full serialization/deserialization support
- Well Organized: Logical module structure matching Discord's API
- Zero Dependencies: Only requires serde and serde_json
- Lightweight: Minimal overhead, pure data structures
Type Coverage
Channels (Module: channel)
// Channel types
// Message types
// Embed types
// Reaction types
// Component types
Entities (Module: entities)
// User types
// Guild types
// Role types
// Emoji types
// Team types
// Interaction types
Gateway (Module: gateway)
// Gateway connection
// Intents for gateway connection
GUILDS
GUILD_MEMBERS
GUILD_MESSAGES
MESSAGE_CONTENT
DIRECT_MESSAGES
// ... and more
Opcodes (Module: opcodes)
Payloads (Module: payloads)
// Core payload structure
// Event payloads
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["derive"] }
= "1.0"
Usage Examples
Working with Messages
use MessageApiType;
use serde_json;
// Deserialize a message from Discord's API
let json = r#"{
"id": "123456789",
"content": "Hello, world!",
"author": {
"id": "987654321",
"username": "TestUser",
"discriminator": "1234",
"avatar": null
},
"channel_id": "555555555"
}"#;
let message: MessageApiType = from_str?;
println!;
println!;
Working with Guilds
use ;
// Deserialize guild data
let guild: GuildApiType = from_value?;
println!;
println!;
println!;
Working with Gateway Payloads
use ;
// Parse incoming gateway payload
let payload: Payload = from_str?;
match payload.op
Creating Gateway Payloads
use ;
// Create an IDENTIFY payload
let identify = Payload ;
// Serialize and send to gateway
let json = to_string?;
Working with Embeds
use ;
// Create a rich embed
let embed = EmbedApiType ;
Working with Intents
use Intents;
// Combine multiple intents using bitwise OR
let intents = GUILDS as u32
| GUILD_MESSAGES as u32
| MESSAGE_CONTENT as u32
| GUILD_MEMBERS as u32;
println!;
// Check if an intent is enabled
if has_intent
Module Structure
disruption_types/
├── channel/ # Channel-related types
│ ├── channel.rs # Channel structures
│ ├── thread.rs # Thread structures
│ ├── mention.rs # Mention structures
│ ├── overwrites.rs # Permission overwrites
│ └── message/ # Message-related types
│ ├── message.rs # Core message type
│ ├── embed.rs # Embed types
│ ├── attachment.rs # Attachment types
│ ├── reaction.rs # Reaction types
│ ├── component.rs # Component types
│ ├── reference.rs # Message reference
│ ├── activity.rs # Message activity
│ └── interaction.rs # Message interaction
├── entities/ # Entity types
│ ├── user.rs # User types
│ ├── guild.rs # Guild types (via mod.rs)
│ ├── role.rs # Role types
│ ├── emoji.rs # Emoji types
│ ├── teams.rs # Team types
│ └── interaction.rs # Interaction types
├── gateway/ # Gateway-specific types
│ └── gateway.rs # Intents and events
├── opcodes/ # Gateway opcodes
│ └── opcodes.rs # Opcode enum
└── payloads/ # Gateway payloads
├── hello.rs # HELLO payload
├── ready.rs # READY payload
├── identify.rs # IDENTIFY payload
├── resume.rs # RESUME payload
└── presence.rs # Presence payload
Type Naming Convention
All Discord API types follow the *ApiType naming convention to:
- Clearly indicate they represent Discord API structures
- Avoid naming conflicts with user code
- Maintain consistency across the crate
Examples:
MessageApiType- A Discord messageUserApiType- A Discord userGuildApiType- A Discord guildChannelApiType- A Discord channel
Serde Attributes
Types use serde attributes for proper JSON handling:
Common patterns:
#[serde(skip_serializing_if = "Option::is_none")]- Don't includenullfields#[serde(default)]- Use default value if field is missing#[serde(rename = "different_name")]- Map to different JSON field name
Events
All Discord gateway events are defined in the Event enum:
Convert from string:
use Event;
let event = try_from?;
Default Implementations
Many types implement Default for easy construction:
use MessageApiType;
let message = MessageApiType ;
When to Use
Use disruption_types directly if:
- You're building a custom Discord client
- You need to work with raw API payloads
- You're implementing your own gateway or REST client
- You need type definitions without the full library overhead
Use the main disruption crate if:
- You're building a Discord bot (recommended)
- You want high-level helpers and abstractions
- You prefer the event-driven Handler trait
Zero-Cost Abstractions
All types are simple structs with no runtime overhead:
- No vtables or dynamic dispatch
- No hidden allocations
- Direct field access
- Inlined by default
// This compiles to just field access
let username = user.username.as_str;
let message_content = message.content.as_str;
Contributing
When adding new types:
- Follow the
*ApiTypenaming convention - Add appropriate serde attributes
- Implement
Debug,Clonewhere appropriate - Use
Option<T>for nullable fields - Use
Vec<T>for array fields - Document with Discord API reference links
Discord API Version
This crate targets Discord API v10.
Changes between API versions are handled through careful versioning and documentation.
Dependencies
Minimal dependencies:
- serde: Serialization framework
- serde_json: JSON support
- serde_repr: Enum representation
License
This project is licensed under the MIT License - see the LICENSE file for details.
Links
- Main Crate: disruption
- Gateway Crate: disruption_gateway
- Repository: https://github.com/H1ghBre4k3r/disruption
- Discord API Documentation: https://discord.com/developers/docs/intro
- Discord API Reference: https://discord.com/developers/docs/reference
Part of the Disruption ecosystem | Built with ❤️ in Rust