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 2.8 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. [DebuggerDisplay(@"{DebuggerDisplay,nq}")]
  11. public class SocketRole : SocketEntity<ulong>, IRole
  12. {
  13. public SocketGuild Guild { get; }
  14. /// <inheritdoc />
  15. public Color Color { get; private set; }
  16. /// <inheritdoc />
  17. public bool IsHoisted { get; private set; }
  18. /// <inheritdoc />
  19. public bool IsManaged { get; private set; }
  20. /// <inheritdoc />
  21. public bool IsMentionable { get; private set; }
  22. /// <inheritdoc />
  23. public string Name { get; private set; }
  24. /// <inheritdoc />
  25. public GuildPermissions Permissions { get; private set; }
  26. /// <inheritdoc />
  27. public int Position { get; private set; }
  28. /// <inheritdoc />
  29. public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
  30. public bool IsEveryone => Id == Guild.Id;
  31. /// <inheritdoc />
  32. public string Mention => IsEveryone ? "@everyone" : MentionUtils.MentionRole(Id);
  33. public IEnumerable<SocketGuildUser> Members
  34. => Guild.Users.Where(x => x.Roles.Any(r => r.Id == Id));
  35. internal SocketRole(SocketGuild guild, ulong id)
  36. : base(guild.Discord, id)
  37. {
  38. Guild = guild;
  39. }
  40. internal static SocketRole Create(SocketGuild guild, ClientState state, Model model)
  41. {
  42. var entity = new SocketRole(guild, model.Id);
  43. entity.Update(state, model);
  44. return entity;
  45. }
  46. internal void Update(ClientState state, Model model)
  47. {
  48. Name = model.Name;
  49. IsHoisted = model.Hoist;
  50. IsManaged = model.Managed;
  51. IsMentionable = model.Mentionable;
  52. Position = model.Position;
  53. Color = new Color(model.Color);
  54. Permissions = new GuildPermissions(model.Permissions);
  55. }
  56. /// <inheritdoc />
  57. public Task ModifyAsync(Action<RoleProperties> func, RequestOptions options = null)
  58. => RoleHelper.ModifyAsync(this, Discord, func, options);
  59. /// <inheritdoc />
  60. public Task DeleteAsync(RequestOptions options = null)
  61. => RoleHelper.DeleteAsync(this, Discord, options);
  62. public override string ToString() => Name;
  63. private string DebuggerDisplay => $"{Name} ({Id})";
  64. internal SocketRole Clone() => MemberwiseClone() as SocketRole;
  65. /// <inheritdoc />
  66. public int CompareTo(IRole role) => RoleUtils.Compare(this, role);
  67. //IRole
  68. /// <inheritdoc />
  69. IGuild IRole.Guild => Guild;
  70. }
  71. }