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

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