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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  13. public class SocketVoiceChannel : SocketGuildChannel, IVoiceChannel, ISocketAudioChannel
  14. {
  15. public int Bitrate { get; private set; }
  16. public int? UserLimit { get; private set; }
  17. public override IReadOnlyCollection<SocketGuildUser> Users
  18. => Guild.Users.Where(x => x.VoiceChannel?.Id == Id).ToImmutableArray();
  19. internal SocketVoiceChannel(DiscordSocketClient discord, ulong id, SocketGuild guild)
  20. : base(discord, id, guild)
  21. {
  22. }
  23. internal new static SocketVoiceChannel Create(SocketGuild guild, ClientState state, Model model)
  24. {
  25. var entity = new SocketVoiceChannel(guild.Discord, model.Id, guild);
  26. entity.Update(state, model);
  27. return entity;
  28. }
  29. internal override void Update(ClientState state, Model model)
  30. {
  31. base.Update(state, model);
  32. Bitrate = model.Bitrate.Value;
  33. UserLimit = model.UserLimit.Value != 0 ? model.UserLimit.Value : (int?)null;
  34. }
  35. public Task ModifyAsync(Action<VoiceChannelProperties> func, RequestOptions options = null)
  36. => ChannelHelper.ModifyAsync(this, Discord, func, options);
  37. public async Task<IAudioClient> ConnectAsync(Action<IAudioClient> configAction = null)
  38. {
  39. return await Guild.ConnectAudioAsync(Id, false, false, configAction).ConfigureAwait(false);
  40. }
  41. public override SocketGuildUser GetUser(ulong id)
  42. {
  43. var user = Guild.GetUser(id);
  44. if (user?.VoiceChannel?.Id == Id)
  45. return user;
  46. return null;
  47. }
  48. private string DebuggerDisplay => $"{Name} ({Id}, Voice)";
  49. internal new SocketVoiceChannel Clone() => MemberwiseClone() as SocketVoiceChannel;
  50. //IGuildChannel
  51. Task<IGuildUser> IGuildChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
  52. => Task.FromResult<IGuildUser>(GetUser(id));
  53. IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> IGuildChannel.GetUsersAsync(CacheMode mode, RequestOptions options)
  54. => ImmutableArray.Create<IReadOnlyCollection<IGuildUser>>(Users).ToAsyncEnumerable();
  55. }
  56. }