Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add team member Role property #2965

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions src/Discord.Net.Core/Entities/Teams/ITeamMember.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
namespace Discord
namespace Discord;

/// <summary>
/// Represents a Discord Team member.
/// </summary>
public interface ITeamMember
{
/// <summary>
/// Represents a Discord Team member.
/// Gets the membership state of this team member.
/// </summary>
public interface ITeamMember
{
/// <summary>
/// Gets the membership state of this team member.
/// </summary>
MembershipState MembershipState { get; }
/// <summary>
/// Gets the permissions of this team member.
/// </summary>
string[] Permissions { get; }
/// <summary>
/// Gets the team unique identifier for this team member.
/// </summary>
ulong TeamId { get; }
/// <summary>
/// Gets the Discord user of this team member.
/// </summary>
IUser User { get; }
}
MembershipState MembershipState { get; }

/// <summary>
/// Gets the permissions of this team member.
/// </summary>
string[] Permissions { get; }

/// <summary>
/// Gets the team unique identifier for this team member.
/// </summary>
ulong TeamId { get; }

/// <summary>
/// Gets the Discord user of this team member.
/// </summary>
IUser User { get; }

/// <summary>
/// Gets the role of this team member.
/// </summary>
public TeamRole Role { get; }
}
27 changes: 27 additions & 0 deletions src/Discord.Net.Core/Entities/Teams/TeamRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Discord;

/// <summary>
/// Represents a Discord Team member role.
/// </summary>
public enum TeamRole
{
/// <summary>
/// The user is the owner of the team.
/// </summary>
Owner,

/// <summary>
/// The user is an admin in the team.
/// </summary>
Admin,

/// <summary>
/// The user is a developer in the team.
/// </summary>
Developer,

/// <summary>
/// The user is a read-only member of the team.
/// </summary>
ReadOnly,
}
29 changes: 17 additions & 12 deletions src/Discord.Net.Rest/API/Common/TeamMember.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
using Newtonsoft.Json;

namespace Discord.API
namespace Discord.API;

internal class TeamMember
{
internal class TeamMember
{
[JsonProperty("membership_state")]
public MembershipState MembershipState { get; set; }
[JsonProperty("permissions")]
public string[] Permissions { get; set; }
[JsonProperty("team_id")]
public ulong TeamId { get; set; }
[JsonProperty("user")]
public User User { get; set; }
}
[JsonProperty("membership_state")]
public MembershipState MembershipState { get; set; }

[JsonProperty("permissions")]
public string[] Permissions { get; set; }

[JsonProperty("team_id")]
public ulong TeamId { get; set; }

[JsonProperty("user")]
public User User { get; set; }

[JsonProperty("role")]
public string Role { get; set; }
}
2 changes: 1 addition & 1 deletion src/Discord.Net.Rest/Entities/Teams/RestTeam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal virtual void Update(Model model)
_iconId = model.Icon.Value;
Name = model.Name;
OwnerUserId = model.OwnerUserId;
TeamMembers = model.TeamMembers.Select(x => new RestTeamMember(Discord, x)).ToImmutableArray();
TeamMembers = model.TeamMembers.Select(x => new RestTeamMember(Discord, x, model)).ToImmutableArray();
}
}
}
58 changes: 38 additions & 20 deletions src/Discord.Net.Rest/Entities/Teams/RestTeamMember.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
using System;

using Model = Discord.API.TeamMember;
using TeamModel = Discord.API.Team;

namespace Discord.Rest;

namespace Discord.Rest
public class RestTeamMember : ITeamMember
{
public class RestTeamMember : ITeamMember
/// <inheritdoc />
public MembershipState MembershipState { get; }

/// <inheritdoc />
public string[] Permissions { get; }

/// <inheritdoc />
public ulong TeamId { get; }

/// <inheritdoc />
public IUser User { get; }

/// <inheritdoc />
public TeamRole Role { get; }

internal RestTeamMember(BaseDiscordClient discord, Model model, TeamModel team)
{
/// <inheritdoc />
public MembershipState MembershipState { get; }
/// <inheritdoc />
public string[] Permissions { get; }
/// <inheritdoc />
public ulong TeamId { get; }
/// <inheritdoc />
public IUser User { get; }

internal RestTeamMember(BaseDiscordClient discord, Model model)
MembershipState = model.MembershipState switch
{
MembershipState = model.MembershipState switch
API.MembershipState.Invited => MembershipState.Invited,
API.MembershipState.Accepted => MembershipState.Accepted,
_ => throw new InvalidOperationException("Invalid membership state"),
};
Permissions = model.Permissions;
TeamId = model.TeamId;
User = RestUser.Create(discord, model.User);

if (team.OwnerUserId == model.User.Id)
Role = TeamRole.Owner;
else
Role = model.Role switch
{
API.MembershipState.Invited => MembershipState.Invited,
API.MembershipState.Accepted => MembershipState.Accepted,
_ => throw new InvalidOperationException("Invalid membership state"),
"admin" => TeamRole.Admin,
"developer" => TeamRole.Developer,
"read_only" => TeamRole.ReadOnly,
_ => 0
};
Permissions = model.Permissions;
TeamId = model.TeamId;
User = RestUser.Create(discord, model.User);
}
}
}