use super::prelude::*;
#[cfg(feature = "model")]
use crate::builder::CreateInvite;
#[cfg(all(feature = "cache", feature = "model"))]
use crate::cache::Cache;
#[cfg(feature = "model")]
use crate::http::{CacheHttp, Http};
#[cfg(feature = "model")]
use crate::internal::prelude::*;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct Invite {
pub approximate_member_count: Option<u64>,
pub approximate_presence_count: Option<u64>,
pub code: String,
pub channel: InviteChannel,
pub guild: Option<InviteGuild>,
pub inviter: Option<User>,
pub target_type: Option<InviteTargetType>,
pub target_user: Option<UserId>,
pub target_application: Option<ApplicationId>,
pub expires_at: Option<Timestamp>,
pub stage_instance: Option<InviteStageInstance>,
#[serde(rename = "guild_scheduled_event")]
pub scheduled_event: Option<ScheduledEvent>,
}
#[cfg(feature = "model")]
impl Invite {
#[inline]
pub async fn create(
cache_http: impl CacheHttp,
channel_id: impl Into<ChannelId>,
builder: CreateInvite<'_>,
) -> Result<RichInvite> {
channel_id.into().create_invite(cache_http, builder).await
}
pub async fn delete(&self, cache_http: impl CacheHttp) -> Result<Invite> {
#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {
crate::utils::user_has_perms_cache(
cache,
self.channel.id,
Permissions::MANAGE_GUILD,
)?;
}
}
cache_http.http().as_ref().delete_invite(&self.code, None).await
}
pub async fn get(
http: impl AsRef<Http>,
code: &str,
member_counts: bool,
expiration: bool,
event_id: Option<ScheduledEventId>,
) -> Result<Invite> {
let mut invite = code;
#[cfg(feature = "utils")]
{
invite = crate::utils::parse_invite(invite);
}
http.as_ref().get_invite(invite, member_counts, expiration, event_id).await
}
#[must_use]
pub fn url(&self) -> String {
format!("https://discord.gg/{}", self.code)
}
}
#[non_exhaustive]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct InviteChannel {
pub id: ChannelId,
pub name: String,
#[serde(rename = "type")]
pub kind: ChannelType,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct InviteGuild {
pub id: GuildId,
pub name: String,
pub splash: Option<ImageHash>,
pub banner: Option<ImageHash>,
pub description: Option<String>,
pub icon: Option<ImageHash>,
pub features: Vec<String>,
pub verification_level: VerificationLevel,
pub vanity_url_code: Option<String>,
pub nsfw_level: NsfwLevel,
pub premium_subscription_count: Option<u64>,
}
#[cfg(feature = "model")]
impl InviteGuild {
#[cfg(all(feature = "cache", feature = "utils"))]
#[inline]
#[must_use]
pub fn shard_id(&self, cache: impl AsRef<Cache>) -> u32 {
self.id.shard_id(&cache)
}
#[cfg(all(feature = "utils", not(feature = "cache")))]
#[inline]
#[must_use]
pub fn shard_id(&self, shard_count: u32) -> u32 {
self.id.shard_id(shard_count)
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct RichInvite {
pub channel: InviteChannel,
pub code: String,
pub created_at: Timestamp,
pub guild: Option<InviteGuild>,
pub inviter: Option<User>,
pub max_age: u32,
pub max_uses: u8,
pub temporary: bool,
pub uses: u64,
}
#[cfg(feature = "model")]
impl RichInvite {
pub async fn delete(&self, cache_http: impl CacheHttp) -> Result<Invite> {
#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {
crate::utils::user_has_perms_cache(
cache,
self.channel.id,
Permissions::MANAGE_GUILD,
)?;
}
}
cache_http.http().as_ref().delete_invite(&self.code, None).await
}
#[must_use]
pub fn url(&self) -> String {
format!("https://discord.gg/{}", self.code)
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct InviteStageInstance {
pub members: Vec<PartialMember>,
pub participant_count: u64,
pub speaker_count: u64,
pub topic: String,
}
enum_number! {
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[serde(from = "u8", into = "u8")]
#[non_exhaustive]
pub enum InviteTargetType {
Stream = 1,
EmbeddedApplication = 2,
_ => Unknown(u8),
}
}