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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using Discord.API.Rest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.Immutable;
  5. using System.Diagnostics;
  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 ulong GuildId => Guild.Id;
  18. public GuildPermissions GuildPermissions
  19. {
  20. get
  21. {
  22. if (!Guild.Available)
  23. throw new InvalidOperationException("Resolving permissions requires the parent guild to be downloaded.");
  24. return new GuildPermissions(Permissions.ResolveGuild(Guild, this));
  25. }
  26. }
  27. public IReadOnlyCollection<ulong> RoleIds => _roleIds;
  28. public DateTimeOffset? JoinedAt => DateTimeUtils.FromTicks(_joinedAtTicks);
  29. internal RestGuildUser(BaseDiscordClient discord, IGuild guild, ulong id)
  30. : base(discord, id)
  31. {
  32. Guild = guild;
  33. }
  34. internal static RestGuildUser Create(BaseDiscordClient discord, IGuild guild, Model model)
  35. {
  36. var entity = new RestGuildUser(discord, guild, model.User.Id);
  37. entity.Update(model);
  38. return entity;
  39. }
  40. internal void Update(Model model)
  41. {
  42. _joinedAtTicks = model.JoinedAt.UtcTicks;
  43. if (model.Nick.IsSpecified)
  44. Nickname = model.Nick.Value;
  45. UpdateRoles(model.Roles);
  46. }
  47. private void UpdateRoles(ulong[] roleIds)
  48. {
  49. var roles = ImmutableArray.CreateBuilder<ulong>(roleIds.Length + 1);
  50. roles.Add(Guild.Id);
  51. for (int i = 0; i < roleIds.Length; i++)
  52. roles.Add(roleIds[i]);
  53. _roleIds = roles.ToImmutable();
  54. }
  55. public override async Task UpdateAsync()
  56. => Update(await UserHelper.GetAsync(this, Discord));
  57. public Task ModifyAsync(Action<ModifyGuildMemberParams> func)
  58. => UserHelper.ModifyAsync(this, Discord, func);
  59. public Task KickAsync()
  60. => UserHelper.KickAsync(this, Discord);
  61. public ChannelPermissions GetPermissions(IGuildChannel channel)
  62. {
  63. var guildPerms = GuildPermissions;
  64. return new ChannelPermissions(Permissions.ResolveChannel(Guild, this, channel, guildPerms.RawValue));
  65. }
  66. //IVoiceState
  67. bool IVoiceState.IsDeafened => false;
  68. bool IVoiceState.IsMuted => false;
  69. bool IVoiceState.IsSelfDeafened => false;
  70. bool IVoiceState.IsSelfMuted => false;
  71. bool IVoiceState.IsSuppressed => false;
  72. IVoiceChannel IVoiceState.VoiceChannel => null;
  73. string IVoiceState.VoiceSessionId => null;
  74. }
  75. }