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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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<RestInviteMetadata> GetInviteAsync(BaseDiscordClient client,
  50. string inviteId, bool withCount, RequestOptions options)
  51. {
  52. var args = new GetInviteParams
  53. {
  54. WithCounts = withCount
  55. };
  56. var model = await client.ApiClient.GetInviteAsync(inviteId, args, options).ConfigureAwait(false);
  57. if (model != null)
  58. return RestInviteMetadata.Create(client, null, null, model);
  59. return null;
  60. }
  61. public static async Task<RestGuild> GetGuildAsync(BaseDiscordClient client,
  62. ulong id, RequestOptions options)
  63. {
  64. var model = await client.ApiClient.GetGuildAsync(id, options).ConfigureAwait(false);
  65. if (model != null)
  66. return RestGuild.Create(client, model);
  67. return null;
  68. }
  69. public static async Task<RestGuildEmbed?> GetGuildEmbedAsync(BaseDiscordClient client,
  70. ulong id, RequestOptions options)
  71. {
  72. var model = await client.ApiClient.GetGuildEmbedAsync(id, options).ConfigureAwait(false);
  73. if (model != null)
  74. return RestGuildEmbed.Create(model);
  75. return null;
  76. }
  77. public static IAsyncEnumerable<IReadOnlyCollection<RestUserGuild>> GetGuildSummariesAsync(BaseDiscordClient client,
  78. ulong? fromGuildId, int? limit, RequestOptions options)
  79. {
  80. return new PagedAsyncEnumerable<RestUserGuild>(
  81. DiscordConfig.MaxGuildsPerBatch,
  82. async (info, ct) =>
  83. {
  84. var args = new GetGuildSummariesParams
  85. {
  86. Limit = info.PageSize
  87. };
  88. if (info.Position != null)
  89. args.AfterGuildId = info.Position.Value;
  90. var models = await client.ApiClient.GetMyGuildsAsync(args, options).ConfigureAwait(false);
  91. return models
  92. .Select(x => RestUserGuild.Create(client, x))
  93. .ToImmutableArray();
  94. },
  95. nextPage: (info, lastPage) =>
  96. {
  97. if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
  98. return false;
  99. info.Position = lastPage.Max(x => x.Id);
  100. return true;
  101. },
  102. start: fromGuildId,
  103. count: limit
  104. );
  105. }
  106. public static async Task<IReadOnlyCollection<RestGuild>> GetGuildsAsync(BaseDiscordClient client, RequestOptions options)
  107. {
  108. var summaryModels = await GetGuildSummariesAsync(client, null, null, options).FlattenAsync().ConfigureAwait(false);
  109. var guilds = ImmutableArray.CreateBuilder<RestGuild>();
  110. foreach (var summaryModel in summaryModels)
  111. {
  112. var guildModel = await client.ApiClient.GetGuildAsync(summaryModel.Id).ConfigureAwait(false);
  113. if (guildModel != null)
  114. guilds.Add(RestGuild.Create(client, guildModel));
  115. }
  116. return guilds.ToImmutable();
  117. }
  118. public static async Task<RestGuild> CreateGuildAsync(BaseDiscordClient client,
  119. string name, IVoiceRegion region, Stream jpegIcon, RequestOptions options)
  120. {
  121. var args = new CreateGuildParams(name, region.Id);
  122. if (jpegIcon != null)
  123. args.Icon = new API.Image(jpegIcon);
  124. var model = await client.ApiClient.CreateGuildAsync(args, options).ConfigureAwait(false);
  125. return RestGuild.Create(client, model);
  126. }
  127. public static async Task<RestUser> GetUserAsync(BaseDiscordClient client,
  128. ulong id, RequestOptions options)
  129. {
  130. var model = await client.ApiClient.GetUserAsync(id, options).ConfigureAwait(false);
  131. if (model != null)
  132. return RestUser.Create(client, model);
  133. return null;
  134. }
  135. public static async Task<RestGuildUser> GetGuildUserAsync(BaseDiscordClient client,
  136. ulong guildId, ulong id, RequestOptions options)
  137. {
  138. var guild = await GetGuildAsync(client, guildId, options).ConfigureAwait(false);
  139. if (guild == null)
  140. return null;
  141. var model = await client.ApiClient.GetGuildMemberAsync(guildId, id, options).ConfigureAwait(false);
  142. if (model != null)
  143. return RestGuildUser.Create(client, guild, model);
  144. return null;
  145. }
  146. public static async Task<RestWebhook> GetWebhookAsync(BaseDiscordClient client, ulong id, RequestOptions options)
  147. {
  148. var model = await client.ApiClient.GetWebhookAsync(id);
  149. if (model != null)
  150. return RestWebhook.Create(client, (IGuild)null, model);
  151. return null;
  152. }
  153. public static async Task<IReadOnlyCollection<RestVoiceRegion>> GetVoiceRegionsAsync(BaseDiscordClient client, RequestOptions options)
  154. {
  155. var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false);
  156. return models.Select(x => RestVoiceRegion.Create(client, x)).ToImmutableArray();
  157. }
  158. public static async Task<RestVoiceRegion> GetVoiceRegionAsync(BaseDiscordClient client,
  159. string id, RequestOptions options)
  160. {
  161. var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false);
  162. return models.Select(x => RestVoiceRegion.Create(client, x)).FirstOrDefault(x => x.Id == id);
  163. }
  164. public static async Task<int> GetRecommendShardCountAsync(BaseDiscordClient client, RequestOptions options)
  165. {
  166. var response = await client.ApiClient.GetBotGatewayAsync(options).ConfigureAwait(false);
  167. return response.Shards;
  168. }
  169. }
  170. }