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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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)
  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).ConfigureAwait(false);
  26. }
  27. public static async Task<EmbedModel> ModifyEmbedAsync(IGuild guild, BaseDiscordClient client,
  28. Action<ModifyGuildEmbedParams> func)
  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).ConfigureAwait(false);
  34. }
  35. public static async Task ModifyChannelsAsync(IGuild guild, BaseDiscordClient client,
  36. IEnumerable<ModifyGuildChannelsParams> args)
  37. {
  38. await client.ApiClient.ModifyGuildChannelsAsync(guild.Id, args).ConfigureAwait(false);
  39. }
  40. public static async Task<IReadOnlyCollection<RoleModel>> ModifyRolesAsync(IGuild guild, BaseDiscordClient client,
  41. IEnumerable<ModifyGuildRolesParams> args)
  42. {
  43. return await client.ApiClient.ModifyGuildRolesAsync(guild.Id, args).ConfigureAwait(false);
  44. }
  45. public static async Task LeaveAsync(IGuild guild, BaseDiscordClient client)
  46. {
  47. await client.ApiClient.LeaveGuildAsync(guild.Id).ConfigureAwait(false);
  48. }
  49. public static async Task DeleteAsync(IGuild guild, BaseDiscordClient client)
  50. {
  51. await client.ApiClient.DeleteGuildAsync(guild.Id).ConfigureAwait(false);
  52. }
  53. //Bans
  54. public static async Task<IReadOnlyCollection<RestBan>> GetBansAsync(IGuild guild, BaseDiscordClient client)
  55. {
  56. var models = await client.ApiClient.GetGuildBansAsync(guild.Id);
  57. return models.Select(x => RestBan.Create(client, x)).ToImmutableArray();
  58. }
  59. public static async Task AddBanAsync(IGuild guild, BaseDiscordClient client,
  60. ulong userId, int pruneDays)
  61. {
  62. var args = new CreateGuildBanParams { DeleteMessageDays = pruneDays };
  63. await client.ApiClient.CreateGuildBanAsync(guild.Id, userId, args);
  64. }
  65. public static async Task RemoveBanAsync(IGuild guild, BaseDiscordClient client,
  66. ulong userId)
  67. {
  68. await client.ApiClient.RemoveGuildBanAsync(guild.Id, userId);
  69. }
  70. //Channels
  71. public static async Task<RestGuildChannel> GetChannelAsync(IGuild guild, BaseDiscordClient client,
  72. ulong id)
  73. {
  74. var model = await client.ApiClient.GetChannelAsync(guild.Id, id).ConfigureAwait(false);
  75. if (model != null)
  76. return RestGuildChannel.Create(client, guild, model);
  77. return null;
  78. }
  79. public static async Task<IReadOnlyCollection<RestGuildChannel>> GetChannelsAsync(IGuild guild, BaseDiscordClient client)
  80. {
  81. var models = await client.ApiClient.GetGuildChannelsAsync(guild.Id).ConfigureAwait(false);
  82. return models.Select(x => RestGuildChannel.Create(client, guild, x)).ToImmutableArray();
  83. }
  84. public static async Task<RestTextChannel> CreateTextChannelAsync(IGuild guild, BaseDiscordClient client,
  85. string name)
  86. {
  87. if (name == null) throw new ArgumentNullException(nameof(name));
  88. var args = new CreateGuildChannelParams(name, ChannelType.Text);
  89. var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args).ConfigureAwait(false);
  90. return RestTextChannel.Create(client, guild, model);
  91. }
  92. public static async Task<RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
  93. string name)
  94. {
  95. if (name == null) throw new ArgumentNullException(nameof(name));
  96. var args = new CreateGuildChannelParams(name, ChannelType.Voice);
  97. var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args).ConfigureAwait(false);
  98. return RestVoiceChannel.Create(client, guild, model);
  99. }
  100. //Integrations
  101. public static async Task<IReadOnlyCollection<RestGuildIntegration>> GetIntegrationsAsync(IGuild guild, BaseDiscordClient client)
  102. {
  103. var models = await client.ApiClient.GetGuildIntegrationsAsync(guild.Id).ConfigureAwait(false);
  104. return models.Select(x => RestGuildIntegration.Create(client, x)).ToImmutableArray();
  105. }
  106. public static async Task<RestGuildIntegration> CreateIntegrationAsync(IGuild guild, BaseDiscordClient client,
  107. ulong id, string type)
  108. {
  109. var args = new CreateGuildIntegrationParams(id, type);
  110. var model = await client.ApiClient.CreateGuildIntegrationAsync(guild.Id, args).ConfigureAwait(false);
  111. return RestGuildIntegration.Create(client, model);
  112. }
  113. //Invites
  114. public static async Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync(IGuild guild, BaseDiscordClient client)
  115. {
  116. var models = await client.ApiClient.GetGuildInvitesAsync(guild.Id).ConfigureAwait(false);
  117. return models.Select(x => RestInviteMetadata.Create(client, x)).ToImmutableArray();
  118. }
  119. //Roles
  120. public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClient client,
  121. string name, GuildPermissions? permissions = null, Color? color = null, bool isHoisted = false)
  122. {
  123. if (name == null) throw new ArgumentNullException(nameof(name));
  124. var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id).ConfigureAwait(false);
  125. var role = RestRole.Create(client, model);
  126. await role.ModifyAsync(x =>
  127. {
  128. x.Name = name;
  129. x.Permissions = (permissions ?? role.Permissions).RawValue;
  130. x.Color = (color ?? Color.Default).RawValue;
  131. x.Hoist = isHoisted;
  132. }).ConfigureAwait(false);
  133. return role;
  134. }
  135. //Users
  136. public static async Task<RestGuildUser> GetUserAsync(IGuild guild, BaseDiscordClient client,
  137. ulong id)
  138. {
  139. var model = await client.ApiClient.GetGuildMemberAsync(guild.Id, id).ConfigureAwait(false);
  140. if (model != null)
  141. return RestGuildUser.Create(client, guild, model);
  142. return null;
  143. }
  144. public static async Task<RestGuildUser> GetCurrentUserAsync(IGuild guild, BaseDiscordClient client)
  145. {
  146. return await GetUserAsync(guild, client, client.CurrentUser.Id).ConfigureAwait(false);
  147. }
  148. public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuild guild, BaseDiscordClient client,
  149. ulong? fromUserId = null, int limit = DiscordConfig.MaxMessagesPerBatch)
  150. {
  151. return new PagedAsyncEnumerable<RestGuildUser>(
  152. DiscordConfig.MaxMessagesPerBatch,
  153. async (info, ct) =>
  154. {
  155. var args = new GetGuildMembersParams
  156. {
  157. Limit = info.PageSize
  158. };
  159. if (info.Position != null)
  160. args.AfterUserId = info.Position.Value;
  161. var models = await client.ApiClient.GetGuildMembersAsync(guild.Id, args);
  162. return models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray();
  163. },
  164. nextPage: (info, lastPage) =>
  165. {
  166. info.Position = lastPage.Max(x => x.Id);
  167. if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
  168. info.Remaining = 0;
  169. },
  170. start: fromUserId,
  171. count: limit
  172. );
  173. }
  174. public static async Task<int> PruneUsersAsync(IGuild guild, BaseDiscordClient client,
  175. int days = 30, bool simulate = false)
  176. {
  177. var args = new GuildPruneParams(days);
  178. GetGuildPruneCountResponse model;
  179. if (simulate)
  180. model = await client.ApiClient.GetGuildPruneCountAsync(guild.Id, args).ConfigureAwait(false);
  181. else
  182. model = await client.ApiClient.BeginGuildPruneAsync(guild.Id, args).ConfigureAwait(false);
  183. return model.Pruned;
  184. }
  185. }
  186. }