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.

RpcVoiceState.cs 2.9 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Diagnostics;
  3. using Model = Discord.API.Rpc.ExtendedVoiceState;
  4. namespace Discord.Rpc
  5. {
  6. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  7. public class RpcVoiceState : IVoiceState
  8. {
  9. [Flags]
  10. private enum Flags : byte
  11. {
  12. Normal = 0x00,
  13. Suppressed = 0x01,
  14. Muted = 0x02,
  15. Deafened = 0x04,
  16. SelfMuted = 0x08,
  17. SelfDeafened = 0x10,
  18. }
  19. private Flags _voiceStates;
  20. public RpcUser User { get; }
  21. public string Nickname { get; private set; }
  22. public int Volume { get; private set; }
  23. public bool IsMuted2 { get; private set; }
  24. public Pan Pan { get; private set; }
  25. public bool IsMuted => (_voiceStates & Flags.Muted) != 0;
  26. public bool IsDeafened => (_voiceStates & Flags.Deafened) != 0;
  27. public bool IsSuppressed => (_voiceStates & Flags.Suppressed) != 0;
  28. public bool IsSelfMuted => (_voiceStates & Flags.SelfMuted) != 0;
  29. public bool IsSelfDeafened => (_voiceStates & Flags.SelfDeafened) != 0;
  30. internal RpcVoiceState(DiscordRpcClient discord, ulong userId)
  31. {
  32. User = new RpcUser(discord, userId);
  33. }
  34. internal static RpcVoiceState Create(DiscordRpcClient discord, Model model)
  35. {
  36. var entity = new RpcVoiceState(discord, model.User.Id);
  37. entity.Update(model);
  38. return entity;
  39. }
  40. internal void Update(Model model)
  41. {
  42. if (model.VoiceState.IsSpecified)
  43. {
  44. Flags voiceStates = Flags.Normal;
  45. if (model.VoiceState.Value.Mute)
  46. voiceStates |= Flags.Muted;
  47. if (model.VoiceState.Value.Deaf)
  48. voiceStates |= Flags.Deafened;
  49. if (model.VoiceState.Value.SelfMute)
  50. voiceStates |= Flags.SelfMuted;
  51. if (model.VoiceState.Value.SelfDeaf)
  52. voiceStates |= Flags.SelfDeafened;
  53. if (model.VoiceState.Value.Suppress)
  54. voiceStates |= Flags.Suppressed;
  55. _voiceStates = voiceStates;
  56. }
  57. User.Update(model.User);
  58. if (model.Nickname.IsSpecified)
  59. Nickname = model.Nickname.Value;
  60. if (model.Volume.IsSpecified)
  61. Volume = model.Volume.Value;
  62. if (model.Mute.IsSpecified)
  63. IsMuted2 = model.Mute.Value;
  64. if (model.Pan.IsSpecified)
  65. Pan = Pan.Create(model.Pan.Value);
  66. }
  67. public override string ToString() => User.ToString();
  68. private string DebuggerDisplay => $"{User} ({_voiceStates})";
  69. string IVoiceState.VoiceSessionId { get { throw new NotSupportedException(); } }
  70. IVoiceChannel IVoiceState.VoiceChannel { get { throw new NotSupportedException(); } }
  71. }
  72. }