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.

GuildHelper.cs 10 KiB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using Discord.API.Rest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using EmbedModel = Discord.API.GuildEmbed;
  8. using Model = Discord.API.Guild;
  9. using RoleModel = Discord.API.Role;
  10. namespace Discord.Rest
  11. {
  12. internal static class GuildHelper
  13. {
  14. //General
  15. public static async Task<Model> ModifyAsync(IGuild guild, BaseDiscordClient client,
  16. Action<ModifyGuildParams> func, RequestOptions options)
  17. {
  18. if (func == null) throw new NullReferenceException(nameof(func));
  19. var args = new ModifyGuildParams();
  20. func(args);
  21. if (args.Splash.IsSpecified && guild.SplashId != null)
  22. args.Splash = new API.Image(guild.SplashId);
  23. if (args.Icon.IsSpecified && guild.IconId != null)
  24. args.Icon = new API.Image(guild.IconId);
  25. return await client.ApiClient.ModifyGuildAsync(guild.Id, args, options).ConfigureAwait(false);
  26. }
  27. public static async Task<EmbedModel> ModifyEmbedAsync(IGuild guild, BaseDiscordClient client,
  28. Action<ModifyGuildEmbedParams> func, RequestOptions options)
  29. {
  30. if (func == null) throw new NullReferenceException(nameof(func));
  31. var args = new ModifyGuildEmbedParams();
  32. func(args);
  33. return await client.ApiClient.ModifyGuildEmbedAsync(guild.Id, args, options).ConfigureAwait(false);
  34. }
  35. public static async Task ModifyChannelsAsync(IGuild guild, BaseDiscordClient client,
  36. IEnumerable<ModifyGuildChannelsParams> args, RequestOptions options)
  37. {
  38. await client.ApiClient.ModifyGuildChannelsAsync(guild.Id, args, options).ConfigureAwait(false);
  39. }
  40. public static async Task<IReadOnlyCollection<RoleModel>> ModifyRolesAsync(IGuild guild, BaseDiscordClient client,
  41. IEnumerable<ModifyGuildRolesParams> args, RequestOptions options)
  42. {
  43. return await client.ApiClient.ModifyGuildRolesAsync(guild.Id, args, options).ConfigureAwait(false);
  44. }
  45. public static async Task LeaveAsync(IGuild guild, BaseDiscordClient client,
  46. RequestOptions options)
  47. {
  48. await client.ApiClient.LeaveGuildAsync(guild.Id, options).ConfigureAwait(false);
  49. }
  50. public static async Task DeleteAsync(IGuild guild, BaseDiscordClient client,
  51. RequestOptions options)
  52. {
  53. await client.ApiClient.DeleteGuildAsync(guild.Id, options).ConfigureAwait(false);
  54. }
  55. //Bans
  56. public static async Task<IReadOnlyCollection<RestBan>> GetBansAsync(IGuild guild, BaseDiscordClient client,
  57. RequestOptions options)
  58. {
  59. var models = await client.ApiClient.GetGuildBansAsync(guild.Id, options).ConfigureAwait(false);
  60. return models.Select(x => RestBan.Create(client, x)).ToImmutableArray();
  61. }
  62. public static async Task AddBanAsync(IGuild guild, BaseDiscordClient client,
  63. ulong userId, int pruneDays, RequestOptions options)
  64. {
  65. var args = new CreateGuildBanParams { DeleteMessageDays = pruneDays };
  66. await client.ApiClient.CreateGuildBanAsync(guild.Id, userId, args, options).ConfigureAwait(false);
  67. }
  68. public static async Task RemoveBanAsync(IGuild guild, BaseDiscordClient client,
  69. ulong userId, RequestOptions options)
  70. {
  71. await client.ApiClient.RemoveGuildBanAsync(guild.Id, userId, options).ConfigureAwait(false);
  72. }
  73. //Channels
  74. public static async Task<RestGuildChannel> GetChannelAsync(IGuild guild, BaseDiscordClient client,
  75. ulong id, RequestOptions options)
  76. {
  77. var model = await client.ApiClient.GetChannelAsync(guild.Id, id, options).ConfigureAwait(false);
  78. if (model != null)
  79. return RestGuildChannel.Create(client, guild, model);
  80. return null;
  81. }
  82. public static async Task<IReadOnlyCollection<RestGuildChannel>> GetChannelsAsync(IGuild guild, BaseDiscordClient client,
  83. RequestOptions options)
  84. {
  85. var models = await client.ApiClient.GetGuildChannelsAsync(guild.Id, options).ConfigureAwait(false);
  86. return models.Select(x => RestGuildChannel.Create(client, guild, x)).ToImmutableArray();
  87. }
  88. public static async Task<RestTextChannel> CreateTextChannelAsync(IGuild guild, BaseDiscordClient client,
  89. string name, RequestOptions options)
  90. {
  91. if (name == null) throw new ArgumentNullException(nameof(name));
  92. var args = new CreateGuildChannelParams(name, ChannelType.Text);
  93. var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
  94. return RestTextChannel.Create(client, guild, model);
  95. }
  96. public static async Task<RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
  97. string name, RequestOptions options)
  98. {
  99. if (name == null) throw new ArgumentNullException(nameof(name));
  100. var args = new CreateGuildChannelParams(name, ChannelType.Voice);
  101. var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
  102. return RestVoiceChannel.Create(client, guild, model);
  103. }
  104. //Integrations
  105. public static async Task<IReadOnlyCollection<RestGuildIntegration>> GetIntegrationsAsync(IGuild guild, BaseDiscordClient client,
  106. RequestOptions options)
  107. {
  108. var models = await client.ApiClient.GetGuildIntegrationsAsync(guild.Id, options).ConfigureAwait(false);
  109. return models.Select(x => RestGuildIntegration.Create(client, guild, x)).ToImmutableArray();
  110. }
  111. public static async Task<RestGuildIntegration> CreateIntegrationAsync(IGuild guild, BaseDiscordClient client,
  112. ulong id, string type, RequestOptions options)
  113. {
  114. var args = new CreateGuildIntegrationParams(id, type);
  115. var model = await client.ApiClient.CreateGuildIntegrationAsync(guild.Id, args, options).ConfigureAwait(false);
  116. return RestGuildIntegration.Create(client, guild, model);
  117. }
  118. //Invites
  119. public static async Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync(IGuild guild, BaseDiscordClient client,
  120. RequestOptions options)
  121. {
  122. var models = await client.ApiClient.GetGuildInvitesAsync(guild.Id, options).ConfigureAwait(false);
  123. return models.Select(x => RestInviteMetadata.Create(client, guild, null, x)).ToImmutableArray();
  124. }
  125. //Roles
  126. public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClient client,
  127. string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
  128. {
  129. if (name == null) throw new ArgumentNullException(nameof(name));
  130. var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, options).ConfigureAwait(false);
  131. var role = RestRole.Create(client, guild, model);
  132. await role.ModifyAsync(x =>
  133. {
  134. x.Name = name;
  135. x.Permissions = (permissions ?? role.Permissions).RawValue;
  136. x.Color = (color ?? Color.Default).RawValue;
  137. x.Hoist = isHoisted;
  138. }, options).ConfigureAwait(false);
  139. return role;
  140. }
  141. //Users
  142. public static async Task<RestGuildUser> GetUserAsync(IGuild guild, BaseDiscordClient client,
  143. ulong id, RequestOptions options)
  144. {
  145. var model = await client.ApiClient.GetGuildMemberAsync(guild.Id, id, options).ConfigureAwait(false);
  146. if (model != null)
  147. return RestGuildUser.Create(client, guild, model);
  148. return null;
  149. }
  150. public static async Task<RestGuildUser> GetCurrentUserAsync(IGuild guild, BaseDiscordClient client,
  151. RequestOptions options)
  152. {
  153. return await GetUserAsync(guild, client, client.CurrentUser.Id, options).ConfigureAwait(false);
  154. }
  155. public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuild guild, BaseDiscordClient client,
  156. ulong? fromUserId, int? limit, RequestOptions options)
  157. {
  158. return new PagedAsyncEnumerable<RestGuildUser>(
  159. DiscordConfig.MaxMessagesPerBatch,
  160. async (info, ct) =>
  161. {
  162. var args = new GetGuildMembersParams
  163. {
  164. Limit = info.PageSize
  165. };
  166. if (info.Position != null)
  167. args.AfterUserId = info.Position.Value;
  168. var models = await client.ApiClient.GetGuildMembersAsync(guild.Id, args, options);
  169. return models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray();
  170. },
  171. nextPage: (info, lastPage) =>
  172. {
  173. if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
  174. return false;
  175. info.Position = lastPage.Max(x => x.Id);
  176. return true;
  177. },
  178. start: fromUserId,
  179. count: limit
  180. );
  181. }
  182. public static async Task<int> PruneUsersAsync(IGuild guild, BaseDiscordClient client,
  183. int days, bool simulate, RequestOptions options)
  184. {
  185. var args = new GuildPruneParams(days);
  186. GetGuildPruneCountResponse model;
  187. if (simulate)
  188. model = await client.ApiClient.GetGuildPruneCountAsync(guild.Id, args, options).ConfigureAwait(false);
  189. else
  190. model = await client.ApiClient.BeginGuildPruneAsync(guild.Id, args, options).ConfigureAwait(false);
  191. return model.Pruned;
  192. }
  193. }
  194. }