Skip to content
Open
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
21 changes: 21 additions & 0 deletions NetCord/JsonModels/JsonPartialRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.Json.Serialization;

namespace NetCord.JsonModels;

public class JsonPartialRole : JsonEntity
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("position")]
public int Position { get; set; }

[JsonPropertyName("colors")]
public JsonRoleColors Colors { get; set; }

[JsonPropertyName("icon")]
public string? IconHash { get; set; }

[JsonPropertyName("unicode_emoji")]
public string? UnicodeEmoji { get; set; }
}
18 changes: 2 additions & 16 deletions NetCord/JsonModels/JsonRole.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
using System.ComponentModel;
Comment thread
jedrek0429 marked this conversation as resolved.
using System.Text.Json.Serialization;

namespace NetCord.JsonModels;

public class JsonRole : JsonEntity
public class JsonRole : JsonPartialRole
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("colors")]
public JsonRoleColors Colors { get; set; }

[JsonPropertyName("hoist")]
public bool Hoist { get; set; }

[JsonPropertyName("icon")]
public string? IconHash { get; set; }

[JsonPropertyName("unicode_emoji")]
public string? UnicodeEmoji { get; set; }

[JsonPropertyName("position")]
public int Position { get; set; }

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

Expand Down
64 changes: 64 additions & 0 deletions NetCord/PartialRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using NetCord.JsonModels;
using NetCord.Rest;

namespace NetCord;

/// <summary>
/// Represents a partial role.
/// </summary>
/// <remarks>
/// Useful for <see cref="Rest.RestInvite"/>.
/// </remarks>
public partial class PartialRole(JsonPartialRole jsonModel, ulong guildId, RestClient client)
: ClientEntity(client), IJsonModel<JsonPartialRole>
{
JsonPartialRole IJsonModel<JsonPartialRole>.JsonModel => jsonModel;

/// <summary>
/// The role's ID.
/// </summary>
public override ulong Id => jsonModel.Id;

/// <summary>
/// The name of the role.
/// </summary>
public string Name => jsonModel.Name;

/// <summary>
/// The role's colors.
/// </summary>
public RoleColors Colors => new(jsonModel.Colors);

/// <summary>
/// The raw position of this role.
/// </summary>
/// <remarks>
/// Use <see cref="Position"/> to get a properly comparable and sortable position value.
/// </remarks>
public int RawPosition => jsonModel.Position;

/// <summary>
/// The position of this role for sorting and comparing.
/// </summary>
public RolePosition Position => new(RawPosition, Id);

/// <summary>
/// The role's icon hash.
/// </summary>
public string? IconHash => jsonModel.IconHash;

/// <summary>
/// The role's Unicode emoji.
/// </summary>
public string? UnicodeEmoji => jsonModel.UnicodeEmoji;

/// <summary>
/// The ID of the guild this role belongs to.
/// </summary>
public ulong GuildId => guildId;

public override string ToString() => $"<@&{Id}>";

public override bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
=> Mention.TryFormatRole(destination, out charsWritten, Id);
}
2 changes: 1 addition & 1 deletion NetCord/Rest/JsonModels/JsonRestInvite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class JsonRestInvite
public InviteFlags? Flags { get; set; }

[JsonPropertyName("roles")]
public JsonRole[]? Roles { get; set; }
public JsonPartialRole[]? Roles { get; set; }

