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

Add support for channel categories (#907) commit a85c5814a74e473e95fe172f0379cbc7f9f951d8 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:25:48 2018 -0500 Code cleanup commit 4b243fd3dd99152b4ebc7ee01d704bd8e57eeee1 Author: Christopher F <computerizedtaco@gmail.com> Date: Sat Jan 6 22:08:28 2018 -0500 Add support for channel categories (#907) commit 41ed9106f2b05530acbf06b245c9aa618011d815 Author: mrspits4ever <spits.lucas@gmail.com> Date: Thu Dec 14 20:02:57 2017 +0100 removed mentioning support for RestCategoryChannel, added channels property to SocketCategoryChannel commit 71142c310847886dff80c49e9357dd0786d67a1b Merge: 4589d731 678a7238 Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:53 2017 +0100 Merge branch 'dev' of https://github.com/RogueException/Discord.Net into feature/channel-categories commit 4589d73187871c98485ed25c6d223706927af7ec Author: mrspits4ever <spits.lucas@gmail.com> Date: Wed Dec 13 21:17:46 2017 +0100 adressed requested changes commit d59b038efa048b2279602e2015ddd2c185e58d63 Author: pegasy <pegasy@users.noreply.github.com> Date: Mon Sep 25 18:53:23 2017 +0200 Renamed classes / properties / methods to use CategoryChannel instead of ChannelCategory to be consistant with how text / voice channels are named. commit 5c4777dc8cc443108f2e7e4afae98824c9a32b1f Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 19:08:25 2017 +0200 removed Guild from class name for ChannelCategory Renamed all properties to use Category instead of Parent Throw exception on GetUsers / GetInvites etc for categories commit e18bd8c799d2327270021c05866cb2e97ad4671b Author: pegasy <pegasy@users.noreply.github.com> Date: Sun Sep 24 15:49:51 2017 +0200 Add support for channel categories (as its own channel type)
7 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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<RestBan> GetBanAsync(IGuild guild, BaseDiscordClient client, ulong userId, RequestOptions options)
  102. {
  103. var model = await client.ApiClient.GetGuildBanAsync(guild.Id, userId, options).ConfigureAwait(false);
  104. return RestBan.Create(client, model);
  105. }
  106. public static async Task AddBanAsync(IGuild guild, BaseDiscordClient client,
  107. ulong userId, int pruneDays, string reason, RequestOptions options)
  108. {
  109. var args = new CreateGuildBanParams { DeleteMessageDays = pruneDays, Reason = reason };
  110. await client.ApiClient.CreateGuildBanAsync(guild.Id, userId, args, options).ConfigureAwait(false);
  111. }
  112. public static async Task RemoveBanAsync(IGuild guild, BaseDiscordClient client,
  113. ulong userId, RequestOptions options)
  114. {
  115. await client.ApiClient.RemoveGuildBanAsync(guild.Id, userId, options).ConfigureAwait(false);
  116. }
  117. //Channels
  118. public static async Task<RestGuildChannel> GetChannelAsync(IGuild guild, BaseDiscordClient client,
  119. ulong id, RequestOptions options)
  120. {
  121. var model = await client.ApiClient.GetChannelAsync(guild.Id, id, options).ConfigureAwait(false);
  122. if (model != null)
  123. return RestGuildChannel.Create(client, guild, model);
  124. return null;
  125. }
  126. public static async Task<IReadOnlyCollection<RestGuildChannel>> GetChannelsAsync(IGuild guild, BaseDiscordClient client,
  127. RequestOptions options)
  128. {
  129. var models = await client.ApiClient.GetGuildChannelsAsync(guild.Id, options).ConfigureAwait(false);
  130. return models.Select(x => RestGuildChannel.Create(client, guild, x)).ToImmutableArray();
  131. }
  132. public static async Task<RestTextChannel> CreateTextChannelAsync(IGuild guild, BaseDiscordClient client,
  133. string name, RequestOptions options)
  134. {
  135. if (name == null) throw new ArgumentNullException(nameof(name));
  136. var args = new CreateGuildChannelParams(name, ChannelType.Text);
  137. var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
  138. return RestTextChannel.Create(client, guild, model);
  139. }
  140. public static async Task<RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
  141. string name, RequestOptions options)
  142. {
  143. if (name == null) throw new ArgumentNullException(nameof(name));
  144. var args = new CreateGuildChannelParams(name, ChannelType.Voice);
  145. var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
  146. return RestVoiceChannel.Create(client, guild, model);
  147. }
  148. public static async Task<RestCategoryChannel> CreateCategoryChannelAsync(IGuild guild, BaseDiscordClient client,
  149. string name, RequestOptions options)
  150. {
  151. if (name == null) throw new ArgumentNullException(nameof(name));
  152. var args = new CreateGuildChannelParams(name, ChannelType.Category);
  153. var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
  154. return RestCategoryChannel.Create(client, guild, model);
  155. }
  156. //Integrations
  157. public static async Task<IReadOnlyCollection<RestGuildIntegration>> GetIntegrationsAsync(IGuild guild, BaseDiscordClient client,
  158. RequestOptions options)
  159. {
  160. var models = await client.ApiClient.GetGuildIntegrationsAsync(guild.Id, options).ConfigureAwait(false);
  161. return models.Select(x => RestGuildIntegration.Create(client, guild, x)).ToImmutableArray();
  162. }
  163. public static async Task<RestGuildIntegration> CreateIntegrationAsync(IGuild guild, BaseDiscordClient client,
  164. ulong id, string type, RequestOptions options)
  165. {
  166. var args = new CreateGuildIntegrationParams(id, type);
  167. var model = await client.ApiClient.CreateGuildIntegrationAsync(guild.Id, args, options).ConfigureAwait(false);
  168. return RestGuildIntegration.Create(client, guild, model);
  169. }
  170. //Invites
  171. public static async Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync(IGuild guild, BaseDiscordClient client,
  172. RequestOptions options)
  173. {
  174. var models = await client.ApiClient.GetGuildInvitesAsync(guild.Id, options).ConfigureAwait(false);
  175. return models.Select(x => RestInviteMetadata.Create(client, guild, null, x)).ToImmutableArray();
  176. }
  177. //Roles
  178. public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClient client,
  179. string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
  180. {
  181. if (name == null) throw new ArgumentNullException(nameof(name));
  182. var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, options).ConfigureAwait(false);
  183. var role = RestRole.Create(client, guild, model);
  184. await role.ModifyAsync(x =>
  185. {
  186. x.Name = name;
  187. x.Permissions = (permissions ?? role.Permissions);
  188. x.Color = (color ?? Color.Default);
  189. x.Hoist = isHoisted;
  190. }, options).ConfigureAwait(false);
  191. return role;
  192. }
  193. //Users
  194. public static async Task<RestGuildUser> GetUserAsync(IGuild guild, BaseDiscordClient client,
  195. ulong id, RequestOptions options)
  196. {
  197. var model = await client.ApiClient.GetGuildMemberAsync(guild.Id, id, options).ConfigureAwait(false);
  198. if (model != null)
  199. return RestGuildUser.Create(client, guild, model);
  200. return null;
  201. }
  202. public static async Task<RestGuildUser> GetCurrentUserAsync(IGuild guild, BaseDiscordClient client,
  203. RequestOptions options)
  204. {
  205. return await GetUserAsync(guild, client, client.CurrentUser.Id, options).ConfigureAwait(false);
  206. }
  207. public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuild guild, BaseDiscordClient client,
  208. ulong? fromUserId, int? limit, RequestOptions options)
  209. {
  210. return new PagedAsyncEnumerable<RestGuildUser>(
  211. DiscordConfig.MaxMessagesPerBatch,
  212. async (info, ct) =>
  213. {
  214. var args = new GetGuildMembersParams
  215. {
  216. Limit = info.PageSize
  217. };
  218. if (info.Position != null)
  219. args.AfterUserId = info.Position.Value;
  220. var models = await client.ApiClient.GetGuildMembersAsync(guild.Id, args, options);
  221. return models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray();
  222. },
  223. nextPage: (info, lastPage) =>
  224. {
  225. if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
  226. return false;
  227. info.Position = lastPage.Max(x => x.Id);
  228. return true;
  229. },
  230. start: fromUserId,
  231. count: limit
  232. );
  233. }
  234. public static async Task<int> PruneUsersAsync(IGuild guild, BaseDiscordClient client,
  235. int days, bool simulate, RequestOptions options)
  236. {
  237. var args = new GuildPruneParams(days);
  238. GetGuildPruneCountResponse model;
  239. if (simulate)
  240. model = await client.ApiClient.GetGuildPruneCountAsync(guild.Id, args, options).ConfigureAwait(false);
  241. else
  242. model = await client.ApiClient.BeginGuildPruneAsync(guild.Id, args, options).ConfigureAwait(false);
  243. return model.Pruned;
  244. }
  245. //Webhooks
  246. public static async Task<RestWebhook> GetWebhookAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options)
  247. {
  248. var model = await client.ApiClient.GetWebhookAsync(id, options: options).ConfigureAwait(false);
  249. if (model == null)
  250. return null;
  251. return RestWebhook.Create(client, guild, model);
  252. }
  253. public static async Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(IGuild guild, BaseDiscordClient client, RequestOptions options)
  254. {
  255. var models = await client.ApiClient.GetGuildWebhooksAsync(guild.Id, options).ConfigureAwait(false);
  256. return models.Select(x => RestWebhook.Create(client, guild, x)).ToImmutableArray();
  257. }
  258. //Emotes
  259. public static async Task<GuildEmote> GetEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options)
  260. {
  261. var emote = await client.ApiClient.GetGuildEmoteAsync(guild.Id, id, options);
  262. return emote.ToEntity();
  263. }
  264. public static async Task<GuildEmote> CreateEmoteAsync(IGuild guild, BaseDiscordClient client, string name, Image image, Optional<IEnumerable<IRole>> roles,
  265. RequestOptions options)
  266. {
  267. var apiargs = new CreateGuildEmoteParams
  268. {
  269. Name = name,
  270. Image = image.ToModel()
  271. };
  272. if (roles.IsSpecified)
  273. apiargs.RoleIds = roles.Value?.Select(xr => xr.Id)?.ToArray();
  274. var emote = await client.ApiClient.CreateGuildEmoteAsync(guild.Id, apiargs, options);
  275. return emote.ToEntity();
  276. }
  277. public static async Task<GuildEmote> ModifyEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, Action<EmoteProperties> func,
  278. RequestOptions options)
  279. {
  280. if (func == null) throw new ArgumentNullException(nameof(func));
  281. var props = new EmoteProperties();
  282. func(props);
  283. var apiargs = new ModifyGuildEmoteParams
  284. {
  285. Name = props.Name
  286. };
  287. if (props.Roles.IsSpecified)
  288. apiargs.RoleIds = props.Roles.Value?.Select(xr => xr.Id)?.ToArray();
  289. var emote = await client.ApiClient.ModifyGuildEmoteAsync(guild.Id, id, apiargs, options);
  290. return emote.ToEntity();
  291. }
  292. public static Task DeleteEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options)
  293. => client.ApiClient.DeleteGuildEmoteAsync(guild.Id, id, options);
  294. }
  295. }