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.

DiscordRestClient.cs 8.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System.Collections.Generic;
  2. using System.Collections.Immutable;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. namespace Discord.Rest
  6. {
  7. public class DiscordRestClient : BaseDiscordClient, IDiscordClient
  8. {
  9. private RestApplication _applicationInfo;
  10. public new RestSelfUser CurrentUser => base.CurrentUser as RestSelfUser;
  11. public DiscordRestClient() : this(new DiscordRestConfig()) { }
  12. public DiscordRestClient(DiscordRestConfig config) : base(config, CreateApiClient(config)) { }
  13. private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config)
  14. => new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent);
  15. internal override void Dispose(bool disposing)
  16. {
  17. if (disposing)
  18. ApiClient.Dispose();
  19. }
  20. internal override async Task OnLoginAsync(TokenType tokenType, string token)
  21. {
  22. var user = await ApiClient.GetMyUserAsync(new RequestOptions { RetryMode = RetryMode.AlwaysRetry }).ConfigureAwait(false);
  23. ApiClient.CurrentUserId = user.Id;
  24. base.CurrentUser = RestSelfUser.Create(this, user);
  25. }
  26. internal override Task OnLogoutAsync()
  27. {
  28. _applicationInfo = null;
  29. return Task.Delay(0);
  30. }
  31. /// <inheritdoc />
  32. public async Task<RestApplication> GetApplicationInfoAsync(RequestOptions options = null)
  33. {
  34. return _applicationInfo ?? (_applicationInfo = await ClientHelper.GetApplicationInfoAsync(this, options));
  35. }
  36. /// <inheritdoc />
  37. public Task<RestChannel> GetChannelAsync(ulong id, RequestOptions options = null)
  38. => ClientHelper.GetChannelAsync(this, id, options);
  39. /// <inheritdoc />
  40. public Task<IReadOnlyCollection<IRestPrivateChannel>> GetPrivateChannelsAsync(RequestOptions options = null)
  41. => ClientHelper.GetPrivateChannelsAsync(this, options);
  42. public Task<IReadOnlyCollection<RestDMChannel>> GetDMChannelsAsync(RequestOptions options = null)
  43. => ClientHelper.GetDMChannelsAsync(this, options);
  44. public Task<IReadOnlyCollection<RestGroupChannel>> GetGroupChannelsAsync(RequestOptions options = null)
  45. => ClientHelper.GetGroupChannelsAsync(this, options);
  46. /// <inheritdoc />
  47. public Task<IReadOnlyCollection<RestConnection>> GetConnectionsAsync(RequestOptions options = null)
  48. => ClientHelper.GetConnectionsAsync(this, options);
  49. /// <inheritdoc />
  50. public Task<RestInviteMetadata> GetInviteAsync(string inviteId, bool withCount = false, RequestOptions options = null)
  51. => ClientHelper.GetInviteAsync(this, inviteId, withCount, options);
  52. /// <inheritdoc />
  53. public Task<RestGuild> GetGuildAsync(ulong id, RequestOptions options = null)
  54. => ClientHelper.GetGuildAsync(this, id, options);
  55. /// <inheritdoc />
  56. public Task<RestGuildEmbed?> GetGuildEmbedAsync(ulong id, RequestOptions options = null)
  57. => ClientHelper.GetGuildEmbedAsync(this, id, options);
  58. /// <inheritdoc />
  59. public IAsyncEnumerable<IReadOnlyCollection<RestUserGuild>> GetGuildSummariesAsync(RequestOptions options = null)
  60. => ClientHelper.GetGuildSummariesAsync(this, null, null, options);
  61. /// <inheritdoc />
  62. public IAsyncEnumerable<IReadOnlyCollection<RestUserGuild>> GetGuildSummariesAsync(ulong fromGuildId, int limit, RequestOptions options = null)
  63. => ClientHelper.GetGuildSummariesAsync(this, fromGuildId, limit, options);
  64. /// <inheritdoc />
  65. public Task<IReadOnlyCollection<RestGuild>> GetGuildsAsync(RequestOptions options = null)
  66. => ClientHelper.GetGuildsAsync(this, options);
  67. /// <inheritdoc />
  68. public Task<RestGuild> CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon = null, RequestOptions options = null)
  69. => ClientHelper.CreateGuildAsync(this, name, region, jpegIcon, options);
  70. /// <inheritdoc />
  71. public Task<RestUser> GetUserAsync(ulong id, RequestOptions options = null)
  72. => ClientHelper.GetUserAsync(this, id, options);
  73. /// <inheritdoc />
  74. public Task<RestGuildUser> GetGuildUserAsync(ulong guildId, ulong id, RequestOptions options = null)
  75. => ClientHelper.GetGuildUserAsync(this, guildId, id, options);
  76. /// <inheritdoc />
  77. public Task<IReadOnlyCollection<RestVoiceRegion>> GetVoiceRegionsAsync(RequestOptions options = null)
  78. => ClientHelper.GetVoiceRegionsAsync(this, options);
  79. /// <inheritdoc />
  80. public Task<RestVoiceRegion> GetVoiceRegionAsync(string id, RequestOptions options = null)
  81. => ClientHelper.GetVoiceRegionAsync(this, id, options);
  82. /// <inheritdoc />
  83. public Task<RestWebhook> GetWebhookAsync(ulong id, RequestOptions options = null)
  84. => ClientHelper.GetWebhookAsync(this, id, options);
  85. //IDiscordClient
  86. async Task<IApplication> IDiscordClient.GetApplicationInfoAsync(RequestOptions options)
  87. => await GetApplicationInfoAsync(options).ConfigureAwait(false);
  88. async Task<IChannel> IDiscordClient.GetChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  89. {
  90. if (mode == CacheMode.AllowDownload)
  91. return await GetChannelAsync(id, options).ConfigureAwait(false);
  92. else
  93. return null;
  94. }
  95. async Task<IReadOnlyCollection<IPrivateChannel>> IDiscordClient.GetPrivateChannelsAsync(CacheMode mode, RequestOptions options)
  96. {
  97. if (mode == CacheMode.AllowDownload)
  98. return await GetPrivateChannelsAsync(options).ConfigureAwait(false);
  99. else
  100. return ImmutableArray.Create<IPrivateChannel>();
  101. }
  102. async Task<IReadOnlyCollection<IDMChannel>> IDiscordClient.GetDMChannelsAsync(CacheMode mode, RequestOptions options)
  103. {
  104. if (mode == CacheMode.AllowDownload)
  105. return await GetDMChannelsAsync(options).ConfigureAwait(false);
  106. else
  107. return ImmutableArray.Create<IDMChannel>();
  108. }
  109. async Task<IReadOnlyCollection<IGroupChannel>> IDiscordClient.GetGroupChannelsAsync(CacheMode mode, RequestOptions options)
  110. {
  111. if (mode == CacheMode.AllowDownload)
  112. return await GetGroupChannelsAsync(options).ConfigureAwait(false);
  113. else
  114. return ImmutableArray.Create<IGroupChannel>();
  115. }
  116. async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
  117. => await GetConnectionsAsync(options).ConfigureAwait(false);
  118. async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, bool withCount, RequestOptions options)
  119. => await GetInviteAsync(inviteId, withCount, options).ConfigureAwait(false);
  120. async Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
  121. {
  122. if (mode == CacheMode.AllowDownload)
  123. return await GetGuildAsync(id, options).ConfigureAwait(false);
  124. else
  125. return null;
  126. }
  127. async Task<IReadOnlyCollection<IGuild>> IDiscordClient.GetGuildsAsync(CacheMode mode, RequestOptions options)
  128. {
  129. if (mode == CacheMode.AllowDownload)
  130. return await GetGuildsAsync(options).ConfigureAwait(false);
  131. else
  132. return ImmutableArray.Create<IGuild>();
  133. }
  134. async Task<IGuild> IDiscordClient.CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon, RequestOptions options)
  135. => await CreateGuildAsync(name, region, jpegIcon, options).ConfigureAwait(false);
  136. async Task<IUser> IDiscordClient.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  137. {
  138. if (mode == CacheMode.AllowDownload)
  139. return await GetUserAsync(id, options).ConfigureAwait(false);
  140. else
  141. return null;
  142. }
  143. async Task<IReadOnlyCollection<IVoiceRegion>> IDiscordClient.GetVoiceRegionsAsync(RequestOptions options)
  144. => await GetVoiceRegionsAsync(options).ConfigureAwait(false);
  145. async Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options)
  146. => await GetVoiceRegionAsync(id, options).ConfigureAwait(false);
  147. async Task<IWebhook> IDiscordClient.GetWebhookAsync(ulong id, RequestOptions options)
  148. => await GetWebhookAsync(id, options);
  149. }
  150. }