discord_message/lib.rs
1//! `discord-message` is a crate containing the utilities needed to build Discord webhook messages from Rust.
2//!
3//! ## Example message creation
4//!
5//! ```
6//! fn main() {
7//!     let message = DiscordMessage {
8//!         username: Some("BotMan".to_string()),
9//!         content: "Text message. Up to 2000 characters.".to_string(),
10//!         embeds: vec![
11//!             Embed {
12//!                 title: "Title".to_string(),
13//!                 description: "Text message. You can use Markdown here.".to_string(),
14//!                 ..Default::default()
15//!             }
16//!         ],
17//!         ..Default::default()
18//!     };
19//!     let json = message.to_json().unwrap();
20//! }
21//! ```
22
23use serde::{Deserialize, Serialize};
24use url::Url;
25pub use serde_json::Error;
26
27/// Describes a field that can be used inside a message embed
28#[derive(Debug, Serialize, Deserialize, Default)]
29pub struct EmbedField {
30    /// Field title
31    #[serde(rename = "name")]
32    pub title: String,
33    /// Field value
34    pub value: String,
35    /// If true, the field will be displayed on the same line as the last
36    pub inline: bool,
37}
38
39/// Describes an embed author
40#[derive(Debug, Serialize, Deserialize, Default)]
41pub struct EmbedAuthor {
42    /// Author name
43    pub name: String,
44    /// URL of the author
45    pub url: Option<Url>,
46    /// Avatar URL for the author
47    pub icon_url: Option<Url>,
48}
49
50/// Describes an embed thumbnail
51#[derive(Debug, Serialize, Deserialize)]
52pub struct EmbedThumbnail {
53    /// Thumbnail URL
54    pub url: Url,
55}
56
57/// Describes an embed image
58#[derive(Debug, Serialize, Deserialize)]
59pub struct EmbedImage {
60    /// Image URL
61    pub url: Url,
62}
63
64/// Describes an embed footer
65#[derive(Debug, Serialize, Deserialize, Default)]
66pub struct EmbedFooter {
67    /// Footer text
68    pub text: String,
69    /// Footer icon URL
70    pub icon_url: Option<Url>,
71}
72
73/// Describes an embed
74#[derive(Debug, Serialize, Deserialize, Default)]
75pub struct Embed {
76    /// The title of the embed
77    pub title: String,
78    /// The description of the embed
79    pub description: String,
80    /// The color of the embed
81    pub color: Option<u32>,
82    /// The embed author
83    pub author: Option<EmbedAuthor>,
84    /// Possible fields
85    pub fields: Option<Vec<EmbedField>>,
86    /// The thumbnail of the embed
87    pub thumbnail: Option<EmbedThumbnail>,
88    /// The image of the embed
89    pub image: Option<EmbedImage>,
90    /// The footer of the embed
91    pub footer: Option<EmbedFooter>,
92}
93
94/// The main message type
95#[derive(Debug, Serialize, Deserialize, Default)]
96pub struct DiscordMessage {
97    /// Bot username override
98    pub username: Option<String>,
99    /// Bot avatar override
100    pub avatar_url: Option<Url>,
101    /// Message content
102    pub content: String,
103    /// Any possible embeds
104    pub embeds: Vec<Embed>,
105}
106
107impl DiscordMessage {
108    /// Generate JSON data
109    pub fn to_json(&self) -> serde_json::Result<String> {
110        serde_json::to_string(self)
111    }
112}