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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
use serde_json::Value; use std::default::Default; use internal::prelude::*; /// A builder to create a [`RichInvite`] for use via [`GuildChannel::create_invite`]. /// /// This is a structured and cleaner way of creating an invite, as all /// parameters are optional. /// /// # Examples /// /// Create an invite with a max age of 3600 seconds and 10 max uses: /// /// ```rust,no_run /// # use serenity::prelude::*; /// # use serenity::model::*; /// /// struct Handler; /// /// impl EventHandler for Handler { /// fn on_message(&self, _: Context, msg: Message) { /// use serenity::client::CACHE; /// if msg.content == "!createinvite" { /// let channel = match CACHE.read().unwrap().guild_channel(msg.channel_id) { /// Some(channel) => channel, /// None => { /// let _ = msg.channel_id.say("Error creating invite"); /// /// return; /// }, /// }; /// /// let reader = channel.read().unwrap(); /// /// let invite = match reader.create_invite(|i| i.max_age(3600).max_uses(10)) { /// Ok(invite) => invite, /// Err(why) => { /// println!("Err creating invite: {:?}", why); /// /// if let Err(why) = msg.channel_id.say("Error creating invite") { /// println!("Err sending err msg: {:?}", why); /// } /// /// return; /// }, /// }; /// /// drop(reader); /// /// let content = format!("Here's your invite: {}", invite.url()); /// let _ = msg.channel_id.say(&content); /// } /// } /// } /// let mut client = Client::new("token", Handler); client.start().unwrap(); /// ``` /// /// [`GuildChannel::create_invite`]: ../model/struct.GuildChannel.html#method.create_invite /// [`RichInvite`]: ../model/struct.Invite.html #[derive(Clone, Debug)] pub struct CreateInvite(pub JsonMap); impl CreateInvite { /// The duration that the invite will be valid for. /// /// Set to `0` for an invite which does not expire after an amount of time. /// /// Defaults to `86400`, or 24 hours. /// /// # Examples /// /// Create an invite with a max age of `3600` seconds, or 1 hour: /// /// ```rust,no_run /// # use serenity::client::CACHE; /// # use serenity::model::ChannelId; /// # use std::error::Error; /// # /// # fn try_main() -> Result<(), Box<Error>> { /// # let channel = CACHE.read().unwrap().guild_channel(81384788765712384).unwrap(); /// # let channel = channel.read().unwrap(); /// # /// let invite = channel.create_invite(|i| i.max_age(3600))?; /// # Ok(()) /// # } /// # /// # fn main() { /// # try_main().unwrap(); /// # } /// ``` pub fn max_age(mut self, max_age: u64) -> Self { self.0 .insert("max_age".to_owned(), Value::Number(Number::from(max_age))); self } /// The number of uses that the invite will be valid for. /// /// Set to `0` for an invite which does not expire after a number of uses. /// /// Defaults to `0`. /// /// # Examples /// /// Create an invite with a max use limit of `5`: /// /// ```rust,no_run /// # use serenity::client::CACHE; /// # use serenity::model::ChannelId; /// # use std::error::Error; /// # /// # fn try_main() -> Result<(), Box<Error>> { /// # let channel = CACHE.read().unwrap().guild_channel(81384788765712384).unwrap(); /// # let channel = channel.read().unwrap(); /// # /// let invite = channel.create_invite(|i| i.max_uses(5))?; /// # Ok(()) /// # } /// # /// # fn main() { /// # try_main().unwrap(); /// # } /// ``` pub fn max_uses(mut self, max_uses: u64) -> Self { self.0 .insert("max_uses".to_owned(), Value::Number(Number::from(max_uses))); self } /// Whether an invite grants a temporary membership. /// /// Defaults to `false`. /// /// # Examples /// /// Create an invite which is temporary: /// /// ```rust,no_run /// # use serenity::client::CACHE; /// # use serenity::model::ChannelId; /// # use std::error::Error; /// # /// # fn try_main() -> Result<(), Box<Error>> { /// # let channel = CACHE.read().unwrap().guild_channel(81384788765712384).unwrap(); /// # let channel = channel.read().unwrap(); /// # /// let invite = channel.create_invite(|i| i.temporary(true))?; /// # Ok(()) /// # } /// # /// # fn main() { /// # try_main().unwrap(); /// # } /// ``` pub fn temporary(mut self, temporary: bool) -> Self { self.0 .insert("temporary".to_owned(), Value::Bool(temporary)); self } /// Whether or not to try to reuse a similar invite. /// /// Defaults to `false`. /// /// # Examples /// /// Create an invite which is unique: /// /// ```rust,no_run /// # use serenity::client::CACHE; /// # use serenity::model::ChannelId; /// # use std::error::Error; /// # /// # fn try_main() -> Result<(), Box<Error>> { /// # let channel = CACHE.read().unwrap().guild_channel(81384788765712384).unwrap(); /// # let channel = channel.read().unwrap(); /// # /// let invite = channel.create_invite(|i| i.unique(true))?; /// # Ok(()) /// # } /// # /// # fn main() { /// # try_main().unwrap(); /// # } /// ``` pub fn unique(mut self, unique: bool) -> Self { self.0.insert("unique".to_owned(), Value::Bool(unique)); self } } impl Default for CreateInvite { /// Creates a builder with default values, setting `validate` to `null`. /// /// # Examples /// /// Create a default `CreateInvite` builder: /// /// ```rust /// use serenity::utils::builder::CreateInvite; /// /// let invite_builder = CreateInvite::default(); /// ``` fn default() -> CreateInvite { let mut map = Map::new(); map.insert("validate".to_owned(), Value::Null); CreateInvite(map) } }