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.

SocketRole.cs 3.6 kB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using Discord.Rest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Model = Discord.API.Role;
  8. namespace Discord.WebSocket
  9. {
  10. /// <summary>
  11. /// Represents a WebSocket-based role to be given to a guild user.
  12. /// </summary>
  13. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  14. public class SocketRole : SocketEntity<ulong>, IRole
  15. {
  16. /// <summary>
  17. /// Gets the guild that owns this role.
  18. /// </summary>
  19. /// <returns>
  20. /// A <see cref="SocketGuild"/> representing the parent guild of this role.
  21. /// </returns>
  22. public SocketGuild Guild { get; }
  23. /// <inheritdoc />
  24. public Color Color { get; private set; }
  25. /// <inheritdoc />
  26. public bool IsHoisted { get; private set; }
  27. /// <inheritdoc />
  28. public bool IsManaged { get; private set; }
  29. /// <inheritdoc />
  30. public bool IsMentionable { get; private set; }
  31. /// <inheritdoc />
  32. public string Name { get; private set; }
  33. /// <inheritdoc />
  34. public GuildPermissions Permissions { get; private set; }
  35. /// <inheritdoc />
  36. public int Position { get; private set; }
  37. /// <inheritdoc />
  38. public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
  39. /// <summary>
  40. /// Returns a value that determines if the role is an @everyone role.
  41. /// </summary>
  42. /// <returns>
  43. /// <c>true</c> if the role is @everyone; otherwise <c>false</c>.
  44. /// </returns>
  45. public bool IsEveryone => Id == Guild.Id;
  46. /// <inheritdoc />
  47. public string Mention => IsEveryone ? "@everyone" : MentionUtils.MentionRole(Id);
  48. public IEnumerable<SocketGuildUser> Members
  49. => Guild.Users.Where(x => x.Roles.Any(r => r.Id == Id));
  50. internal SocketRole(SocketGuild guild, ulong id)
  51. : base(guild.Discord, id)
  52. {
  53. Guild = guild;
  54. }
  55. internal static SocketRole Create(SocketGuild guild, ClientState state, Model model)
  56. {
  57. var entity = new SocketRole(guild, model.Id);
  58. entity.Update(state, model);
  59. return entity;
  60. }
  61. internal void Update(ClientState state, Model model)
  62. {
  63. Name = model.Name;
  64. IsHoisted = model.Hoist;
  65. IsManaged = model.Managed;
  66. IsMentionable = model.Mentionable;
  67. Position = model.Position;
  68. Color = new Color(model.Color);
  69. Permissions = new GuildPermissions(model.Permissions);
  70. }
  71. /// <inheritdoc />
  72. public Task ModifyAsync(Action<RoleProperties> func, RequestOptions options = null)
  73. => RoleHelper.ModifyAsync(this, Discord, func, options);
  74. /// <inheritdoc />
  75. public Task DeleteAsync(RequestOptions options = null)
  76. => RoleHelper.DeleteAsync(this, Discord, options);
  77. /// <summary>
  78. /// Gets the name of the role.
  79. /// </summary>
  80. /// <returns>
  81. /// A string that resolves to <see cref="Discord.WebSocket.SocketRole.Name" />.
  82. /// </returns>
  83. public override string ToString() => Name;
  84. private string DebuggerDisplay => $"{Name} ({Id})";
  85. internal SocketRole Clone() => MemberwiseClone() as SocketRole;
  86. /// <inheritdoc />
  87. public int CompareTo(IRole role) => RoleUtils.Compare(this, role);
  88. //IRole
  89. /// <inheritdoc />
  90. IGuild IRole.Guild => Guild;
  91. }
  92. }