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.

RestGuildUser.cs 5.1 kB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Immutable;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Model = Discord.API.GuildMember;
  8. namespace Discord.Rest
  9. {
  10. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  11. public class RestGuildUser : RestUser, IGuildUser, IUpdateable
  12. {
  13. private long? _joinedAtTicks;
  14. private ImmutableArray<ulong> _roleIds;
  15. public string Nickname { get; private set; }
  16. internal IGuild Guild { get; private set; }
  17. public bool IsDeafened { get; private set; }
  18. public bool IsMuted { get; private set; }
  19. public ulong GuildId => Guild.Id;
  20. public GuildPermissions GuildPermissions
  21. {
  22. get
  23. {
  24. if (!Guild.Available)
  25. throw new InvalidOperationException("Resolving permissions requires the parent guild to be downloaded.");
  26. return new GuildPermissions(Permissions.ResolveGuild(Guild, this));
  27. }
  28. }
  29. public IReadOnlyCollection<ulong> RoleIds => _roleIds;
  30. public DateTimeOffset? JoinedAt => DateTimeUtils.FromTicks(_joinedAtTicks);
  31. internal RestGuildUser(BaseDiscordClient discord, IGuild guild, ulong id)
  32. : base(discord, id)
  33. {
  34. Guild = guild;
  35. }
  36. internal static RestGuildUser Create(BaseDiscordClient discord, IGuild guild, Model model)
  37. {
  38. var entity = new RestGuildUser(discord, guild, model.User.Id);
  39. entity.Update(model);
  40. return entity;
  41. }
  42. internal void Update(Model model)
  43. {
  44. if (model.JoinedAt.IsSpecified)
  45. _joinedAtTicks = model.JoinedAt.Value.UtcTicks;
  46. if (model.Nick.IsSpecified)
  47. Nickname = model.Nick.Value;
  48. if (model.Deaf.IsSpecified)
  49. IsDeafened = model.Deaf.Value;
  50. if (model.Mute.IsSpecified)
  51. IsMuted = model.Mute.Value;
  52. if (model.Roles.IsSpecified)
  53. UpdateRoles(model.Roles.Value);
  54. }
  55. private void UpdateRoles(ulong[] roleIds)
  56. {
  57. var roles = ImmutableArray.CreateBuilder<ulong>(roleIds.Length + 1);
  58. roles.Add(Guild.Id);
  59. for (int i = 0; i < roleIds.Length; i++)
  60. roles.Add(roleIds[i]);
  61. _roleIds = roles.ToImmutable();
  62. }
  63. public override async Task UpdateAsync(RequestOptions options = null)
  64. {
  65. var model = await Discord.ApiClient.GetGuildMemberAsync(GuildId, Id, options).ConfigureAwait(false);
  66. Update(model);
  67. }
  68. public async Task ModifyAsync(Action<GuildUserProperties> func, RequestOptions options = null)
  69. {
  70. var args = await UserHelper.ModifyAsync(this, Discord, func, options).ConfigureAwait(false);
  71. if (args.Deaf.IsSpecified)
  72. IsDeafened = args.Deaf.Value;
  73. if (args.Mute.IsSpecified)
  74. IsMuted = args.Mute.Value;
  75. if (args.Nickname.IsSpecified)
  76. Nickname = args.Nickname.Value;
  77. if (args.Roles.IsSpecified)
  78. UpdateRoles(args.Roles.Value.Select(x => x.Id).ToArray());
  79. else if (args.RoleIds.IsSpecified)
  80. UpdateRoles(args.RoleIds.Value.ToArray());
  81. }
  82. public Task KickAsync(string reason = null, RequestOptions options = null)
  83. => UserHelper.KickAsync(this, Discord, reason, options);
  84. /// <inheritdoc />
  85. public Task AddRoleAsync(IRole role, RequestOptions options = null)
  86. => AddRolesAsync(new[] { role }, options);
  87. /// <inheritdoc />
  88. public Task AddRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null)
  89. => UserHelper.AddRolesAsync(this, Discord, roles, options);
  90. /// <inheritdoc />
  91. public Task RemoveRoleAsync(IRole role, RequestOptions options = null)
  92. => RemoveRolesAsync(new[] { role }, options);
  93. /// <inheritdoc />
  94. public Task RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options = null)
  95. => UserHelper.RemoveRolesAsync(this, Discord, roles, options);
  96. public ChannelPermissions GetPermissions(IGuildChannel channel)
  97. {
  98. var guildPerms = GuildPermissions;
  99. return new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, guildPerms.RawValue));
  100. }
  101. //IGuildUser
  102. IGuild IGuildUser.Guild
  103. {
  104. get
  105. {
  106. if (Guild != null)
  107. return Guild;
  108. throw new InvalidOperationException("Unable to return this entity's parent unless it was fetched through that object.");
  109. }
  110. }
  111. //IVoiceState
  112. bool IVoiceState.IsSelfDeafened => false;
  113. bool IVoiceState.IsSelfMuted => false;
  114. bool IVoiceState.IsSuppressed => false;
  115. IVoiceChannel IVoiceState.VoiceChannel => null;
  116. string IVoiceState.VoiceSessionId => null;
  117. }
  118. }