use super::prelude::*;
#[cfg(all(feature = "cache", feature = "model"))]
use super::{utils as model_utils, Permissions};
#[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::*;
#[cfg(feature = "model")]
use crate::json;
use crate::model::Timestamp;
#[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>,
}
#[cfg(feature = "model")]
impl Invite {
#[inline]
pub async fn create<F>(
cache_http: impl CacheHttp,
channel_id: impl Into<ChannelId>,
f: F,
) -> Result<RichInvite>
where
F: FnOnce(CreateInvite) -> CreateInvite,
{
let channel_id = channel_id.into();
#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {
model_utils::user_has_perms_cache(
cache,
channel_id,
None,
Permissions::CREATE_INSTANT_INVITE,
)?;
}
}
let map = json::hashmap_to_json_map(f(CreateInvite::default()).0);
cache_http.http().create_invite(channel_id.0, &map, None).await
}
pub async fn delete(&self, cache_http: impl CacheHttp) -> Result<Invite> {
#[cfg(feature = "cache")]
{
if let Some(cache) = cache_http.cache() {
let guild_id = self.guild.as_ref().map(|g| g.id);
model_utils::user_has_perms_cache(
cache,
self.channel.id,
guild_id,
Permissions::MANAGE_GUILD,
)?;
}
}
cache_http.http().as_ref().delete_invite(&self.code).await
}
pub async fn get(
http: impl AsRef<Http>,
code: &str,
member_counts: bool,
expiration: bool,
event_id: Option<u64>,
) -> 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 icon: Option<String>,
pub name: String,
pub splash: Option<String>,
pub text_channel_count: Option<u64>,
pub voice_channel_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>) -> u64 {
self.id.shard_id(&cache)
}
#[cfg(all(feature = "utils", not(feature = "cache")))]
#[inline]
#[must_use]
pub fn shard_id(&self, shard_count: u64) -> u64 {
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: u64,
pub max_uses: u64,
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() {
let guild_id = self.guild.as_ref().map(|g| g.id);
model_utils::user_has_perms_cache(
cache,
self.channel.id,
guild_id,
Permissions::MANAGE_GUILD,
)?;
}
}
cache_http.http().as_ref().delete_invite(&self.code).await
}
#[must_use]
pub fn url(&self) -> String {
format!("https://discord.gg/{}", self.code)
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct InviteStageInstance {
members: Vec<PartialMember>,
participant_count: u64,
speaker_count: u64,
topic: String,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum InviteTargetType {
Normal = 0,
Stream = 1,
EmmbeddedApplication = 2,
Unknown = !0,
}
enum_number!(InviteTargetType {
Normal,
Stream,
EmmbeddedApplication
});