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.

SocketGuildInvite.cs 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Discord.Rest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.Serialization.Formatters;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using InviteUpdate = Discord.API.Gateway.InviteCreatedEvent;
  9. namespace Discord.WebSocket
  10. {
  11. /// <summary>
  12. /// Represents a guild invite
  13. /// </summary>
  14. public class SocketGuildInvite : SocketEntity<string>, ISocketInvite
  15. {
  16. public string Code { get; private set; }
  17. public string Url => $"{DiscordConfig.InviteUrl}{Code}";
  18. public SocketGuildChannel Channel { get; private set; }
  19. public SocketGuild Guild { get; private set; }
  20. /// <summary>
  21. /// Gets the unique invite code
  22. /// <returns>
  23. /// Returns the unique invite code
  24. /// </returns>
  25. /// </summary>
  26. public string Id => Code;
  27. /// <summary>
  28. /// Gets the user who created the invite
  29. /// <returns>
  30. /// Returns the user who created the invite
  31. /// </returns>
  32. /// </summary>
  33. public SocketGuildUser Inviter { get; private set; }
  34. /// <summary>
  35. /// Gets the maximum number of times the invite can be used, if there is no limit then the value will be 0
  36. /// <returns>
  37. /// Returns the maximum number of times the invite can be used, if there is no limit then the value will be 0
  38. /// </returns>
  39. /// </summary>
  40. public int? MaxUses { get; private set; }
  41. /// <summary>
  42. /// Gets whether or not the invite is temporary (invited users will be kicked on disconnect unless they're assigned a role)
  43. /// <returns>
  44. /// Returns whether or not the invite is temporary (invited users will be kicked on disconnect unless they're assigned a role)
  45. /// </returns>
  46. /// </summary>
  47. public bool Temporary { get; private set; }
  48. /// <summary>
  49. /// Gets the time at which the invite was created
  50. /// <returns>
  51. /// Returns the time at which the invite was created
  52. /// </returns>
  53. /// </summary>
  54. public DateTimeOffset? CreatedAt { get; private set; }
  55. /// <summary>
  56. /// Gets how long the invite is valid for
  57. /// <returns>
  58. /// Returns how long the invite is valid for (in seconds)
  59. /// </returns>
  60. /// </summary>
  61. public TimeSpan? MaxAge { get; private set; }
  62. internal SocketGuildInvite(DiscordSocketClient _client, SocketGuild guild, SocketGuildChannel channel, string inviteCode, RestInviteMetadata rest) : base(_client, inviteCode)
  63. {
  64. Code = inviteCode;
  65. Guild = guild;
  66. Channel = channel;
  67. CreatedAt = rest.CreatedAt;
  68. Temporary = rest.IsTemporary;
  69. MaxUses = rest.MaxUses;
  70. Inviter = guild.GetUser(rest.Inviter.Id);
  71. if (rest.MaxAge.HasValue)
  72. MaxAge = TimeSpan.FromSeconds(rest.MaxAge.Value);
  73. }
  74. internal SocketGuildInvite(DiscordSocketClient _client, SocketGuild guild, SocketGuildChannel channel, string inviteCode, InviteUpdate Update) : base(_client, inviteCode)
  75. {
  76. Code = inviteCode;
  77. Guild = guild;
  78. Channel = channel;
  79. if (Update.RawTimestamp.IsSpecified)
  80. CreatedAt = Update.RawTimestamp.Value;
  81. else
  82. CreatedAt = DateTimeOffset.Now;
  83. if (Update.inviter.IsSpecified)
  84. Inviter = guild.GetUser(Update.inviter.Value.Id);
  85. Temporary = Update.TempInvite;
  86. MaxUses = Update.MaxUsers;
  87. MaxAge = TimeSpan.FromSeconds(Update.RawAge);
  88. }
  89. internal static SocketGuildInvite Create(DiscordSocketClient _client, SocketGuild guild, SocketGuildChannel channel, string inviteCode, InviteUpdate Update)
  90. {
  91. var invite = new SocketGuildInvite(_client, guild, channel, inviteCode, Update);
  92. return invite;
  93. }
  94. internal static SocketGuildInvite CreateFromRest(DiscordSocketClient _client, SocketGuild guild, SocketGuildChannel channel, string inviteCode, RestInviteMetadata rest)
  95. {
  96. var invite = new SocketGuildInvite(_client, guild, channel, inviteCode, rest);
  97. return invite;
  98. }
  99. /// <summary>
  100. /// Deletes the invite
  101. /// </summary>
  102. /// <param name="options"></param>
  103. /// <returns></returns>
  104. public Task DeleteAsync(RequestOptions options = null)
  105. => SocketInviteHelper.DeleteAsync(this, Discord, options);
  106. }
  107. }