[JsonPropertyName("uses")]
public int? Uses { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions NetCord/Rest/RestClient.Guild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public async Task<IReadOnlyList<Role>> ModifyGuildRolePositionsAsync(ulong guild
}

[GenerateAlias([typeof(RestGuild)], nameof(RestGuild.Id), TypeNameOverride = nameof(Guild))]
[GenerateAlias([typeof(Role)], nameof(Role.GuildId), nameof(Role.Id), TypeNameOverride = $"{nameof(Guild)}{nameof(Role)}")]
[GenerateAlias([typeof(PartialRole)], nameof(PartialRole.GuildId), nameof(PartialRole.Id), TypeNameOverride = $"{nameof(Guild)}{nameof(Role)}")]
public async Task<Role> ModifyGuildRoleAsync(ulong guildId, ulong roleId, Action<RoleOptions> action, RestRequestProperties? properties = null, CancellationToken cancellationToken = default)
{
RoleOptions obj = new();
Expand All @@ -207,7 +207,7 @@ public async Task<Role> ModifyGuildRoleAsync(ulong guildId, ulong roleId, Action
}

[GenerateAlias([typeof(RestGuild)], nameof(RestGuild.Id), TypeNameOverride = nameof(Guild))]
[GenerateAlias([typeof(Role)], nameof(Role.GuildId), nameof(Role.Id), TypeNameOverride = $"{nameof(Guild)}{nameof(Role)}")]
[GenerateAlias([typeof(PartialRole)], nameof(PartialRole.GuildId), nameof(PartialRole.Id), TypeNameOverride = $"{nameof(Guild)}{nameof(Role)}")]
public Task DeleteGuildRoleAsync(ulong guildId, ulong roleId, RestRequestProperties? properties = null, CancellationToken cancellationToken = default)
=> SendRequestAsync(HttpMethod.Delete, $"/guilds/{guildId}/roles/{roleId}", null, new(guildId), properties, cancellationToken: cancellationToken);

Expand Down
4 changes: 2 additions & 2 deletions NetCord/Rest/RestInvite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public partial class RestInvite : IInvite, IJsonModel<JsonModels.JsonRestInvite>

public InviteFlags? Flags => _jsonModel.Flags;

public IReadOnlyList<Role>? Roles { get; }
public IReadOnlyList<PartialRole>? Roles { get; }

// Metadata
public int? Uses => _jsonModel.Uses;
Expand Down Expand Up @@ -61,7 +61,7 @@ public RestInvite(JsonModels.JsonRestInvite jsonModel, RestClient client)
var guildId = Guild.Id;

if (jsonModel.Roles is { } roles)
Roles = roles.Select(role => new Role(role, guildId, client)).ToArray();
Roles = roles.Select(role => new PartialRole(role, guildId, client)).ToArray();
}

if (jsonModel.Channel is { } channel)
Expand Down
110 changes: 28 additions & 82 deletions NetCord/Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,130 +6,76 @@ namespace NetCord;
/// <summary>
/// Represents a role in a guild.
/// </summary>
public partial class Role : ClientEntity, IJsonModel<JsonRole>
public partial class Role(JsonRole jsonModel, ulong guildId, RestClient client)
: PartialRole(jsonModel, guildId, client), IJsonModel<JsonRole>
{
JsonRole IJsonModel<JsonRole>.JsonModel => _jsonModel;
private readonly JsonRole _jsonModel;
JsonRole IJsonModel<JsonRole>.JsonModel => jsonModel;

/// <summary>
/// The <see cref="Role"/>'s ID.
/// The role's ID.
/// </summary>
public override ulong Id => _jsonModel.Id;
public override ulong Id => jsonModel.Id;

/// <summary>
/// The name of the <see cref="Role"/>.
/// Whether this role causes users with it to be displayed in a separate section in the guild users list.
/// </summary>
public string Name => _jsonModel.Name;
public bool Hoist => jsonModel.Hoist;

/// <summary>
/// The <see cref="Role"/>'s colors.
/// The permission bit set for this role.
/// </summary>
public RoleColors Colors { get; }
public Permissions Permissions => jsonModel.Permissions;

/// <summary>
/// Whether this <see cref="Role"/> causes users with it to be displayed in a separate section in the guild users list.
/// Whether this role is managed by an integration.
/// </summary>
public bool Hoist => _jsonModel.Hoist;
public bool Managed => jsonModel.Managed;

/// <summary>
/// The <see cref="Role"/>'s icon hash.
/// Whether this role is mentionable.
/// </summary>
public string? IconHash => _jsonModel.IconHash;
public bool Mentionable => jsonModel.Mentionable;

/// <summary>
/// The <see cref="Role"/>'s Unicode emoji.
/// The tags this role has.
/// </summary>
public string? UnicodeEmoji => _jsonModel.UnicodeEmoji;
public RoleTags? Tags { get; } = jsonModel.Tags is { } tags ? new(tags) : null;

/// <summary>
/// The raw position of this <see cref="Role"/>.
/// The role's flags combined as a bitfield.
/// </summary>
/// <remarks>
/// Use <see cref="Position"/> to get a properly comparable and sortable position value.
/// </remarks>
public int RawPosition => _jsonModel.Position;

/// <summary>
/// The position of this <see cref="Role"/> for sorting and comparing.
/// </summary>
public RolePosition Position => new(RawPosition, Id);

/// <summary>
/// The permission bit set for this <see cref="Role"/>.
/// </summary>
public Permissions Permissions => _jsonModel.Permissions;
public RoleFlags Flags => jsonModel.Flags;

/// <summary>
/// Whether this <see cref="Role"/> is managed by an integration.
/// </summary>
public bool Managed => _jsonModel.Managed;

/// <summary>
/// Whether this <see cref="Role"/> is mentionable.
/// </summary>
public bool Mentionable => _jsonModel.Mentionable;

/// <summary>
/// The tags this <see cref="Role"/> has.
/// </summary>
public RoleTags? Tags { get; }

/// <summary>
/// The <see cref="Role"/>'s flags combined as a bitfield.
/// </summary>
public RoleFlags Flags => _jsonModel.Flags;

/// <summary>
/// The ID of the guild this <see cref="Role"/> belongs to.
/// </summary>
public ulong GuildId { get; }

public Role(JsonRole jsonModel, ulong guildId, RestClient client) : base(client)
{
_jsonModel = jsonModel;

Colors = new(jsonModel.Colors);

if (jsonModel.Tags is { } tags)
Tags = new(tags);

GuildId = guildId;
}

/// <summary>
/// Gets the <see cref="ImageUrl"/> of the <see cref="Role"/>'s icon.
/// Gets the <see cref="ImageUrl"/> of the role's icon.
/// </summary>
/// <param name="format">The format of the returned <see cref="ImageUrl"/>.</param>
/// <returns>An <see cref="ImageUrl"/> pointing to the role's icon. If the role does not have one set, returns <see langword="null"/>.</returns>
public ImageUrl? GetIconUrl(ImageFormat format) => IconHash is string hash ? ImageUrl.RoleIcon(Id, hash, format) : null;

public override string ToString() => $"<@&{Id}>";

public override bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null) => Mention.TryFormatRole(destination, out charsWritten, Id);
}

