using Discord.Rest; using System; using System.Diagnostics; using System.Threading.Tasks; using Model = Discord.API.Gateway.InviteCreateEvent; namespace Discord.WebSocket { [DebuggerDisplay(@"{DebuggerDisplay,nq}")] public class SocketInvite : SocketEntity, IInviteMetadata { private long _createdAtTicks; /// public ulong ChannelId { get; private set; } /// /// Gets the channel where this invite was created. /// public SocketGuildChannel Channel { get; private set; } /// public ulong? GuildId { get; private set; } /// /// Gets the guild where this invite was created. /// public SocketGuild Guild { get; private set; } /// ChannelType IInvite.ChannelType { get { switch (Channel) { case IVoiceChannel voiceChannel: return ChannelType.Voice; case ICategoryChannel categoryChannel: return ChannelType.Category; case IDMChannel dmChannel: return ChannelType.DM; case IGroupChannel groupChannel: return ChannelType.Group; case INewsChannel newsChannel: return ChannelType.News; case ITextChannel textChannel: return ChannelType.Text; default: throw new InvalidOperationException("Invalid channel type."); } } } /// string IInvite.ChannelName => Channel.Name; /// string IInvite.GuildName => Guild.Name; /// int? IInvite.PresenceCount => throw new NotImplementedException(); /// int? IInvite.MemberCount => throw new NotImplementedException(); /// [Obsolete("This property doesn't exist anymore and shouldn't be used.")] bool IInviteMetadata.IsRevoked => throw new NotImplementedException(); /// public bool IsTemporary { get; private set; } /// int? IInviteMetadata.MaxAge { get => MaxAge; } /// int? IInviteMetadata.MaxUses { get => MaxUses; } /// int? IInviteMetadata.Uses { get => Uses; } /// /// Gets the time (in seconds) until the invite expires. /// public int MaxAge { get; private set; } /// /// Gets the max number of uses this invite may have. /// public int MaxUses { get; private set; } /// /// Gets the number of times this invite has been used. /// public int Uses { get; private set; } /// /// Gets the user that created this invite if available. /// public SocketGuildUser Inviter { get; private set; } /// DateTimeOffset? IInviteMetadata.CreatedAt => DateTimeUtils.FromTicks(_createdAtTicks); /// /// Gets when this invite was created. /// public DateTimeOffset CreatedAt => DateTimeUtils.FromTicks(_createdAtTicks); /// /// Gets the user targeted by this invite if available. /// public SocketUser TargetUser { get; private set; } /// /// Gets the type of the user targeted by this invite. /// public TargetUserType TargetUserType { get; private set; } /// public string Code => Id; /// public string Url => $"{DiscordConfig.InviteUrl}{Code}"; internal SocketInvite(DiscordSocketClient discord, SocketGuild guild, SocketGuildChannel channel, SocketGuildUser inviter, SocketUser target, string id) : base(discord, id) { Guild = guild; Channel = channel; Inviter = inviter; TargetUser = target; } internal static SocketInvite Create(DiscordSocketClient discord, SocketGuild guild, SocketGuildChannel channel, SocketGuildUser inviter, SocketUser target, Model model) { var entity = new SocketInvite(discord, guild, channel, inviter, target, model.Code); entity.Update(model); return entity; } internal void Update(Model model) { ChannelId = model.ChannelId; GuildId = model.GuildId.IsSpecified ? model.GuildId.Value : Guild.Id; IsTemporary = model.Temporary; MaxAge = model.MaxAge; MaxUses = model.MaxUses; Uses = model.Uses; _createdAtTicks = model.CreatedAt.UtcTicks; TargetUserType = model.TargetUserType.IsSpecified ? model.TargetUserType.Value : TargetUserType.Undefined; } /// public Task DeleteAsync(RequestOptions options = null) => InviteHelper.DeleteAsync(this, Discord, options); /// /// Gets the URL of the invite. /// /// /// A string that resolves to the Url of the invite. /// public override string ToString() => Url; private string DebuggerDisplay => $"{Url} ({Guild?.Name} / {Channel.Name})"; /// IGuild IInvite.Guild => Guild; /// IChannel IInvite.Channel => Channel; /// IUser IInvite.Inviter => Inviter; /// IUser IInvite.TargetUser => TargetUser; } }