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 8.7 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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  12. public class SocketGuildChannel : SocketChannel, IGuildChannel
  13. {
  14. private ImmutableArray<Overwrite> _overwrites;
  15. public SocketGuild Guild { get; }
  16. public string Name { get; private set; }
  17. public int Position { get; private set; }
  18. public ulong? CategoryId { get; private set; }
  19. public ICategoryChannel Category
  20. => CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null;
  21. public IReadOnlyCollection<Overwrite> PermissionOverwrites => _overwrites;
  22. public new virtual IReadOnlyCollection<SocketGuildUser> Users => ImmutableArray.Create<SocketGuildUser>();
  23. internal SocketGuildChannel(DiscordSocketClient discord, ulong id, SocketGuild guild)
  24. : base(discord, id)
  25. {
  26. Guild = guild;
  27. }
  28. internal static SocketGuildChannel Create(SocketGuild guild, ClientState state, Model model)
  29. {
  30. switch (model.Type)
  31. {
  32. case ChannelType.Text:
  33. return SocketTextChannel.Create(guild, state, model);
  34. case ChannelType.Voice:
  35. return SocketVoiceChannel.Create(guild, state, model);
  36. case ChannelType.Category:
  37. return SocketCategoryChannel.Create(guild, state, model);
  38. default:
  39. // TODO: Proper implementation for channel categories
  40. return new SocketGuildChannel(guild.Discord, model.Id, guild);
  41. }
  42. }
  43. internal override void Update(ClientState state, Model model)
  44. {
  45. Name = model.Name.Value;
  46. Position = model.Position.Value;
  47. CategoryId = model.CategoryId;
  48. var overwrites = model.PermissionOverwrites.Value;
  49. var newOverwrites = ImmutableArray.CreateBuilder<Overwrite>(overwrites.Length);
  50. for (int i = 0; i < overwrites.Length; i++)
  51. newOverwrites.Add(overwrites[i].ToEntity());
  52. _overwrites = newOverwrites.ToImmutable();
  53. }
  54. public Task ModifyAsync(Action<GuildChannelProperties> func, RequestOptions options = null)
  55. => ChannelHelper.ModifyAsync(this, Discord, func, options);
  56. public Task DeleteAsync(RequestOptions options = null)
  57. => ChannelHelper.DeleteAsync(this, Discord, options);
  58. public OverwritePermissions? GetPermissionOverwrite(IUser user)
  59. {
  60. for (int i = 0; i < _overwrites.Length; i++)
  61. {
  62. if (_overwrites[i].TargetId == user.Id)
  63. return _overwrites[i].Permissions;
  64. }
  65. return null;
  66. }
  67. public OverwritePermissions? GetPermissionOverwrite(IRole role)
  68. {
  69. for (int i = 0; i < _overwrites.Length; i++)
  70. {
  71. if (_overwrites[i].TargetId == role.Id)
  72. return _overwrites[i].Permissions;
  73. }
  74. return null;
  75. }
  76. public async Task AddPermissionOverwriteAsync(IUser user, OverwritePermissions perms, RequestOptions options = null)
  77. {
  78. await ChannelHelper.AddPermissionOverwriteAsync(this, Discord, user, perms, options).ConfigureAwait(false);
  79. _overwrites = _overwrites.Add(new Overwrite(user.Id, PermissionTarget.User, new OverwritePermissions(perms.AllowValue, perms.DenyValue)));
  80. }
  81. public async Task AddPermissionOverwriteAsync(IRole role, OverwritePermissions perms, RequestOptions options = null)
  82. {
  83. await ChannelHelper.AddPermissionOverwriteAsync(this, Discord, role, perms, options).ConfigureAwait(false);
  84. _overwrites = _overwrites.Add(new Overwrite(role.Id, PermissionTarget.Role, new OverwritePermissions(perms.AllowValue, perms.DenyValue)));
  85. }
  86. public async Task RemovePermissionOverwriteAsync(IUser user, RequestOptions options = null)
  87. {
  88. await ChannelHelper.RemovePermissionOverwriteAsync(this, Discord, user, options).ConfigureAwait(false);
  89. for (int i = 0; i < _overwrites.Length; i++)
  90. {
  91. if (_overwrites[i].TargetId == user.Id)
  92. {
  93. _overwrites = _overwrites.RemoveAt(i);
  94. return;
  95. }
  96. }
  97. }
  98. public async Task RemovePermissionOverwriteAsync(IRole role, RequestOptions options = null)
  99. {
  100. await ChannelHelper.RemovePermissionOverwriteAsync(this, Discord, role, options).ConfigureAwait(false);
  101. for (int i = 0; i < _overwrites.Length; i++)
  102. {
  103. if (_overwrites[i].TargetId == role.Id)
  104. {
  105. _overwrites = _overwrites.RemoveAt(i);
  106. return;
  107. }
  108. }
  109. }
  110. public async Task<IReadOnlyCollection<RestInviteMetadata>> GetInvitesAsync(RequestOptions options = null)
  111. => await ChannelHelper.GetInvitesAsync(this, Discord, options).ConfigureAwait(false);
  112. public async Task<RestInviteMetadata> CreateInviteAsync(int? maxAge = 86400, int? maxUses = null, bool isTemporary = false, bool isUnique = false, RequestOptions options = null)
  113. => await ChannelHelper.CreateInviteAsync(this, Discord, maxAge, maxUses, isTemporary, isUnique, options).ConfigureAwait(false);
  114. public new virtual SocketGuildUser GetUser(ulong id) => null;
  115. public override string ToString() => Name;
  116. internal new SocketGuildChannel Clone() => MemberwiseClone() as SocketGuildChannel;
  117. //SocketChannel
  118. internal override IReadOnlyCollection<SocketUser> GetUsersInternal() => Users;
  119. internal override SocketUser GetUserInternal(ulong id) => GetUser(id);
  120. //IGuildChannel
  121. IGuild IGuildChannel.Guild => Guild;
  122. ulong IGuildChannel.GuildId => Guild.Id;
  123. Task<ICategoryChannel> IGuildChannel.GetCategoryAsync()
  124. => Task.FromResult(Category);
  125. async Task<IReadOnlyCollection<IInviteMetadata>> IGuildChannel.GetInvitesAsync(RequestOptions options)
  126. => await GetInvitesAsync(options).ConfigureAwait(false);
  127. async Task<IInviteMetadata> IGuildChannel.CreateInviteAsync(int? maxAge, int? maxUses, bool isTemporary, bool isUnique, RequestOptions options)
  128. => await CreateInviteAsync(maxAge, maxUses, isTemporary, isUnique, options).ConfigureAwait(false);
  129. OverwritePermissions? IGuildChannel.GetPermissionOverwrite(IRole role)
  130. => GetPermissionOverwrite(role);
  131. OverwritePermissions? IGuildChannel.GetPermissionOverwrite(IUser user)
  132. => GetPermissionOverwrite(user);
  133. async Task IGuildChannel.AddPermissionOverwriteAsync(IRole role, OverwritePermissions permissions, RequestOptions options)
  134. => await AddPermissionOverwriteAsync(role, permissions, options).ConfigureAwait(false);
  135. async Task IGuildChannel.AddPermissionOverwriteAsync(IUser user, OverwritePermissions permissions, RequestOptions options)
  136. => await AddPermissionOverwriteAsync(user, permissions, options).ConfigureAwait(false);
  137. async Task IGuildChannel.RemovePermissionOverwriteAsync(IRole role, RequestOptions options)
  138. => await RemovePermissionOverwriteAsync(role, options).ConfigureAwait(false);
  139. async Task IGuildChannel.RemovePermissionOverwriteAsync(IUser user, RequestOptions options)
  140. => await RemovePermissionOverwriteAsync(user, options).ConfigureAwait(false);
  141. IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  142. => ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
  143. Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  144. => Task.FromResult<IGuildUser>(GetUser(id));
  145. //IChannel
  146. IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  147. => ImmutableArray.Create<IReadOnlyCollection<IUser>>(Users).ToAsyncEnumerable(); //Overridden in Text/Voice
  148. Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  149. => Task.FromResult<IUser>(GetUser(id)); //Overridden in Text/Voice
  150. }
  151. }