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.

SocketGuildChannel.cs 11 kB

8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
7 years ago
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
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using Discord.Rest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Model = Discord.API.Channel;
  9. namespace Discord.WebSocket
  10. {
  11. /// <summary>
  12. /// Represents a WebSocket-based guild channel.
  13. /// </summary>
  14. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  15. public class SocketGuildChannel : SocketChannel, IGuildChannel
  16. {
  17. private ImmutableArray<Overwrite> _overwrites;
  18. /// <summary>
  19. /// Gets the guild associated with this channel.
  20. /// </summary>
  21. /// <returns>
  22. /// A guild that this channel belongs to.
  23. /// </returns>
  24. public SocketGuild Guild { get; }
  25. /// <inheritdoc />
  26. public string Name { get; private set; }
  27. /// <inheritdoc />
  28. public int Position { get; private set; }
  29. /// <inheritdoc />
  30. public IReadOnlyCollection<Overwrite> PermissionOverwrites => _overwrites;
  31. /// <summary>
  32. /// Gets a collection of users that are able to view the channel.
  33. /// </summary>
  34. /// <returns>
  35. /// A collection of users that can access the channel (i.e. the users seen in the user list).
  36. /// </returns>
  37. public new virtual IReadOnlyCollection<SocketGuildUser> Users => ImmutableArray.Create<SocketGuildUser>();
  38. internal SocketGuildChannel(DiscordSocketClient discord, ulong id, SocketGuild guild)
  39. : base(discord, id)
  40. {
  41. Guild = guild;
  42. }
  43. internal static SocketGuildChannel Create(SocketGuild guild, ClientState state, Model model)
  44. {
  45. switch (model.Type)
  46. {
  47. case ChannelType.Text:
  48. return SocketTextChannel.Create(guild, state, model);
  49. case ChannelType.Voice:
  50. return SocketVoiceChannel.Create(guild, state, model);
  51. case ChannelType.Category:
  52. return SocketCategoryChannel.Create(guild, state, model);
  53. default:
  54. // TODO: Proper implementation for channel categories
  55. return new SocketGuildChannel(guild.Discord, model.Id, guild);
  56. }
  57. }
  58. internal override void Update(ClientState state, Model model)
  59. {
  60. Name = model.Name.Value;
  61. Position = model.Position.Value;
  62. var overwrites = model.PermissionOverwrites.Value;
  63. var newOverwrites = ImmutableArray.CreateBuilder<Overwrite>(overwrites.Length);
  64. for (int i = 0; i < overwrites.Length; i++)
  65. newOverwrites.Add(overwrites[i].ToEntity());
  66. _overwrites = newOverwrites.ToImmutable();
  67. }
  68. /// <inheritdoc />
  69. public Task ModifyAsync(Action<GuildChannelProperties> func, RequestOptions options = null)
  70. => ChannelHelper.ModifyAsync(this, Discord, func, options);
  71. /// <inheritdoc />
  72. public Task DeleteAsync(RequestOptions options = null)
  73. => ChannelHelper.DeleteAsync(this, Discord, options);
  74. /// <summary>
  75. /// Gets the overwrite permissions of the specified <paramref name="user"/>.
  76. /// </summary>
  77. /// <param name="user">The user that you want to get the overwrite permissions for.</param>
  78. /// <returns>
  79. /// The overwrite permissions for the requested user; otherwise <c>null</c>.
  80. /// </returns>
  81. public OverwritePermissions? GetPermissionOverwrite(IUser user)
  82. {
  83. for (int i = 0; i < _overwrites.Length; i++)
  84. {
  85. if (_overwrites[i].TargetId == user.Id)
  86. return _overwrites[i].Permissions;
  87. }
  88. return null;
  89. }
  90. /// <summary>
  91. /// Gets the overwrite permissions of the specified <paramref name="role"/>.
  92. /// </summary>
  93. /// <param name="role">The role that you want to get the overwrite permissions for.</param>
  94. /// <returns>
  95. /// The overwrite permissions for the requested role; otherwise <c>null</c>.
  96. /// </returns>
  97. public OverwritePermissions? GetPermissionOverwrite(IRole role)
  98. {
  99. for (int i = 0; i < _overwrites.Length; i++)
  100. {
  101. if (_overwrites[i].TargetId == role.Id)
  102. return _overwrites[i].Permissions;
  103. }
  104. return null;
  105. }
  106. /// <summary>
  107. /// Adds an overwrite permission for the specified <paramref name="user"/>.
  108. /// </summary>
  109. /// <param name="user">The user you want the overwrite permission to apply to.</param>
  110. /// <param name="perms">The overwrite permission you want to add.</param>
  111. /// <param name="options">The options to be used when sending the request.</param>
  112. /// <returns>An awaitable <see cref="Task"/>.</returns>
  113. public async Task AddPermissionOverwriteAsync(IUser user, OverwritePermissions perms, RequestOptions options = null)
  114. {
  115. await ChannelHelper.AddPermissionOverwriteAsync(this, Discord, user, perms, options).ConfigureAwait(false);
  116. _overwrites = _overwrites.Add(new Overwrite(user.Id, PermissionTarget.User, new OverwritePermissions(perms.AllowValue, perms.DenyValue)));
  117. }
  118. /// <summary>
  119. /// Adds an overwrite permission for the specified <paramref name="role"/>.
  120. /// </summary>
  121. /// <param name="role">The role you want the overwrite permission to apply to.</param>
  122. /// <param name="perms">The overwrite permission you want to add.</param>
  123. /// <param name="options">The options to be used when sending the request. </param>
  124. /// <returns>An awaitable <see cref="Task"/>.</returns>
  125. public async Task AddPermissionOverwriteAsync(IRole role, OverwritePermissions perms, RequestOptions options = null)
  126. {
  127. await ChannelHelper.AddPermissionOverwriteAsync(this, Discord, role, perms, options).ConfigureAwait(false);
  128. _overwrites = _overwrites.Add(new Overwrite(role.Id, PermissionTarget.Role, new OverwritePermissions(perms.AllowValue, perms.DenyValue)));
  129. }
  130. public async Task RemovePermissionOverwriteAsync(IUser user, RequestOptions options = null)
  131. {
  132. await ChannelHelper.RemovePermissionOverwriteAsync(this, Discord, user, options).ConfigureAwait(false);
  133. for (int i = 0; i < _overwrites.Length; i++)
  134. {
  135. if (_overwrites[i].TargetId == user.Id)
  136. {
  137. _overwrites = _overwrites.RemoveAt(i);
  138. return;
  139. }
  140. }
  141. }
  142. public async Task RemovePermissionOverwriteAsync(IRole role, RequestOptions options = null)
  143. {
  144. await ChannelHelper.RemovePermissionOverwriteAsync(this, Discord, role, options).ConfigureAwait(false);
  145. for (int i = 0; i < _overwrites.Length; i++)
  146. {
  147. if (_overwrites[i].TargetId == role.Id)
  148. {
  149. _overwrites = _overwrites.RemoveAt(i);
  150. return;
  151. }
  152. }
  153. }
  154. public async Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync(RequestOptions options = null)
  155. => await ChannelHelper.GetInvitesAsync(this, Discord, options).ConfigureAwait(false);
  156. public async Task<RestInviteMetadata> CreateInviteAsync(int? maxAge = 86400, int? maxUses = null, bool isTemporary = false, bool isUnique = false, RequestOptions options = null)
  157. => await ChannelHelper.CreateInviteAsync(this, Discord, maxAge, maxUses, isTemporary, isUnique, options).ConfigureAwait(false);
  158. public new virtual SocketGuildUser GetUser(ulong id) => null;
  159. /// <summary>
  160. /// Gets the name of the channel.
  161. /// </summary>
  162. public override string ToString() => Name;
  163. private string DebuggerDisplay => $"{Name} ({Id}, Guild)";
  164. internal new SocketGuildChannel Clone() => MemberwiseClone() as SocketGuildChannel;
  165. //SocketChannel
  166. internal override IReadOnlyCollection<SocketUser> GetUsersInternal() => Users;
  167. internal override SocketUser GetUserInternal(ulong id) => GetUser(id);
  168. //IGuildChannel
  169. /// <inheritdoc />
  170. IGuild IGuildChannel.Guild => Guild;
  171. /// <inheritdoc />
  172. ulong IGuildChannel.GuildId => Guild.Id;
  173. /// <inheritdoc />
  174. async Task<IReadOnlyCollection<IInviteMetadata>> IGuildChannel.GetInvitesAsync(RequestOptions options)
  175. => await GetInvitesAsync(options).ConfigureAwait(false);
  176. /// <inheritdoc />
  177. async Task<IInviteMetadata> IGuildChannel.CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary, bool isUnique, RequestOptions options)
  178. => await CreateInviteAsync(maxAge, maxUses, isTemporary, isUnique, options).ConfigureAwait(false);
  179. /// <inheritdoc />
  180. OverwritePermissions? IGuildChannel.GetPermissionOverwrite(IRole role)
  181. => GetPermissionOverwrite(role);
  182. /// <inheritdoc />
  183. OverwritePermissions? IGuildChannel.GetPermissionOverwrite(IUser user)
  184. => GetPermissionOverwrite(user);
  185. /// <inheritdoc />
  186. async Task IGuildChannel.AddPermissionOverwriteAsync(IRole role, OverwritePermissions permissions, RequestOptions options)
  187. => await AddPermissionOverwriteAsync(role, permissions, options).ConfigureAwait(false);
  188. /// <inheritdoc />
  189. async Task IGuildChannel.AddPermissionOverwriteAsync(IUser user, OverwritePermissions permissions, RequestOptions options)
  190. => await AddPermissionOverwriteAsync(user, permissions, options).ConfigureAwait(false);
  191. /// <inheritdoc />
  192. async Task IGuildChannel.RemovePermissionOverwriteAsync(IRole role, RequestOptions options)
  193. => await RemovePermissionOverwriteAsync(role, options).ConfigureAwait(false);
  194. /// <inheritdoc />
  195. async Task IGuildChannel.RemovePermissionOverwriteAsync(IUser user, RequestOptions options)
  196. => await RemovePermissionOverwriteAsync(user, options).ConfigureAwait(false);
  197. /// <inheritdoc />
  198. IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  199. => ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
  200. /// <inheritdoc />
  201. Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  202. => Task.FromResult<IGuildUser>(GetUser(id));
  203. //IChannel
  204. /// <inheritdoc />
  205. IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  206. => ImmutableArray.Create<IReadOnlyCollection<IUser>>(Users).ToAsyncEnumerable(); //Overridden in Text/Voice
  207. /// <inheritdoc />
  208. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  209. => Task.FromResult<IUser>(GetUser(id)); //Overridden in Text/Voice
  210. }
  211. }