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.3 kB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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<RestInvite> GetInviteAsync(string inviteId, RequestOptions options = null)
  51. => ClientHelper.GetInviteAsync(this, inviteId, 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. //IDiscordClient
  83. async Task<IApplication> IDiscordClient.GetApplicationInfoAsync(RequestOptions options)
  84. => await GetApplicationInfoAsync(options).ConfigureAwait(false);
  85. async Task<IChannel> IDiscordClient.GetChannelAsync(ulong id, CacheMode mode, RequestOptions options)
  86. {
  87. if (mode == CacheMode.AllowDownload)
  88. return await GetChannelAsync(id, options).ConfigureAwait(false);
  89. else
  90. return null;
  91. }
  92. async Task<IReadOnlyCollection<IPrivateChannel>> IDiscordClient.GetPrivateChannelsAsync(CacheMode mode, RequestOptions options)
  93. {
  94. if (mode == CacheMode.AllowDownload)
  95. return await GetPrivateChannelsAsync(options).ConfigureAwait(false);
  96. else
  97. return ImmutableArray.Create<IPrivateChannel>();
  98. }
  99. async Task<IReadOnlyCollection<IDMChannel>> IDiscordClient.GetDMChannelsAsync(CacheMode mode, RequestOptions options)
  100. {
  101. if (mode == CacheMode.AllowDownload)
  102. return await GetDMChannelsAsync(options).ConfigureAwait(false);
  103. else
  104. return ImmutableArray.Create<IDMChannel>();
  105. }
  106. async Task<IReadOnlyCollection<IGroupChannel>> IDiscordClient.GetGroupChannelsAsync(CacheMode mode, RequestOptions options)
  107. {
  108. if (mode == CacheMode.AllowDownload)
  109. return await GetGroupChannelsAsync(options).ConfigureAwait(false);
  110. else
  111. return ImmutableArray.Create<IGroupChannel>();
  112. }
  113. async Task<IReadOnlyCollection<IConnection>> IDiscordClient.GetConnectionsAsync(RequestOptions options)
  114. => await GetConnectionsAsync(options).ConfigureAwait(false);
  115. async Task<IInvite> IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
  116. => await GetInviteAsync(inviteId, options).ConfigureAwait(false);
  117. async Task<IGuild> IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
  118. {
  119. if (mode == CacheMode.AllowDownload)
  120. return await GetGuildAsync(id, options).ConfigureAwait(false);
  121. else
  122. return null;
  123. }
  124. async Task<IReadOnlyCollection<IGuild>> IDiscordClient.GetGuildsAsync(CacheMode mode, RequestOptions options)
  125. {
  126. if (mode == CacheMode.AllowDownload)
  127. return await GetGuildsAsync(options).ConfigureAwait(false);
  128. else
  129. return ImmutableArray.Create<IGuild>();
  130. }
  131. async Task<IGuild> IDiscordClient.CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon, RequestOptions options)
  132. => await CreateGuildAsync(name, region, jpegIcon, options).ConfigureAwait(false);
  133. async Task<IUser> IDiscordClient.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  134. {
  135. if (mode == CacheMode.AllowDownload)
  136. return await GetUserAsync(id, options).ConfigureAwait(false);
  137. else
  138. return null;
  139. }
  140. async Task<IReadOnlyCollection<IVoiceRegion>> IDiscordClient.GetVoiceRegionsAsync(RequestOptions options)
  141. => await GetVoiceRegionsAsync(options).ConfigureAwait(false);
  142. async Task<IVoiceRegion> IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options)
  143. => await GetVoiceRegionAsync(id, options).ConfigureAwait(false);
  144. }
  145. }