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.5 kB

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