diff --git a/NetCord/JsonModels/JsonPartialRole.cs b/NetCord/JsonModels/JsonPartialRole.cs new file mode 100644 index 00000000..777f4073 --- /dev/null +++ b/NetCord/JsonModels/JsonPartialRole.cs @@ -0,0 +1,25 @@ +using System.ComponentModel; +using System.Text.Json.Serialization; + +namespace NetCord.JsonModels; + +/// +/// This class is used when a role is returned in a context where not all properties are available, i.e. in . +/// +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; } +} diff --git a/NetCord/JsonModels/JsonRole.cs b/NetCord/JsonModels/JsonRole.cs index 87cf34ed..de7b559b 100644 --- a/NetCord/JsonModels/JsonRole.cs +++ b/NetCord/JsonModels/JsonRole.cs @@ -1,27 +1,13 @@ +using System.ComponentModel; 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; } diff --git a/NetCord/PartialRole.cs b/NetCord/PartialRole.cs new file mode 100644 index 00000000..2c8228a1 --- /dev/null +++ b/NetCord/PartialRole.cs @@ -0,0 +1,72 @@ +using System.ComponentModel; + +using NetCord.JsonModels; +using NetCord.Rest; + +namespace NetCord; + +/// +/// Represents a partial role. +/// +/// +/// Useful for . +/// +public partial class PartialRole : ClientEntity, IJsonModel +{ + JsonPartialRole IJsonModel.JsonModel => _jsonModel; + private readonly JsonPartialRole _jsonModel; + + /// + /// The role's ID. + /// + public override ulong Id => _jsonModel.Id; + + /// + /// The name of the role. + /// + public string Name => _jsonModel.Name; + + /// + /// The role's colors. + /// + public RoleColors Colors { get; } + + /// + /// The raw position of this role. + /// + /// + /// Use to get a properly comparable and sortable position value. + /// + public int RawPosition => _jsonModel.Position; + + /// + /// The position of this role for sorting and comparing. + /// + public RolePosition Position => new(RawPosition, Id); + + /// + /// The role's icon hash. + /// + public string? IconHash => _jsonModel.IconHash; + + /// + /// The role's Unicode emoji. + /// + public string? UnicodeEmoji => _jsonModel.UnicodeEmoji; + + /// + /// The ID of the guild this role belongs to. + /// + public ulong GuildId { get; } + + public override string ToString() => $"<@&{Id}>"; + + public PartialRole(JsonPartialRole jsonModel, ulong guildId, RestClient client) : base(client) + { + _jsonModel = jsonModel; + + Colors = new(jsonModel.Colors); + + GuildId = guildId; + } +} diff --git a/NetCord/Rest/JsonModels/JsonRestInvite.cs b/NetCord/Rest/JsonModels/JsonRestInvite.cs index fd007431..8684171a 100644 --- a/NetCord/Rest/JsonModels/JsonRestInvite.cs +++ b/NetCord/Rest/JsonModels/JsonRestInvite.cs @@ -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; } diff --git a/NetCord/Rest/RestClient.Guild.cs b/NetCord/Rest/RestClient.Guild.cs index eb62e537..92854281 100644 --- a/NetCord/Rest/RestClient.Guild.cs +++ b/NetCord/Rest/RestClient.Guild.cs @@ -197,7 +197,7 @@ public async Task> 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 ModifyGuildRoleAsync(ulong guildId, ulong roleId, Action action, RestRequestProperties? properties = null, CancellationToken cancellationToken = default) { RoleOptions obj = new(); @@ -207,7 +207,7 @@ public async Task 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); diff --git a/NetCord/Rest/RestInvite.cs b/NetCord/Rest/RestInvite.cs index 18be1a36..90cb6061 100644 --- a/NetCord/Rest/RestInvite.cs +++ b/NetCord/Rest/RestInvite.cs @@ -33,7 +33,7 @@ public partial class RestInvite : IInvite, IJsonModel public InviteFlags? Flags => _jsonModel.Flags; - public IReadOnlyList? Roles { get; } + public IReadOnlyList? Roles { get; } // Metadata public int? Uses => _jsonModel.Uses; @@ -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) diff --git a/NetCord/Role.cs b/NetCord/Role.cs index 0107ac91..5ce7824e 100644 --- a/NetCord/Role.cs +++ b/NetCord/Role.cs @@ -4,100 +4,58 @@ namespace NetCord; /// -/// Represents a role in a guild. +/// Represents a full role in a guild. /// -public partial class Role : ClientEntity, IJsonModel +public partial class Role : PartialRole, IJsonModel { JsonRole IJsonModel.JsonModel => _jsonModel; private readonly JsonRole _jsonModel; /// - /// The 's ID. + /// The role's ID. /// public override ulong Id => _jsonModel.Id; /// - /// The name of the . - /// - public string Name => _jsonModel.Name; - - /// - /// The 's colors. - /// - public RoleColors Colors { get; } - - /// - /// Whether this causes users with it to be displayed in a separate section in the guild users list. + /// Whether this role causes users with it to be displayed in a separate section in the guild users list. /// public bool Hoist => _jsonModel.Hoist; /// - /// The 's icon hash. - /// - public string? IconHash => _jsonModel.IconHash; - - /// - /// The 's Unicode emoji. - /// - public string? UnicodeEmoji => _jsonModel.UnicodeEmoji; - - /// - /// The raw position of this . - /// - /// - /// Use to get a properly comparable and sortable position value. - /// - public int RawPosition => _jsonModel.Position; - - /// - /// The position of this for sorting and comparing. - /// - public RolePosition Position => new(RawPosition, Id); - - /// - /// The permission bit set for this . + /// The permission bit set for this role. /// public Permissions Permissions => _jsonModel.Permissions; /// - /// Whether this is managed by an integration. + /// Whether this role is managed by an integration. /// public bool Managed => _jsonModel.Managed; /// - /// Whether this is mentionable. + /// Whether this role is mentionable. /// public bool Mentionable => _jsonModel.Mentionable; /// - /// The tags this has. + /// The tags this role has. /// public RoleTags? Tags { get; } /// - /// The 's flags combined as a bitfield. + /// The role's flags combined as a bitfield. /// public RoleFlags Flags => _jsonModel.Flags; - /// - /// The ID of the guild this belongs to. - /// - public ulong GuildId { get; } - - public Role(JsonRole jsonModel, ulong guildId, RestClient client) : base(client) + public Role(JsonRole jsonModel, ulong guildId, RestClient client) : base(jsonModel, guildId, client) { _jsonModel = jsonModel; - Colors = new(jsonModel.Colors); - if (jsonModel.Tags is { } tags) Tags = new(tags); - - GuildId = guildId; } /// - /// Gets the of the 's icon. + /// Gets the of the role's icon. /// /// The format of the returned . /// An pointing to the role's icon. If the role does not have one set, returns . @@ -109,19 +67,19 @@ public Role(JsonRole jsonModel, ulong guildId, RestClient client) : base(client) } /// -/// Represents the colors of a . +/// Represents the colors of a role. /// public class RoleColors(JsonRoleColors jsonModel) : IJsonModel { JsonRoleColors IJsonModel.JsonModel => jsonModel; /// - /// The primary color for the . + /// The primary color for the role. /// public Color PrimaryColor => jsonModel.PrimaryColor; /// - /// The secondary color for the . 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. /// /// /// Requires the guild to have the ENHANCED_ROLE_COLORS guild feature. @@ -129,7 +87,7 @@ public class RoleColors(JsonRoleColors jsonModel) : IJsonModel public Color? SecondaryColor => jsonModel.SecondaryColor; /// - /// The tertiary color for the . This will turn the gradient into a holographic style. + /// The tertiary color for the role. This will turn the gradient into a holographic style. /// /// /// Requires the guild to have the ENHANCED_ROLE_COLORS guild feature. @@ -138,19 +96,19 @@ public class RoleColors(JsonRoleColors jsonModel) : IJsonModel } /// -/// Represents the tags associated with a . +/// Represents the tags associated with a role. /// public class RoleTags(JsonRoleTags jsonModel) : IJsonModel { JsonRoleTags IJsonModel.JsonModel => jsonModel; /// - /// The ID of the bot this belongs to. + /// The ID of the bot this role belongs to. /// public ulong? BotId => jsonModel.BotId; /// - /// The ID of the integration this belongs to. + /// The ID of the integration this role belongs to. /// public ulong? IntegrationId => jsonModel.IntegrationId; @@ -160,17 +118,17 @@ public class RoleTags(JsonRoleTags jsonModel) : IJsonModel public bool IsPremiumSubscriber => jsonModel.IsPremiumSubscriber; /// - /// The ID of this 's subscription SKU and listing. + /// The ID of this role's subscription SKU and listing. /// public ulong? SubscriptionListingId => jsonModel.SubscriptionListingId; /// - /// Whether this is available for purchase. + /// Whether this role is available for purchase. /// public bool IsAvailableForPurchase => jsonModel.IsAvailableForPurchase; /// - /// Whether this is a guild's linked role. + /// Whether this role is a guild's linked role. /// public bool GuildConnections => jsonModel.GuildConnections; }