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.

RestInvite.cs 3.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading.Tasks;
  4. using Discord.API.Rest;
  5. using Model = Discord.API.Invite;
  6. namespace Discord.Rest
  7. {
  8. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  9. public class RestInvite : RestEntity<string>, IInvite, IUpdateable
  10. {
  11. public string ChannelName { get; private set; }
  12. public string GuildName { get; private set; }
  13. public int? PresenceCount { get; private set; }
  14. public int? MemberCount { get; private set; }
  15. public ulong ChannelId { get; private set; }
  16. public ulong? GuildId { get; private set; }
  17. internal IChannel Channel { get; private set; }
  18. internal IGuild Guild { get; private set; }
  19. public string Code => Id;
  20. public string Url => $"{DiscordConfig.InviteUrl}{Code}";
  21. internal RestInvite(BaseDiscordClient discord, IGuild guild, IChannel channel, string id)
  22. : base(discord, id)
  23. {
  24. Guild = guild;
  25. Channel = channel;
  26. }
  27. internal static RestInvite Create(BaseDiscordClient discord, IGuild guild, IChannel channel, Model model)
  28. {
  29. var entity = new RestInvite(discord, guild, channel, model.Code);
  30. entity.Update(model);
  31. return entity;
  32. }
  33. internal void Update(Model model)
  34. {
  35. GuildId = model.Guild.IsSpecified ? model.Guild.Value.Id : default(ulong?);
  36. ChannelId = model.Channel.Id;
  37. GuildName = model.Guild.IsSpecified ? model.Guild.Value.Name : null;
  38. ChannelName = model.Channel.Name;
  39. MemberCount = model.MemberCount.IsSpecified ? model.MemberCount.Value : null;
  40. PresenceCount = model.PresenceCount.IsSpecified ? model.PresenceCount.Value : null;
  41. }
  42. public async Task UpdateAsync(RequestOptions options = null)
  43. {
  44. var args = new GetInviteParams();
  45. if (MemberCount != null || PresenceCount != null)
  46. args.WithCounts = true;
  47. var model = await Discord.ApiClient.GetInviteAsync(Code, args, options).ConfigureAwait(false);
  48. Update(model);
  49. }
  50. public Task DeleteAsync(RequestOptions options = null)
  51. => InviteHelper.DeleteAsync(this, Discord, options);
  52. public override string ToString() => Url;
  53. private string DebuggerDisplay => $"{Url} ({GuildName} / {ChannelName})";
  54. IGuild IInvite.Guild
  55. {
  56. get
  57. {
  58. if (Guild != null)
  59. return Guild;
  60. if (Channel is IGuildChannel guildChannel)
  61. return guildChannel.Guild; //If it fails, it'll still return this exception
  62. throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
  63. }
  64. }
  65. IChannel IInvite.Channel
  66. {
  67. get
  68. {
  69. if (Channel != null)
  70. return Channel;
  71. throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
  72. }
  73. }
  74. }
  75. }