You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SocketInvite.cs 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using Discord.Rest;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Threading.Tasks;
  5. using Model = Discord.API.Gateway.InviteCreateEvent;
  6. namespace Discord.WebSocket
  7. {
  8. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  9. public class SocketInvite : SocketEntity<string>, IInviteMetadata
  10. {
  11. private long _createdAtTicks;
  12. /// <inheritdoc />
  13. public ulong ChannelId { get; private set; }
  14. /// <summary>
  15. /// Gets the channel where this invite was created.
  16. /// </summary>
  17. public SocketGuildChannel Channel { get; private set; }
  18. /// <inheritdoc />
  19. public ulong? GuildId { get; private set; }
  20. /// <summary>
  21. /// Gets the guild where this invite was created.
  22. /// </summary>
  23. public SocketGuild Guild { get; private set; }
  24. /// <inheritdoc />
  25. ChannelType IInvite.ChannelType
  26. {
  27. get
  28. {
  29. switch (Channel)
  30. {
  31. case IVoiceChannel voiceChannel: return ChannelType.Voice;
  32. case ICategoryChannel categoryChannel: return ChannelType.Category;
  33. case IDMChannel dmChannel: return ChannelType.DM;
  34. case IGroupChannel groupChannel: return ChannelType.Group;
  35. case INewsChannel newsChannel: return ChannelType.News;
  36. case ITextChannel textChannel: return ChannelType.Text;
  37. default: throw new InvalidOperationException("Invalid channel type.");
  38. }
  39. }
  40. }
  41. /// <inheritdoc />
  42. string IInvite.ChannelName => Channel.Name;
  43. /// <inheritdoc />
  44. string IInvite.GuildName => Guild.Name;
  45. /// <inheritdoc />
  46. int? IInvite.PresenceCount => throw new NotImplementedException();
  47. /// <inheritdoc />
  48. int? IInvite.MemberCount => throw new NotImplementedException();
  49. /// <inheritdoc />
  50. [Obsolete("This property doesn't exist anymore and shouldn't be used.")]
  51. bool IInviteMetadata.IsRevoked => throw new NotImplementedException();
  52. /// <inheritdoc />
  53. public bool IsTemporary { get; private set; }
  54. /// <inheritdoc />
  55. int? IInviteMetadata.MaxAge { get => MaxAge; }
  56. /// <inheritdoc />
  57. int? IInviteMetadata.MaxUses { get => MaxUses; }
  58. /// <inheritdoc />
  59. int? IInviteMetadata.Uses { get => Uses; }
  60. /// <summary>
  61. /// Gets the time (in seconds) until the invite expires.
  62. /// </summary>
  63. public int MaxAge { get; private set; }
  64. /// <summary>
  65. /// Gets the max number of uses this invite may have.
  66. /// </summary>
  67. public int MaxUses { get; private set; }
  68. /// <summary>
  69. /// Gets the number of times this invite has been used.
  70. /// </summary>
  71. public int Uses { get; private set; }
  72. /// <summary>
  73. /// Gets the user that created this invite if available.
  74. /// </summary>
  75. public SocketGuildUser Inviter { get; private set; }
  76. /// <inheritdoc />
  77. DateTimeOffset? IInviteMetadata.CreatedAt => DateTimeUtils.FromTicks(_createdAtTicks);
  78. /// <summary>
  79. /// Gets when this invite was created.
  80. /// </summary>
  81. public DateTimeOffset CreatedAt => DateTimeUtils.FromTicks(_createdAtTicks);
  82. /// <summary>
  83. /// Gets the user targeted by this invite if available.
  84. /// </summary>
  85. public SocketUser TargetUser { get; private set; }
  86. /// <summary>
  87. /// Gets the type of the user targeted by this invite.
  88. /// </summary>
  89. public TargetUserType TargetUserType { get; private set; }
  90. /// <inheritdoc />
  91. public string Code => Id;
  92. /// <inheritdoc />
  93. public string Url => $"{DiscordConfig.InviteUrl}{Code}";
  94. internal SocketInvite(DiscordSocketClient discord, SocketGuild guild, SocketGuildChannel channel, SocketGuildUser inviter, SocketUser target, string id)
  95. : base(discord, id)
  96. {
  97. Guild = guild;
  98. Channel = channel;
  99. Inviter = inviter;
  100. TargetUser = target;
  101. }
  102. internal static SocketInvite Create(DiscordSocketClient discord, SocketGuild guild, SocketGuildChannel channel, SocketGuildUser inviter, SocketUser target, Model model)
  103. {
  104. var entity = new SocketInvite(discord, guild, channel, inviter, target, model.Code);
  105. entity.Update(model);
  106. return entity;
  107. }
  108. internal void Update(Model model)
  109. {
  110. ChannelId = model.ChannelId;
  111. GuildId = model.GuildId.IsSpecified ? model.GuildId.Value : Guild.Id;
  112. IsTemporary = model.Temporary;
  113. MaxAge = model.MaxAge;
  114. MaxUses = model.MaxUses;
  115. Uses = model.Uses;
  116. _createdAtTicks = model.CreatedAt.UtcTicks;
  117. TargetUserType = model.TargetUserType.IsSpecified ? model.TargetUserType.Value : TargetUserType.Undefined;
  118. }
  119. /// <inheritdoc />
  120. public Task DeleteAsync(RequestOptions options = null)
  121. => InviteHelper.DeleteAsync(this, Discord, options);
  122. /// <summary>
  123. /// Gets the URL of the invite.
  124. /// </summary>
  125. /// <returns>
  126. /// A string that resolves to the Url of the invite.
  127. /// </returns>
  128. public override string ToString() => Url;
  129. private string DebuggerDisplay => $"{Url} ({Guild?.Name} / {Channel.Name})";
  130. /// <inheritdoc />
  131. IGuild IInvite.Guild => Guild;
  132. /// <inheritdoc />
  133. IChannel IInvite.Channel => Channel;
  134. /// <inheritdoc />
  135. IUser IInvite.Inviter => Inviter;
  136. /// <inheritdoc />
  137. IUser IInvite.TargetUser => TargetUser;
  138. }
  139. }