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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
//! Models for server and channel invites. use chrono::{DateTime, FixedOffset}; use super::prelude::*; #[cfg(feature = "client")] use crate::client::Context; #[cfg(feature = "model")] use crate::builder::CreateInvite; #[cfg(feature = "model")] use crate::internal::prelude::*; #[cfg(all(feature = "cache", feature = "model"))] use super::{Permissions, utils as model_utils}; #[cfg(feature = "model")] use crate::utils; #[cfg(feature = "cache")] use crate::cache::CacheRwLock; #[cfg(feature = "http")] use crate::http::Http; /// Information about an invite code. /// /// Information can not be accessed for guilds the current user is banned from. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Invite { /// The approximate number of [`Member`]s in the related [`Guild`]. /// /// [`Guild`]: ../guild/struct.Guild.html /// [`Member`]: ../guild/struct.Member.html pub approximate_member_count: Option<u64>, /// The approximate number of [`Member`]s with an active session in the /// related [`Guild`]. /// /// An active session is defined as an open, heartbeating WebSocket connection. /// These include [invisible][`OnlineStatus::Invisible`] members. /// /// [`OnlineStatus::Invisible`]: ../user/enum.OnlineStatus.html#variant.Invisible /// [`Guild`]: ../guild/struct.Guild.html /// [`Member`]: ../guild/struct.Member.html pub approximate_presence_count: Option<u64>, /// The unique code for the invite. pub code: String, /// A representation of the minimal amount of information needed about the /// [`GuildChannel`] being invited to. /// /// [`GuildChannel`]: ../channel/struct.GuildChannel.html pub channel: InviteChannel, /// A representation of the minimal amount of information needed about the /// [`Guild`] being invited to. /// /// This can be `None` if the invite is to a [`Group`] and not to a /// Guild. /// /// [`Guild`]: ../guild/struct.Guild.html /// [`Group`]: ../channel/struct.Group.html pub guild: Option<InviteGuild>, } #[cfg(feature = "model")] impl Invite { /// Creates an invite for a [`GuildChannel`], providing a builder so that /// fields may optionally be set. /// /// See the documentation for the [`CreateInvite`] builder for information /// on how to use this and the default values that it provides. /// /// Requires the [Create Invite] permission. /// /// # Errors /// /// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] /// if the current user does not have the required [permission]. /// /// [`ModelError::InvalidPermissions`]: ../error/enum.Error.html#variant.InvalidPermissions /// [`CreateInvite`]: ../../builder/struct.CreateInvite.html /// [`GuildChannel`]: ../channel/struct.GuildChannel.html /// [Create Invite]: ../permissions/struct.Permissions.html#associatedconstant.CREATE_INVITE /// [permission]: ../permissions/index.html #[cfg(feature = "client")] pub fn create<C, F>(context: &Context, channel_id: C, f: F) -> Result<RichInvite> where C: Into<ChannelId>, F: FnOnce(CreateInvite) -> CreateInvite { Self::_create(&context, channel_id.into(), f) } #[cfg(feature = "client")] fn _create<F>(context: &Context, channel_id: ChannelId, f: F) -> Result<RichInvite> where F: FnOnce(CreateInvite) -> CreateInvite { #[cfg(feature = "cache")] { let req = Permissions::CREATE_INVITE; if !model_utils::user_has_perms(&context.cache, channel_id, req)? { return Err(Error::Model(ModelError::InvalidPermissions(req))); } } let map = utils::vecmap_to_json_map(f(CreateInvite::default()).0); context.http.create_invite(channel_id.0, &map) } /// Deletes the invite. /// /// **Note**: Requires the [Manage Guild] permission. /// /// # Errors /// /// If the `cache` is enabled, returns a [`ModelError::InvalidPermissions`] /// if the current user does not have the required [permission]. /// /// [`ModelError::InvalidPermissions`]: ../error/enum.Error.html#variant.InvalidPermissions /// [Manage Guild]: ../permissions/struct.Permissions.html#associatedconstant.MANAGE_GUILD /// [permission]: ../permissions/index.html #[cfg(feature = "client")] pub fn delete(&self, context: &Context) -> Result<Invite> { #[cfg(feature = "cache")] { let req = Permissions::MANAGE_GUILD; if !model_utils::user_has_perms(&context.cache, self.channel.id, req)? { return Err(Error::Model(ModelError::InvalidPermissions(req))); } } context.http.as_ref().delete_invite(&self.code) } /// Gets the information about an invite. #[cfg(feature = "http")] pub fn get(http: impl AsRef<Http>, code: &str, stats: bool) -> Result<Invite> { let mut invite = code; #[cfg(feature = "utils")] { invite = crate::utils::parse_invite(invite); } http.as_ref().get_invite(invite, stats) } /// Returns a URL to use for the invite. /// /// # Examples /// /// Retrieve the URL for an invite with the code `WxZumR`: /// /// ```rust /// # use serenity::model::prelude::*; /// # /// # let invite = Invite { /// # approximate_member_count: Some(1812), /// # approximate_presence_count: Some(717), /// # code: "WxZumR".to_string(), /// # channel: InviteChannel { /// # id: ChannelId(1), /// # name: "foo".to_string(), /// # kind: ChannelType::Text, /// # }, /// # guild: Some(InviteGuild { /// # id: GuildId(2), /// # icon: None, /// # name: "bar".to_string(), /// # splash_hash: None, /// # text_channel_count: Some(7), /// # voice_channel_count: Some(3), /// # }), /// # }; /// # /// assert_eq!(invite.url(), "https://discord.gg/WxZumR"); /// ``` pub fn url(&self) -> String { format!("https://discord.gg/{}", self.code) } } /// A minimal information about the channel an invite points to. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct InviteChannel { pub id: ChannelId, pub name: String, #[serde(rename = "type")] pub kind: ChannelType, } /// A minimal amount of information about the guild an invite points to. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct InviteGuild { pub id: GuildId, pub icon: Option<String>, pub name: String, pub splash_hash: Option<String>, pub text_channel_count: Option<u64>, pub voice_channel_count: Option<u64>, } #[cfg(feature = "model")] impl InviteGuild { /// Returns the Id of the shard associated with the guild. /// /// When the cache is enabled this will automatically retrieve the total /// number of shards. /// /// **Note**: When the cache is enabled, this function unlocks the cache to /// retrieve the total number of shards in use. If you already have the /// total, consider using [`utils::shard_id`]. /// /// [`utils::shard_id`]: ../../utils/fn.shard_id.html #[cfg(all(feature = "cache", feature = "utils"))] #[inline] pub fn shard_id(&self, cache: impl AsRef<CacheRwLock>) -> u64 { self.id.shard_id(&cache) } /// Returns the Id of the shard associated with the guild. /// /// When the cache is enabled this will automatically retrieve the total /// number of shards. /// /// When the cache is not enabled, the total number of shards being used /// will need to be passed. /// /// # Examples /// /// Retrieve the Id of the shard for a guild with Id `81384788765712384`, /// using 17 shards: /// /// ```rust,ignore /// use serenity::utils; /// /// // assumes a `guild` has already been bound /// /// assert_eq!(guild.shard_id(17), 7); /// ``` #[cfg(all(feature = "utils", not(feature = "cache")))] #[inline] pub fn shard_id(&self, shard_count: u64) -> u64 { self.id.shard_id(shard_count) } } /// Detailed information about an invite. /// This information can only be retrieved by anyone with the [Manage Guild] /// permission. Otherwise, a minimal amount of information can be retrieved via /// the [`Invite`] struct. /// /// [`Invite`]: struct.Invite.html /// [Manage Guild]: ../permissions/struct.Permissions.html#associatedconstant.MANAGE_GUILD #[derive(Clone, Debug, Deserialize, Serialize)] pub struct RichInvite { /// A representation of the minimal amount of information needed about the /// channel being invited to. pub channel: InviteChannel, /// The unique code for the invite. pub code: String, /// When the invite was created. pub created_at: DateTime<FixedOffset>, /// A representation of the minimal amount of information needed about the /// [`Guild`] being invited to. /// /// This can be `None` if the invite is to a [`Group`] and not to a /// Guild. /// /// [`Guild`]: ../guild/struct.Guild.html /// [`Group`]: ../channel/struct.Group.html pub guild: Option<InviteGuild>, /// The user that created the invite. pub inviter: User, /// The maximum age of the invite in seconds, from when it was created. pub max_age: u64, /// The maximum number of times that an invite may be used before it expires. /// Note that this does not supersede the [`max_age`] value, if the value of /// [`temporary`] is `true`. If the value of `temporary` is `false`, then the /// invite _will_ self-expire after the given number of max uses. /// If the value is `0`, then the invite is permanent. /// /// [`max_age`]: #structfield.max_age /// [`temporary`]: #structfield.temporary pub max_uses: u64, /// Indicator of whether the invite self-expires after a certain amount of /// time or uses. pub temporary: bool, /// The amount of times that an invite has been used. pub uses: u64, } #[cfg(feature = "model")] impl RichInvite { /// Deletes the invite. /// /// Refer to [`http::delete_invite`] for more information. /// /// **Note**: Requires the [Manage Guild] permission. /// /// # Errors /// /// If the `cache` feature is enabled, then this returns a /// [`ModelError::InvalidPermissions`] if the current user does not have /// the required [permission]. /// /// [`ModelError::InvalidPermissions`]: ../error/enum.Error.html#variant.InvalidPermissions /// [`Invite::delete`]: struct.Invite.html#method.delete /// [`http::delete_invite`]: ../../http/fn.delete_invite.html /// [Manage Guild]: ../permissions/struct.Permissions.html#associatedconstant.MANAGE_GUILD.html /// [permission]: ../permissions/index.html #[cfg(feature = "client")] pub fn delete(&self, context: &Context) -> Result<Invite> { #[cfg(feature = "cache")] { let req = Permissions::MANAGE_GUILD; if !model_utils::user_has_perms(&context.cache, self.channel.id, req)? { return Err(Error::Model(ModelError::InvalidPermissions(req))); } } context.http.as_ref().delete_invite(&self.code) } /// Returns a URL to use for the invite. /// /// # Examples /// /// Retrieve the URL for an invite with the code `WxZumR`: /// /// ```rust /// # use serenity::model::prelude::*; /// # /// # let invite = RichInvite { /// # code: "WxZumR".to_string(), /// # channel: InviteChannel { /// # id: ChannelId(1), /// # name: "foo".to_string(), /// # kind: ChannelType::Text, /// # }, /// # created_at: "2017-01-29T15:35:17.136000+00:00".parse().unwrap(), /// # guild: Some(InviteGuild { /// # id: GuildId(2), /// # icon: None, /// # name: "baz".to_string(), /// # splash_hash: None, /// # text_channel_count: None, /// # voice_channel_count: None, /// # }), /// # inviter: User { /// # avatar: None, /// # bot: false, /// # discriminator: 3, /// # id: UserId(4), /// # name: "qux".to_string(), /// # }, /// # max_age: 5, /// # max_uses: 6, /// # temporary: true, /// # uses: 7, /// # }; /// # /// assert_eq!(invite.url(), "https://discord.gg/WxZumR"); /// ``` pub fn url(&self) -> String { format!("https://discord.gg/{}", self.code) } }