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.

ClientHelper.cs 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using Discord.API.Rest;
  2. using System.Collections.Generic;
  3. using System.Collections.Immutable;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace Discord.Rest
  8. {
  9. internal static class ClientHelper
  10. {
  11. //Applications
  12. public static async Task<RestApplication> GetApplicationInfoAsync(BaseDiscordClient client, RequestOptions options)
  13. {
  14. var model = await client.ApiClient.GetMyApplicationAsync(options).ConfigureAwait(false);
  15. return RestApplication.Create(client, model);
  16. }
  17. public static async Task<RestChannel> GetChannelAsync(BaseDiscordClient client,
  18. ulong id, RequestOptions options)
  19. {
  20. var model = await client.ApiClient.GetChannelAsync(id, options).ConfigureAwait(false);
  21. if (model != null)
  22. return RestChannel.Create(client, model);
  23. return null;
  24. }
  25. public static async Task<IReadOnlyCollection<IRestPrivateChannel>> GetPrivateChannelsAsync(BaseDiscordClient client, RequestOptions options)
  26. {
  27. var models = await client.ApiClient.GetMyPrivateChannelsAsync(options).ConfigureAwait(false);
  28. return models.Select(x => RestChannel.CreatePrivate(client, x)).ToImmutableArray();
  29. }
  30. public static async Task<IReadOnlyCollection<RestDMChannel>> GetDMChannelsAsync(BaseDiscordClient client, RequestOptions options)
  31. {
  32. var models = await client.ApiClient.GetMyPrivateChannelsAsync(options).ConfigureAwait(false);
  33. return models
  34. .Where(x => x.Type == ChannelType.DM)
  35. .Select(x => RestDMChannel.Create(client, x)).ToImmutableArray();
  36. }
  37. public static async Task<IReadOnlyCollection<RestGroupChannel>> GetGroupChannelsAsync(BaseDiscordClient client, RequestOptions options)
  38. {
  39. var models = await client.ApiClient.GetMyPrivateChannelsAsync(options).ConfigureAwait(false);
  40. return models
  41. .Where(x => x.Type == ChannelType.Group)
  42. .Select(x => RestGroupChannel.Create(client, x)).ToImmutableArray();
  43. }
  44. public static async Task<IReadOnlyCollection<RestConnection>> GetConnectionsAsync(BaseDiscordClient client, RequestOptions options)
  45. {
  46. var models = await client.ApiClient.GetMyConnectionsAsync(options).ConfigureAwait(false);
  47. return models.Select(x => RestConnection.Create(x)).ToImmutableArray();
  48. }
  49. public static async Task<RestInvite> GetInviteAsync(BaseDiscordClient client,
  50. string inviteId, RequestOptions options)
  51. {
  52. var model = await client.ApiClient.GetInviteAsync(inviteId, options).ConfigureAwait(false);
  53. if (model != null)
  54. return RestInvite.Create(client, null, null, model);
  55. return null;
  56. }
  57. public static async Task<RestGuild> GetGuildAsync(BaseDiscordClient client,
  58. ulong id, RequestOptions options)
  59. {
  60. var model = await client.ApiClient.GetGuildAsync(id, options).ConfigureAwait(false);
  61. if (model != null)
  62. return RestGuild.Create(client, model);
  63. return null;
  64. }
  65. public static async Task<RestGuildEmbed?> GetGuildEmbedAsync(BaseDiscordClient client,
  66. ulong id, RequestOptions options)
  67. {
  68. var model = await client.ApiClient.GetGuildEmbedAsync(id, options).ConfigureAwait(false);
  69. if (model != null)
  70. return RestGuildEmbed.Create(model);
  71. return null;
  72. }
  73. public static IAsyncEnumerable<IReadOnlyCollection<RestUserGuild>> GetGuildSummariesAsync(BaseDiscordClient client,
  74. ulong? fromGuildId, int? limit, RequestOptions options)
  75. {
  76. return new PagedAsyncEnumerable<RestUserGuild>(
  77. DiscordConfig.MaxGuildsPerBatch,
  78. async (info, ct) =>
  79. {
  80. var args = new GetGuildSummariesParams
  81. {
  82. Limit = info.PageSize
  83. };
  84. if (info.Position != null)
  85. args.AfterGuildId = info.Position.Value;
  86. var models = await client.ApiClient.GetMyGuildsAsync(args, options).ConfigureAwait(false);
  87. return models
  88. .Select(x => RestUserGuild.Create(client, x))
  89. .ToImmutableArray();
  90. },
  91. nextPage: (info, lastPage) =>
  92. {
  93. if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
  94. return false;
  95. info.Position = lastPage.Max(x => x.Id);
  96. return true;
  97. },
  98. start: fromGuildId,
  99. count: limit
  100. );
  101. }
  102. public static async Task<IReadOnlyCollection<RestGuild>> GetGuildsAsync(BaseDiscordClient client, RequestOptions options)
  103. {
  104. var summaryModels = await GetGuildSummariesAsync(client, null, null, options).FlattenAsync().ConfigureAwait(false);
  105. var guilds = ImmutableArray.CreateBuilder<RestGuild>();
  106. foreach (var summaryModel in summaryModels)
  107. {
  108. var guildModel = await client.ApiClient.GetGuildAsync(summaryModel.Id).ConfigureAwait(false);
  109. if (guildModel != null)
  110. guilds.Add(RestGuild.Create(client, guildModel));
  111. }
  112. return guilds.ToImmutable();
  113. }
  114. public static async Task<RestGuild> CreateGuildAsync(BaseDiscordClient client,
  115. string name, IVoiceRegion region, Stream jpegIcon, RequestOptions options)
  116. {
  117. var args = new CreateGuildParams(name, region.Id);
  118. if (jpegIcon != null)
  119. args.Icon = new API.Image(jpegIcon);
  120. var model = await client.ApiClient.CreateGuildAsync(args, options).ConfigureAwait(false);
  121. return RestGuild.Create(client, model);
  122. }
  123. public static async Task<RestUser> GetUserAsync(BaseDiscordClient client,
  124. ulong id, RequestOptions options)
  125. {
  126. var model = await client.ApiClient.GetUserAsync(id, options).ConfigureAwait(false);
  127. if (model != null)
  128. return RestUser.Create(client, model);
  129. return null;
  130. }
  131. public static async Task<RestGuildUser> GetGuildUserAsync(BaseDiscordClient client,
  132. ulong guildId, ulong id, RequestOptions options)
  133. {
  134. var model = await client.ApiClient.GetGuildMemberAsync(guildId, id, options).ConfigureAwait(false);
  135. if (model != null)
  136. return RestGuildUser.Create(client, new RestGuild(client, guildId), model);
  137. return null;
  138. }
  139. public static async Task<RestWebhook> GetWebhookAsync(BaseDiscordClient client, ulong id, RequestOptions options)
  140. {
  141. var model = await client.ApiClient.GetWebhookAsync(id);
  142. if (model != null)
  143. return RestWebhook.Create(client, (IGuild)null, model);
  144. return null;
  145. }
  146. public static async Task<IReadOnlyCollection<RestVoiceRegion>> GetVoiceRegionsAsync(BaseDiscordClient client, RequestOptions options)
  147. {
  148. var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false);
  149. return models.Select(x => RestVoiceRegion.Create(client, x)).ToImmutableArray();
  150. }
  151. public static async Task<RestVoiceRegion> GetVoiceRegionAsync(BaseDiscordClient client,
  152. string id, RequestOptions options)
  153. {
  154. var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false);
  155. return models.Select(x => RestVoiceRegion.Create(client, x)).FirstOrDefault(x => x.Id == id);
  156. }
  157. }
  158. }