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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#![warn(missing_docs)]
//! Rusty-interaction is a library that allows you to work with Discord's new [Interactions](https://blog.discord.com/slash-commands-are-here-8db0a385d9e6).
//! It can expose types and provides helper functions to validate your Interactions.
//! It can optionally provide a handler that allows you to receive interactions via outgoing webhook.
//!
//! Note that Discord also has [official documentation](https://discord.com/developers/docs/intro).
//!
//! ## Examples
//! See the [`examples`](https://github.com/hugopilot/rusty-interaction/tree/main/examples`) directory.

#[macro_use]
mod macros;

#[allow(dead_code)]
const BASE_URL: &str = "https://discord.com/api/v9";

#[cfg(feature = "types")]
/// Exposes useful data models
pub mod types;

/// Provides a helper function to validate Discord interactions.
#[cfg(feature = "security")]
pub mod security;

/// Provides an entire handler to handle Discord interactions.
#[cfg(any(feature = "handler", feature = "extended-handler"))]
#[cfg_attr(docsrs, doc(cfg(feature = "handler")))]
pub mod handler;
#[cfg(any(feature = "handler", feature = "extended-handler"))]
#[cfg_attr(docsrs, doc(cfg(feature = "handler")))]
pub use actix;

#[cfg(any(feature = "handler", feature = "extended-handler"))]
#[cfg_attr(docsrs, doc(cfg(feature = "handler")))]
pub use log;

#[cfg(any(feature = "handler", feature = "extended-handler"))]
#[cfg_attr(docsrs, doc(cfg(feature = "handler")))]
pub use attributes::*;

#[cfg(all(test, feature = "handler"))]
mod tests;

// ===== USEFUL MACROS =====
#[macro_export]
#[doc(hidden)]
macro_rules! expect_successful_api_response {
    ($response:ident, $succret:expr) => {
        match $response {
            Err(e) => {
                debug!("Discord API request failed: {:#?}", e);
                Err(HttpError {
                    code: 0,
                    message: format!("{:#?}", e),
                })
            }
            Ok(r) => {
                let st = r.status();
                if !st.is_success() {
                    let e = format!("{:#?}", r.text().await);
                    debug!("Discord API returned an error: {:#?}", e);
                    Err(HttpError {
                        code: st.as_u16(),
                        message: e,
                    })
                } else {
                    $succret
                }
            }
        }
    };
}
#[macro_export]
#[doc(hidden)]
macro_rules! expect_specific_api_response {
    ($response:ident, $expres:expr, $succret:expr) => {
        match $response {
            Err(e) => {
                debug!("Discord API request failed: {:#?}", e);

                Err(HttpError {
                    code: 0,
                    message: format!("{:#?}", e),
                })
            }
            Ok(r) => {
                let st = r.status();
                if st != $expres {
                    let e = format!("{:#?}", r.text().await);
                    debug!("Discord API returned an error: {:#?}", e);
                    Err(HttpError {
                        code: st.as_u16(),
                        message: e,
                    })
                } else {
                    $succret
                }
            }
        }
    };
}

#[macro_export]
#[doc(hidden)]
macro_rules! expect_successful_api_response_and_return {
    ($response:ident, $struc:ident, $retval:ident, $succret:expr) => {
        match $response {
            Err(e) => {
                debug!("Discord API request failed: {:#?}", e);
                Err(HttpError {
                    code: 0,
                    message: format!("{:#?}", e),
                })
            }
            Ok(r) => {
                let st = r.status();
                if !st.is_success() {
                    let e = format!("{:#?}", r.text().await);
                    debug!("Discord API returned an error: {:#?}", e);
                    Err(HttpError {
                        code: st.as_u16(),
                        message: e,
                    })
                } else {
                    let a: Result<$struc, serde_json::Error> =
                        serde_json::from_str(&r.text().await.unwrap());

                    match a {
                        Err(e) => {
                            debug!("Failed to decode response: {:#?}", e);
                            Err(HttpError {
                                code: 500,
                                message: format!("{:?}", e),
                            })
                        }
                        Ok($retval) => $succret,
                    }
                }
            }
        }
    };
}