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 12 kB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. using ImageModel = Discord.API.Image;
  11. namespace Discord.Rest
  12. {
  13. internal static class GuildHelper
  14. {
  15. //General
  16. public static async Task<Model> ModifyAsync(IGuild guild, BaseDiscordClient client,
  17. Action<GuildProperties> func, RequestOptions options)
  18. {
  19. if (func == null) throw new NullReferenceException(nameof(func));
  20. var args = new GuildProperties();
  21. func(args);
  22. var apiArgs = new API.Rest.ModifyGuildParams
  23. {
  24. AfkChannelId = args.AfkChannelId,
  25. AfkTimeout = args.AfkTimeout,
  26. SystemChannelId = args.SystemChannelId,
  27. DefaultMessageNotifications = args.DefaultMessageNotifications,
  28. Icon = args.Icon.IsSpecified ? args.Icon.Value?.ToModel() : Optional.Create<ImageModel?>(),
  29. Name = args.Name,
  30. Splash = args.Splash.IsSpecified ? args.Splash.Value?.ToModel() : Optional.Create<ImageModel?>(),
  31. Username = args.Username,
  32. VerificationLevel = args.VerificationLevel
  33. };
  34. if (args.AfkChannel.IsSpecified)
  35. apiArgs.AfkChannelId = args.AfkChannel.Value.Id;
  36. else if (args.AfkChannelId.IsSpecified)
  37. apiArgs.AfkChannelId = args.AfkChannelId.Value;
  38. if (args.SystemChannel.IsSpecified)
  39. apiArgs.SystemChannelId = args.SystemChannel.Value.Id;
  40. else if (args.SystemChannelId.IsSpecified)
  41. apiArgs.SystemChannelId = args.SystemChannelId.Value;
  42. if (args.Owner.IsSpecified)
  43. apiArgs.OwnerId = args.Owner.Value.Id;
  44. else if (args.OwnerId.IsSpecified)
  45. apiArgs.OwnerId = args.OwnerId.Value;
  46. if (args.Region.IsSpecified)
  47. apiArgs.RegionId = args.Region.Value.Id;
  48. else if (args.RegionId.IsSpecified)
  49. apiArgs.RegionId = args.RegionId.Value;
  50. if (!apiArgs.Splash.IsSpecified && guild.SplashId != null)
  51. apiArgs.Splash = new ImageModel(guild.SplashId);
  52. if (!apiArgs.Icon.IsSpecified && guild.IconId != null)
  53. apiArgs.Icon = new ImageModel(guild.IconId);
  54. return await client.ApiClient.ModifyGuildAsync(guild.Id, apiArgs, options).ConfigureAwait(false);
  55. }
  56. public static async Task<EmbedModel> ModifyEmbedAsync(IGuild guild, BaseDiscordClient client,
  57. Action<GuildEmbedProperties> func, RequestOptions options)
  58. {
  59. if (func == null) throw new NullReferenceException(nameof(func));
  60. var args = new GuildEmbedProperties();
  61. func(args);
  62. var apiArgs = new API.Rest.ModifyGuildEmbedParams
  63. {
  64. Enabled = args.Enabled
  65. };
  66. if (args.Channel.IsSpecified)
  67. apiArgs.ChannelId = args.Channel.Value?.Id;
  68. else if (args.ChannelId.IsSpecified)
  69. apiArgs.ChannelId = args.ChannelId.Value;
  70. return await client.ApiClient.ModifyGuildEmbedAsync(guild.Id, apiArgs, options).ConfigureAwait(false);
  71. }
  72. public static async Task ReorderChannelsAsync(IGuild guild, BaseDiscordClient client,
  73. IEnumerable<ReorderChannelProperties> args, RequestOptions options)
  74. {
  75. var apiArgs = args.Select(x => new API.Rest.ModifyGuildChannelsParams(x.Id, x.Position));
  76. await client.ApiClient.ModifyGuildChannelsAsync(guild.Id, apiArgs, options).ConfigureAwait(false);
  77. }
  78. public static async Task<IReadOnlyCollection<RoleModel>> ReorderRolesAsync(IGuild guild, BaseDiscordClient client,
  79. IEnumerable<ReorderRoleProperties> args, RequestOptions options)
  80. {
  81. var apiArgs = args.Select(x => new API.Rest.ModifyGuildRolesParams(x.Id, x.Position));
  82. return await client.ApiClient.ModifyGuildRolesAsync(guild.Id, apiArgs, options).ConfigureAwait(false);
  83. }
  84. public static async Task LeaveAsync(IGuild guild, BaseDiscordClient client,
  85. RequestOptions options)
  86. {
  87. await client.ApiClient.LeaveGuildAsync(guild.Id, options).ConfigureAwait(false);
  88. }
  89. public static async Task DeleteAsync(IGuild guild, BaseDiscordClient client,
  90. RequestOptions options)
  91. {
  92. await client.ApiClient.DeleteGuildAsync(guild.Id, options).ConfigureAwait(false);
  93. }
  94. //Bans
  95. public static async Task<IReadOnlyCollection<RestBan>> GetBansAsync(IGuild guild, BaseDiscordClient client,
  96. RequestOptions options)
  97. {
  98. var models = await client.ApiClient.GetGuildBansAsync(guild.Id, options).ConfigureAwait(false);
  99. return models.Select(x => RestBan.Create(client, x)).ToImmutableArray();
  100. }
  101. public static async Task AddBanAsync(IGuild guild, BaseDiscordClient client,
  102. ulong userId, int pruneDays, string reason, RequestOptions options)
  103. {
  104. var args = new CreateGuildBanParams { DeleteMessageDays = pruneDays, Reason = reason };
  105. await client.ApiClient.CreateGuildBanAsync(guild.Id, userId, args, options).ConfigureAwait(false);
  106. }
  107. public static async Task RemoveBanAsync(IGuild guild, BaseDiscordClient client,
  108. ulong userId, RequestOptions options)
  109. {
  110. await client.ApiClient.RemoveGuildBanAsync(guild.Id, userId, options).ConfigureAwait(false);
  111. }
  112. //Channels
  113. public static async Task<RestGuildChannel> GetChannelAsync(IGuild guild, BaseDiscordClient client,
  114. ulong id, RequestOptions options)
  115. {
  116. var model = await client.ApiClient.GetChannelAsync(guild.Id, id, options).ConfigureAwait(false);
  117. if (model != null)
  118. return RestGuildChannel.Create(client, guild, model);
  119. return null;
  120. }
  121. public static async Task<IReadOnlyCollection<RestGuildChannel>> GetChannelsAsync(IGuild guild, BaseDiscordClient client,
  122. RequestOptions options)
  123. {
  124. var models = await client.ApiClient.GetGuildChannelsAsync(guild.Id, options).ConfigureAwait(false);
  125. return models.Select(x => RestGuildChannel.Create(client, guild, x)).ToImmutableArray();
  126. }
  127. public static async Task<RestTextChannel> CreateTextChannelAsync(IGuild guild, BaseDiscordClient client,
  128. string name, RequestOptions options)
  129. {
  130. if (name == null) throw new ArgumentNullException(nameof(name));
  131. var args = new CreateGuildChannelParams(name, ChannelType.Text);
  132. var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
  133. return RestTextChannel.Create(client, guild, model);
  134. }
  135. public static async Task<RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
  136. string name, RequestOptions options)
  137. {
  138. if (name == null) throw new ArgumentNullException(nameof(name));
  139. var args = new CreateGuildChannelParams(name, ChannelType.Voice);
  140. var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
  141. return RestVoiceChannel.Create(client, guild, model);
  142. }
  143. //Integrations
  144. public static async Task<IReadOnlyCollection<RestGuildIntegration>> GetIntegrationsAsync(IGuild guild, BaseDiscordClient client,
  145. RequestOptions options)
  146. {
  147. var models = await client.ApiClient.GetGuildIntegrationsAsync(guild.Id, options).ConfigureAwait(false);
  148. return models.Select(x => RestGuildIntegration.Create(client, guild, x)).ToImmutableArray();
  149. }
  150. public static async Task<RestGuildIntegration> CreateIntegrationAsync(IGuild guild, BaseDiscordClient client,
  151. ulong id, string type, RequestOptions options)
  152. {
  153. var args = new CreateGuildIntegrationParams(id, type);
  154. var model = await client.ApiClient.CreateGuildIntegrationAsync(guild.Id, args, options).ConfigureAwait(false);
  155. return RestGuildIntegration.Create(client, guild, model);
  156. }
  157. //Invites
  158. public static async Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync(IGuild guild, BaseDiscordClient client,
  159. RequestOptions options)
  160. {
  161. var models = await client.ApiClient.GetGuildInvitesAsync(guild.Id, options).ConfigureAwait(false);
  162. return models.Select(x => RestInviteMetadata.Create(client, guild, null, x)).ToImmutableArray();
  163. }
  164. //Roles
  165. public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClient client,
  166. string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
  167. {
  168. if (name == null) throw new ArgumentNullException(nameof(name));
  169. var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, options).ConfigureAwait(false);
  170. var role = RestRole.Create(client, guild, model);
  171. await role.ModifyAsync(x =>
  172. {
  173. x.Name = name;
  174. x.Permissions = (permissions ?? role.Permissions);
  175. x.Color = (color ?? Color.Default);
  176. x.Hoist = isHoisted;
  177. }, options).ConfigureAwait(false);
  178. return role;
  179. }
  180. //Users
  181. public static async Task<RestGuildUser> GetUserAsync(IGuild guild, BaseDiscordClient client,
  182. ulong id, RequestOptions options)
  183. {
  184. var model = await client.ApiClient.GetGuildMemberAsync(guild.Id, id, options).ConfigureAwait(false);
  185. if (model != null)
  186. return RestGuildUser.Create(client, guild, model);
  187. return null;
  188. }
  189. public static async Task<RestGuildUser> GetCurrentUserAsync(IGuild guild, BaseDiscordClient client,
  190. RequestOptions options)
  191. {
  192. return await GetUserAsync(guild, client, client.CurrentUser.Id, options).ConfigureAwait(false);
  193. }
  194. public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuild guild, BaseDiscordClient client,
  195. ulong? fromUserId, int? limit, RequestOptions options)
  196. {
  197. return new PagedAsyncEnumerable<RestGuildUser>(
  198. DiscordConfig.MaxMessagesPerBatch,
  199. async (info, ct) =>
  200. {
  201. var args = new GetGuildMembersParams
  202. {
  203. Limit = info.PageSize
  204. };
  205. if (info.Position != null)
  206. args.AfterUserId = info.Position.Value;
  207. var models = await client.ApiClient.GetGuildMembersAsync(guild.Id, args, options);
  208. return models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray();
  209. },
  210. nextPage: (info, lastPage) =>
  211. {
  212. if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
  213. return false;
  214. info.Position = lastPage.Max(x => x.Id);
  215. return true;
  216. },
  217. start: fromUserId,
  218. count: limit
  219. );
  220. }
  221. public static async Task<int> PruneUsersAsync(IGuild guild, BaseDiscordClient client,
  222. int days, bool simulate, RequestOptions options)
  223. {
  224. var args = new GuildPruneParams(days);
  225. GetGuildPruneCountResponse model;
  226. if (simulate)
  227. model = await client.ApiClient.GetGuildPruneCountAsync(guild.Id, args, options).ConfigureAwait(false);
  228. else
  229. model = await client.ApiClient.BeginGuildPruneAsync(guild.Id, args, options).ConfigureAwait(false);
  230. return model.Pruned;
  231. }
  232. }
  233. }