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.

SocketVoiceState.cs 2.5 kB

9 years ago
9 years ago
9 years ago
9 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Diagnostics;
  3. using Model = Discord.API.VoiceState;
  4. namespace Discord.WebSocket
  5. {
  6. //TODO: C#7 Candidate for record type
  7. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  8. public struct SocketVoiceState : IVoiceState
  9. {
  10. public static readonly SocketVoiceState Default = new SocketVoiceState(null, null, false, false, false, false, false);
  11. [Flags]
  12. private enum Flags : byte
  13. {
  14. Normal = 0x00,
  15. Suppressed = 0x01,
  16. Muted = 0x02,
  17. Deafened = 0x04,
  18. SelfMuted = 0x08,
  19. SelfDeafened = 0x10,
  20. }
  21. private readonly Flags _voiceStates;
  22. public SocketVoiceChannel VoiceChannel { get; }
  23. public string VoiceSessionId { get; }
  24. public bool IsMuted => (_voiceStates & Flags.Muted) != 0;
  25. public bool IsDeafened => (_voiceStates & Flags.Deafened) != 0;
  26. public bool IsSuppressed => (_voiceStates & Flags.Suppressed) != 0;
  27. public bool IsSelfMuted => (_voiceStates & Flags.SelfMuted) != 0;
  28. public bool IsSelfDeafened => (_voiceStates & Flags.SelfDeafened) != 0;
  29. internal SocketVoiceState(SocketVoiceChannel voiceChannel, string sessionId, bool isSelfMuted, bool isSelfDeafened, bool isMuted, bool isDeafened, bool isSuppressed)
  30. {
  31. VoiceChannel = voiceChannel;
  32. VoiceSessionId = sessionId;
  33. Flags voiceStates = Flags.Normal;
  34. if (isSelfMuted)
  35. voiceStates |= Flags.SelfMuted;
  36. if (isSelfDeafened)
  37. voiceStates |= Flags.SelfDeafened;
  38. if (isMuted)
  39. voiceStates |= Flags.Muted;
  40. if (isDeafened)
  41. voiceStates |= Flags.Deafened;
  42. if (isSuppressed)
  43. voiceStates |= Flags.Suppressed;
  44. _voiceStates = voiceStates;
  45. }
  46. internal static SocketVoiceState Create(SocketVoiceChannel voiceChannel, Model model)
  47. {
  48. return new SocketVoiceState(voiceChannel, model.SessionId, model.SelfMute, model.SelfDeaf, model.Mute, model.Deaf, model.Suppress);
  49. }
  50. public override string ToString() => VoiceChannel?.Name ?? "Unknown";
  51. private string DebuggerDisplay => $"{VoiceChannel?.Name ?? "Unknown"} ({_voiceStates})";
  52. internal SocketVoiceState Clone() => this;
  53. IVoiceChannel IVoiceState.VoiceChannel => VoiceChannel;
  54. }
  55. }