1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! `discord-message` is a crate containing the utilities needed to build Discord webhook messages from Rust.
//!
//! ## Example message creation
//!
//! ```
//! fn main() {
//!     let message = DiscordMessage {
//!         username: Some("BotMan".to_string()),
//!         content: "Text message. Up to 2000 characters.".to_string(),
//!         embeds: vec![
//!             Embed {
//!                 title: "Title".to_string(),
//!                 description: "Text message. You can use Markdown here.".to_string(),
//!                 ..Default::default()
//!             }
//!         ],
//!         ..Default::default()
//!     };
//!     let json = message.to_json().unwrap();
//! }
//! ```

use serde::{Deserialize, Serialize};
use url::Url;
pub use serde_json::Error;

/// Describes a field that can be used inside a message embed
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct EmbedField {
    /// Field title
    #[serde(rename = "name")]
    pub title: String,
    /// Field value
    pub value: String,
    /// If true, the field will be displayed on the same line as the last
    pub inline: bool,
}

/// Describes an embed author
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct EmbedAuthor {
    /// Author name
    pub name: String,
    /// URL of the author
    pub url: Option<Url>,
    /// Avatar URL for the author
    pub icon_url: Option<Url>,
}

/// Describes an embed thumbnail
#[derive(Debug, Serialize, Deserialize)]
pub struct EmbedThumbnail {
    /// Thumbnail URL
    pub url: Url,
}

/// Describes an embed image
#[derive(Debug, Serialize, Deserialize)]
pub struct EmbedImage {
    /// Image URL
    pub url: Url,
}

/// Describes an embed footer
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct EmbedFooter {
    /// Footer text
    pub text: String,
    /// Footer icon URL
    pub icon_url: Option<Url>,
}

/// Describes an embed
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct Embed {
    /// The title of the embed
    pub title: String,
    /// The description of the embed
    pub description: String,
    /// The color of the embed
    pub color: Option<u32>,
    /// The embed author
    pub author: Option<EmbedAuthor>,
    /// Possible fields
    pub fields: Option<Vec<EmbedField>>,
    /// The thumbnail of the embed
    pub thumbnail: Option<EmbedThumbnail>,
    /// The image of the embed
    pub image: Option<EmbedImage>,
    /// The footer of the embed
    pub footer: Option<EmbedFooter>,
}

/// The main message type
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct DiscordMessage {
    /// Bot username override
    pub username: Option<String>,
    /// Bot avatar override
    pub avatar_url: Option<Url>,
    /// Message content
    pub content: String,
    /// Any possible embeds
    pub embeds: Vec<Embed>,
}

impl DiscordMessage {
    /// Generate JSON data
    pub fn to_json(&self) -> serde_json::Result<String> {
        serde_json::to_string(self)
    }
}