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.

SocketVoiceChannel.cs 3.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Discord.Audio;
  2. using Discord.Rest;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.Immutable;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using Model = Discord.API.Channel;
  10. namespace Discord.WebSocket
  11. {
  12. /// <summary>
  13. /// Represents a WebSocket-based voice channel in a guild.
  14. /// </summary>
  15. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  16. public class SocketVoiceChannel : SocketGuildChannel, IVoiceChannel, ISocketAudioChannel
  17. {
  18. /// <inheritdoc />
  19. public int Bitrate { get; private set; }
  20. /// <inheritdoc />
  21. public int? UserLimit { get; private set; }
  22. public ulong? CategoryId { get; private set; }
  23. public ICategoryChannel Category
  24. => CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null;
  25. /// <inheritdoc />
  26. public override IReadOnlyCollection<SocketGuildUser> Users
  27. => Guild.Users.Where(x => x.VoiceChannel?.Id == Id).ToImmutableArray();
  28. internal SocketVoiceChannel(DiscordSocketClient discord, ulong id, SocketGuild guild)
  29. : base(discord, id, guild)
  30. {
  31. }
  32. internal new static SocketVoiceChannel Create(SocketGuild guild, ClientState state, Model model)
  33. {
  34. var entity = new SocketVoiceChannel(guild.Discord, model.Id, guild);
  35. entity.Update(state, model);
  36. return entity;
  37. }
  38. internal override void Update(ClientState state, Model model)
  39. {
  40. base.Update(state, model);
  41. CategoryId = model.CategoryId;
  42. Bitrate = model.Bitrate.Value;
  43. UserLimit = model.UserLimit.Value != 0 ? model.UserLimit.Value : (int?)null;
  44. }
  45. /// <inheritdoc />
  46. public Task ModifyAsync(Action<VoiceChannelProperties> func, RequestOptions options = null)
  47. => ChannelHelper.ModifyAsync(this, Discord, func, options);
  48. /// <inheritdoc />
  49. public async Task<IAudioClient> ConnectAsync(bool selfDeaf, bool selfMute, bool external)
  50. {
  51. return await Guild.ConnectAudioAsync(Id, selfDeaf, selfMute, external).ConfigureAwait(false);
  52. }
  53. public async Task DisconnectAsync()
  54. => await Guild.DisconnectAudioAsync();
  55. /// <inheritdoc />
  56. public override SocketGuildUser GetUser(ulong id)
  57. {
  58. var user = Guild.GetUser(id);
  59. if (user?.VoiceChannel?.Id == Id)
  60. return user;
  61. return null;
  62. }
  63. private string DebuggerDisplay => $"{Name} ({Id}, Voice)";
  64. internal new SocketVoiceChannel Clone() => MemberwiseClone() as SocketVoiceChannel;
  65. //IGuildChannel
  66. /// <inheritdoc />
  67. Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  68. => Task.FromResult<IGuildUser>(GetUser(id));
  69. /// <inheritdoc />
  70. IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  71. => ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
  72. // INestedChannel
  73. Task<ICategoryChannel> INestedChannel.GetCategoryAsync(CacheMode mode, RequestOptions options)
  74. => Task.FromResult(Category);
  75. }
  76. }