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 18 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)
8 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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, Action<TextChannelProperties> func = null)
  134. {
  135. if (name == null) throw new ArgumentNullException(nameof(name));
  136. var props = new TextChannelProperties();
  137. func?.Invoke(props);
  138. var args = new CreateGuildChannelParams(name, ChannelType.Text)
  139. {
  140. CategoryId = props.CategoryId,
  141. Topic = props.Topic,
  142. IsNsfw = props.IsNsfw
  143. };
  144. var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
  145. return RestTextChannel.Create(client, guild, model);
  146. }
  147. public static async Task<RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
  148. string name, RequestOptions options, Action<VoiceChannelProperties> func = null)
  149. {
  150. if (name == null) throw new ArgumentNullException(nameof(name));
  151. var props = new VoiceChannelProperties();
  152. func?.Invoke(props);
  153. var args = new CreateGuildChannelParams(name, ChannelType.Voice)
  154. {
  155. CategoryId = props.CategoryId,
  156. Bitrate = props.Bitrate,
  157. UserLimit = props.UserLimit
  158. };
  159. var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
  160. return RestVoiceChannel.Create(client, guild, model);
  161. }
  162. public static async Task<RestCategoryChannel> CreateCategoryChannelAsync(IGuild guild, BaseDiscordClient client,
  163. string name, RequestOptions options)
  164. {
  165. if (name == null) throw new ArgumentNullException(nameof(name));
  166. var args = new CreateGuildChannelParams(name, ChannelType.Category);
  167. var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
  168. return RestCategoryChannel.Create(client, guild, model);
  169. }
  170. //Integrations
  171. public static async Task<IReadOnlyCollection<RestGuildIntegration>> GetIntegrationsAsync(IGuild guild, BaseDiscordClient client,
  172. RequestOptions options)
  173. {
  174. var models = await client.ApiClient.GetGuildIntegrationsAsync(guild.Id, options).ConfigureAwait(false);
  175. return models.Select(x => RestGuildIntegration.Create(client, guild, x)).ToImmutableArray();
  176. }
  177. public static async Task<RestGuildIntegration> CreateIntegrationAsync(IGuild guild, BaseDiscordClient client,
  178. ulong id, string type, RequestOptions options)
  179. {
  180. var args = new CreateGuildIntegrationParams(id, type);
  181. var model = await client.ApiClient.CreateGuildIntegrationAsync(guild.Id, args, options).ConfigureAwait(false);
  182. return RestGuildIntegration.Create(client, guild, model);
  183. }
  184. //Invites
  185. public static async Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync(IGuild guild, BaseDiscordClient client,
  186. RequestOptions options)
  187. {
  188. var models = await client.ApiClient.GetGuildInvitesAsync(guild.Id, options).ConfigureAwait(false);
  189. return models.Select(x => RestInviteMetadata.Create(client, guild, null, x)).ToImmutableArray();
  190. }
  191. //Roles
  192. public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClient client,
  193. string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
  194. {
  195. if (name == null) throw new ArgumentNullException(nameof(name));
  196. var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, options).ConfigureAwait(false);
  197. var role = RestRole.Create(client, guild, model);
  198. await role.ModifyAsync(x =>
  199. {
  200. x.Name = name;
  201. x.Permissions = (permissions ?? role.Permissions);
  202. x.Color = (color ?? Color.Default);
  203. x.Hoist = isHoisted;
  204. }, options).ConfigureAwait(false);
  205. return role;
  206. }
  207. //Users
  208. public static async Task<RestGuildUser> GetUserAsync(IGuild guild, BaseDiscordClient client,
  209. ulong id, RequestOptions options)
  210. {
  211. var model = await client.ApiClient.GetGuildMemberAsync(guild.Id, id, options).ConfigureAwait(false);
  212. if (model != null)
  213. return RestGuildUser.Create(client, guild, model);
  214. return null;
  215. }
  216. public static async Task<RestGuildUser> GetCurrentUserAsync(IGuild guild, BaseDiscordClient client,
  217. RequestOptions options)
  218. {
  219. return await GetUserAsync(guild, client, client.CurrentUser.Id, options).ConfigureAwait(false);
  220. }
  221. public static IAsyncEnumerable<IReadOnlyCollection<RestGuildUser>> GetUsersAsync(IGuild guild, BaseDiscordClient client,
  222. ulong? fromUserId, int? limit, RequestOptions options)
  223. {
  224. return new PagedAsyncEnumerable<RestGuildUser>(
  225. DiscordConfig.MaxMessagesPerBatch,
  226. async (info, ct) =>
  227. {
  228. var args = new GetGuildMembersParams
  229. {
  230. Limit = info.PageSize
  231. };
  232. if (info.Position != null)
  233. args.AfterUserId = info.Position.Value;
  234. var models = await client.ApiClient.GetGuildMembersAsync(guild.Id, args, options);
  235. return models.Select(x => RestGuildUser.Create(client, guild, x)).ToImmutableArray();
  236. },
  237. nextPage: (info, lastPage) =>
  238. {
  239. if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
  240. return false;
  241. info.Position = lastPage.Max(x => x.Id);
  242. return true;
  243. },
  244. start: fromUserId,
  245. count: limit
  246. );
  247. }
  248. public static async Task<int> PruneUsersAsync(IGuild guild, BaseDiscordClient client,
  249. int days, bool simulate, RequestOptions options)
  250. {
  251. var args = new GuildPruneParams(days);
  252. GetGuildPruneCountResponse model;
  253. if (simulate)
  254. model = await client.ApiClient.GetGuildPruneCountAsync(guild.Id, args, options).ConfigureAwait(false);
  255. else
  256. model = await client.ApiClient.BeginGuildPruneAsync(guild.Id, args, options).ConfigureAwait(false);
  257. return model.Pruned;
  258. }
  259. // Audit logs
  260. public static IAsyncEnumerable<IReadOnlyCollection<RestAuditLogEntry>> GetAuditLogsAsync(IGuild guild, BaseDiscordClient client,
  261. ulong? from, int? limit, RequestOptions options)
  262. {
  263. return new PagedAsyncEnumerable<RestAuditLogEntry>(
  264. DiscordConfig.MaxAuditLogEntriesPerBatch,
  265. async (info, ct) =>
  266. {
  267. var args = new GetAuditLogsParams
  268. {
  269. Limit = info.PageSize
  270. };
  271. if (info.Position != null)
  272. args.BeforeEntryId = info.Position.Value;
  273. var model = await client.ApiClient.GetAuditLogsAsync(guild.Id, args, options);
  274. return model.Entries.Select((x) => RestAuditLogEntry.Create(client, model, x)).ToImmutableArray();
  275. },
  276. nextPage: (info, lastPage) =>
  277. {
  278. if (lastPage.Count != DiscordConfig.MaxAuditLogEntriesPerBatch)
  279. return false;
  280. info.Position = lastPage.Min(x => x.Id);
  281. return true;
  282. },
  283. start: from,
  284. count: limit
  285. );
  286. }
  287. //Webhooks
  288. public static async Task<RestWebhook> GetWebhookAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options)
  289. {
  290. var model = await client.ApiClient.GetWebhookAsync(id, options: options).ConfigureAwait(false);
  291. if (model == null)
  292. return null;
  293. return RestWebhook.Create(client, guild, model);
  294. }
  295. public static async Task<IReadOnlyCollection<RestWebhook>> GetWebhooksAsync(IGuild guild, BaseDiscordClient client, RequestOptions options)
  296. {
  297. var models = await client.ApiClient.GetGuildWebhooksAsync(guild.Id, options).ConfigureAwait(false);
  298. return models.Select(x => RestWebhook.Create(client, guild, x)).ToImmutableArray();
  299. }
  300. //Emotes
  301. public static async Task<GuildEmote> GetEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options)
  302. {
  303. var emote = await client.ApiClient.GetGuildEmoteAsync(guild.Id, id, options);
  304. return emote.ToEntity();
  305. }
  306. public static async Task<GuildEmote> CreateEmoteAsync(IGuild guild, BaseDiscordClient client, string name, Image image, Optional<IEnumerable<IRole>> roles,
  307. RequestOptions options)
  308. {
  309. var apiargs = new CreateGuildEmoteParams
  310. {
  311. Name = name,
  312. Image = image.ToModel()
  313. };
  314. if (roles.IsSpecified)
  315. apiargs.RoleIds = roles.Value?.Select(xr => xr.Id)?.ToArray();
  316. var emote = await client.ApiClient.CreateGuildEmoteAsync(guild.Id, apiargs, options);
  317. return emote.ToEntity();
  318. }
  319. public static async Task<GuildEmote> ModifyEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, Action<EmoteProperties> func,
  320. RequestOptions options)
  321. {
  322. if (func == null) throw new ArgumentNullException(nameof(func));
  323. var props = new EmoteProperties();
  324. func(props);
  325. var apiargs = new ModifyGuildEmoteParams
  326. {
  327. Name = props.Name
  328. };
  329. if (props.Roles.IsSpecified)
  330. apiargs.RoleIds = props.Roles.Value?.Select(xr => xr.Id)?.ToArray();
  331. var emote = await client.ApiClient.ModifyGuildEmoteAsync(guild.Id, id, apiargs, options);
  332. return emote.ToEntity();
  333. }
  334. public static Task DeleteEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options)
  335. => client.ApiClient.DeleteGuildEmoteAsync(guild.Id, id, options);
  336. }
  337. }