/// <summary>
/// Represents the colors of a <see cref="Role"/>.
/// Represents the colors of a role.
/// </summary>
public class RoleColors(JsonRoleColors jsonModel) : IJsonModel<JsonRoleColors>
{
JsonRoleColors IJsonModel<JsonRoleColors>.JsonModel => jsonModel;

/// <summary>
/// The primary color for the <see cref="Role"/>.
/// The primary color for the role.
/// </summary>
public Color PrimaryColor => jsonModel.PrimaryColor;

/// <summary>
/// The secondary color for the <see cref="Role"/>. This will make the role a gradient between the other provided colors.
/// The secondary color for the role. This will make the role a gradient between the other provided colors.
/// </summary>
/// <remarks>
/// Requires the guild to have the <c>ENHANCED_ROLE_COLORS</c> guild feature.
/// </remarks>
public Color? SecondaryColor => jsonModel.SecondaryColor;

/// <summary>
/// The tertiary color for the <see cref="Role"/>. This will turn the gradient into a holographic style.
/// The tertiary color for the role. This will turn the gradient into a holographic style.
/// </summary>
/// <remarks>
/// Requires the guild to have the <c>ENHANCED_ROLE_COLORS</c> guild feature.
Expand All @@ -138,19 +84,19 @@ public class RoleColors(JsonRoleColors jsonModel) : IJsonModel<JsonRoleColors>
}

/// <summary>
/// Represents the tags associated with a <see cref="Role"/>.
/// Represents the tags associated with a role.
/// </summary>
public class RoleTags(JsonRoleTags jsonModel) : IJsonModel<JsonRoleTags>
{
JsonRoleTags IJsonModel<JsonRoleTags>.JsonModel => jsonModel;

/// <summary>
/// The ID of the bot this <see cref="Role"/> belongs to.
/// The ID of the bot this role belongs to.
/// </summary>
public ulong? BotId => jsonModel.BotId;

/// <summary>
/// The ID of the integration this <see cref="Role"/> belongs to.
/// The ID of the integration this role belongs to.
/// </summary>
public ulong? IntegrationId => jsonModel.IntegrationId;

Expand All @@ -160,17 +106,17 @@ public class RoleTags(JsonRoleTags jsonModel) : IJsonModel<JsonRoleTags>
public bool IsPremiumSubscriber => jsonModel.IsPremiumSubscriber;

/// <summary>
/// The ID of this <see cref="Role"/>'s subscription SKU and listing.
/// The ID of this role's subscription SKU and listing.
/// </summary>
public ulong? SubscriptionListingId => jsonModel.SubscriptionListingId;

/// <summary>
/// Whether this <see cref="Role"/> is available for purchase.
/// Whether this role is available for purchase.
/// </summary>
public bool IsAvailableForPurchase => jsonModel.IsAvailableForPurchase;

/// <summary>
/// Whether this <see cref="Role"/> is a guild's linked role.
/// Whether this role is a guild's linked role.
/// </summary>
public bool GuildConnections => jsonModel.GuildConnections;
}