Browse Source

Cleaned up Hierarchy PR

tags/1.0-rc
RogueException 8 years ago
parent
commit
b1506879db
5 changed files with 47 additions and 6 deletions
  1. +2
    -2
      src/Discord.Net.Core/Entities/Roles/IRole.cs
  2. +18
    -0
      src/Discord.Net.Core/Extensions/RoleExtensions.cs
  3. +2
    -0
      src/Discord.Net.Rest/Entities/Roles/RestRole.cs
  4. +1
    -0
      src/Discord.Net.WebSocket/Entities/Roles/SocketRole.cs
  5. +24
    -4
      src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs

+ 2
- 2
src/Discord.Net.Core/Entities/Roles/IRole.cs View File

@@ -4,7 +4,7 @@ using System.Threading.Tasks;


namespace Discord namespace Discord
{ {
public interface IRole : ISnowflakeEntity, IDeletable, IMentionable
public interface IRole : ISnowflakeEntity, IDeletable, IMentionable, IComparable<IRole>
{ {
/// <summary> Gets the guild owning this role.</summary> /// <summary> Gets the guild owning this role.</summary>
IGuild Guild { get; } IGuild Guild { get; }
@@ -27,4 +27,4 @@ namespace Discord
///// <summary> Modifies this role. </summary> ///// <summary> Modifies this role. </summary>
Task ModifyAsync(Action<ModifyGuildRoleParams> func, RequestOptions options = null); Task ModifyAsync(Action<ModifyGuildRoleParams> func, RequestOptions options = null);
} }
}
}

+ 18
- 0
src/Discord.Net.Core/Extensions/RoleExtensions.cs View File

@@ -0,0 +1,18 @@
namespace Discord
{
internal static class RoleExtensions
{
internal static int Compare(this IRole left, IRole right)
{
if (left == null)
return -1;
if (right == null)
return 1;
var result = left.Position.CompareTo(right.Position);
// As per Discord's documentation, a tie is broken by ID
if (result != 0)
return result;
return left.Id.CompareTo(right.Id);
}
}
}

+ 2
- 0
src/Discord.Net.Rest/Entities/Roles/RestRole.cs View File

@@ -52,6 +52,8 @@ namespace Discord.Rest
public Task DeleteAsync(RequestOptions options = null) public Task DeleteAsync(RequestOptions options = null)
=> RoleHelper.DeleteAsync(this, Discord, options); => RoleHelper.DeleteAsync(this, Discord, options);


public int CompareTo(IRole role) => this.Compare(role);

public override string ToString() => Name; public override string ToString() => Name;
private string DebuggerDisplay => $"{Name} ({Id})"; private string DebuggerDisplay => $"{Name} ({Id})";




+ 1
- 0
src/Discord.Net.WebSocket/Entities/Roles/SocketRole.cs View File

@@ -57,5 +57,6 @@ namespace Discord.WebSocket


//IRole //IRole
IGuild IRole.Guild => Guild; IGuild IRole.Guild => Guild;
public int CompareTo(IRole role) => this.CompareTo(role);
} }
} }

+ 24
- 4
src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs View File

@@ -25,19 +25,39 @@ namespace Discord.WebSocket
public override ushort DiscriminatorValue { get { return GlobalUser.DiscriminatorValue; } internal set { GlobalUser.DiscriminatorValue = value; } } public override ushort DiscriminatorValue { get { return GlobalUser.DiscriminatorValue; } internal set { GlobalUser.DiscriminatorValue = value; } }
public override string AvatarId { get { return GlobalUser.AvatarId; } internal set { GlobalUser.AvatarId = value; } } public override string AvatarId { get { return GlobalUser.AvatarId; } internal set { GlobalUser.AvatarId = value; } }
public GuildPermissions GuildPermissions => new GuildPermissions(Permissions.ResolveGuild(Guild, this)); public GuildPermissions GuildPermissions => new GuildPermissions(Permissions.ResolveGuild(Guild, this));
public IReadOnlyCollection<ulong> RoleIds => _roleIds;
internal override SocketPresence Presence { get { return GlobalUser.Presence; } set { GlobalUser.Presence = value; } } internal override SocketPresence Presence { get { return GlobalUser.Presence; } set { GlobalUser.Presence = value; } }


public SocketVoiceState? VoiceState => Guild.GetVoiceState(Id);
public bool IsSelfDeafened => VoiceState?.IsSelfDeafened ?? false; public bool IsSelfDeafened => VoiceState?.IsSelfDeafened ?? false;
public bool IsSelfMuted => VoiceState?.IsSelfMuted ?? false; public bool IsSelfMuted => VoiceState?.IsSelfMuted ?? false;
public bool IsSuppressed => VoiceState?.IsSuppressed ?? false; public bool IsSuppressed => VoiceState?.IsSuppressed ?? false;
public SocketVoiceChannel VoiceChannel => VoiceState?.VoiceChannel;
public bool IsDeafened => VoiceState?.IsDeafened ?? false; public bool IsDeafened => VoiceState?.IsDeafened ?? false;
public bool IsMuted => VoiceState?.IsMuted ?? false; public bool IsMuted => VoiceState?.IsMuted ?? false;
public DateTimeOffset? JoinedAt => DateTimeUtils.FromTicks(_joinedAtTicks);
public IReadOnlyCollection<ulong> RoleIds => _roleIds;
public SocketVoiceChannel VoiceChannel => VoiceState?.VoiceChannel;
public string VoiceSessionId => VoiceState?.VoiceSessionId ?? ""; public string VoiceSessionId => VoiceState?.VoiceSessionId ?? "";
public SocketVoiceState? VoiceState => Guild.GetVoiceState(Id);


public DateTimeOffset? JoinedAt => DateTimeUtils.FromTicks(_joinedAtTicks);
/// <summary> The position of the user within the role hirearchy. </summary>
/// <remarks> The returned value equal to the position of the highest role the user has,
/// or int.MaxValue if user is the server owner. </remarks>
public int Hierarchy
{
get
{
if (Guild.OwnerId == Id)
return int.MaxValue;

int maxPos = 0;
for (int i = 0; i < _roleIds.Length; i++)
{
var role = Guild.GetRole(_roleIds[i]);
if (role != null && role.Position > maxPos)
maxPos = role.Position;
}
return maxPos;
}
}


internal SocketGuildUser(SocketGuild guild, SocketGlobalUser globalUser) internal SocketGuildUser(SocketGuild guild, SocketGlobalUser globalUser)
: base(guild.Discord, globalUser.Id) : base(guild.Discord, globalUser.Id)


Loading…
Cancel
Save