Skip to content

Commit 565c764

Browse files
committed
fix: fix the implementation for DiscordRoleTags
feat: DiscordRole.RoleType
1 parent dfe2422 commit 565c764

File tree

3 files changed

+118
-35
lines changed

3 files changed

+118
-35
lines changed

DisCatSharp/Entities/Guild/DiscordRole.cs

+16-11
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace DisCatSharp.Entities;
1717
/// <summary>
1818
/// Represents a discord role, to which users can be assigned.
1919
/// </summary>
20-
public class DiscordRole : SnowflakeObject, IEquatable<DiscordRole>
20+
public sealed class DiscordRole : SnowflakeObject, IEquatable<DiscordRole>
2121
{
2222
[JsonProperty("color", NullValueHandling = NullValueHandling.Ignore)]
2323
internal int ColorInternal;
@@ -29,7 +29,7 @@ public class DiscordRole : SnowflakeObject, IEquatable<DiscordRole>
2929
/// Gets the role unicode_emoji.
3030
/// </summary>
3131
[JsonProperty("unicode_emoji", NullValueHandling = NullValueHandling.Ignore)]
32-
internal string UnicodeEmojiString;
32+
internal string? UnicodeEmojiString;
3333

3434
/// <summary>
3535
/// Initializes a new instance of the <see cref="DiscordRole" /> class.
@@ -53,7 +53,7 @@ internal DiscordRole()
5353
/// Gets the description of this role.
5454
/// </summary>
5555
[JsonProperty("description", NullValueHandling = NullValueHandling.Ignore)]
56-
public string Description { get; internal set; }
56+
public string? Description { get; internal set; }
5757

5858
/// <summary>
5959
/// Gets the color of this role.
@@ -96,26 +96,31 @@ public DiscordColor Color
9696
/// Gets the tags this role has.
9797
/// </summary>
9898
[JsonProperty("tags", NullValueHandling = NullValueHandling.Ignore)]
99-
public DiscordRoleTags Tags { get; internal set; }
99+
public DiscordRoleTags? Tags { get; internal set; }
100+
101+
/// <summary>
102+
/// Determines the type of role based on its tags.
103+
/// </summary>
104+
public RoleType Type => this.Tags?.DetermineRoleType() ?? RoleType.Normal;
100105

101106
/// <summary>
102107
/// Gets the role icon's hash.
103108
/// </summary>
104109
[JsonProperty("icon", NullValueHandling = NullValueHandling.Ignore)]
105-
public string IconHash { get; internal set; }
110+
public string? IconHash { get; internal set; }
106111

107112
/// <summary>
108113
/// Gets the role icon's url.
109114
/// </summary>
110115
[JsonIgnore]
111-
public string IconUrl
116+
public string? IconUrl
112117
=> !string.IsNullOrWhiteSpace(this.IconHash) ? $"{DiscordDomain.GetDomain(CoreDomain.DiscordCdn).Url}{Endpoints.ROLE_ICONS}/{this.Id.ToString(CultureInfo.InvariantCulture)}/{this.IconHash}.png?size=64" : null;
113118

114119
/// <summary>
115120
/// Gets the unicode emoji.
116121
/// </summary>
117122
[JsonIgnore]
118-
public DiscordEmoji UnicodeEmoji
123+
public DiscordEmoji? UnicodeEmoji
119124
=> this.UnicodeEmojiString != null ? DiscordEmoji.FromName(this.Discord, $":{this.UnicodeEmojiString}:", false) : null;
120125

121126
/// <summary>
@@ -132,7 +137,7 @@ public DiscordGuild Guild
132137
}
133138

134139
[JsonIgnore]
135-
internal DiscordGuild GuildInternal { get; set; }
140+
internal DiscordGuild? GuildInternal { get; set; }
136141

137142
/// <summary>
138143
/// Gets the role flags.
@@ -231,7 +236,7 @@ public override int GetHashCode()
231236
/// <exception cref="NotFoundException">Thrown when the role does not exist.</exception>
232237
/// <exception cref="BadRequestException">Thrown when an invalid parameter was provided.</exception>
233238
/// <exception cref="ServerErrorException">Thrown when Discord is unable to process the request.</exception>
234-
public Task ModifyPositionAsync(int position, string reason = null)
239+
public Task ModifyPositionAsync(int position, string? reason = null)
235240
{
236241
var roles = this.Guild.Roles.Values.OrderByDescending(xr => xr.Position)
237242
.Select(x => new RestGuildRoleReorderPayload
@@ -263,7 +268,7 @@ public Task ModifyPositionAsync(int position, string reason = null)
263268
/// <exception cref="NotFoundException">Thrown when the role does not exist.</exception>
264269
/// <exception cref="BadRequestException">Thrown when an invalid parameter was provided.</exception>
265270
/// <exception cref="ServerErrorException">Thrown when Discord is unable to process the request.</exception>
266-
public Task ModifyAsync(string name = null, Permissions? permissions = null, DiscordColor? color = null, bool? hoist = null, bool? mentionable = null, string reason = null)
271+
public Task ModifyAsync(string name = null, Permissions? permissions = null, DiscordColor? color = null, bool? hoist = null, bool? mentionable = null, string? reason = null)
267272
=> this.Discord.ApiClient.ModifyGuildRoleAsync(this.GuildId, this.Id, name, permissions, color?.Value, hoist, mentionable, Optional.None, Optional.None, reason);
268273

269274
/// <summary>
@@ -326,7 +331,7 @@ public Task ModifyAsync(Action<RoleEditModel> action)
326331
/// <exception cref="NotFoundException">Thrown when the role does not exist.</exception>
327332
/// <exception cref="BadRequestException">Thrown when an invalid parameter was provided.</exception>
328333
/// <exception cref="ServerErrorException">Thrown when Discord is unable to process the request.</exception>
329-
public Task DeleteAsync(string reason = null)
334+
public Task DeleteAsync(string? reason = null)
330335
=> this.Discord.ApiClient.DeleteRoleAsync(this.GuildId, this.Id, reason);
331336

332337
#endregion
+45-24
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,74 @@
1-
using Newtonsoft.Json;
1+
using DisCatSharp.Entities;
2+
using DisCatSharp.Enums;
23

3-
namespace DisCatSharp.Entities;
4+
using Newtonsoft.Json;
45

56
/// <summary>
6-
/// Represents a discord role tags.
7+
/// Represents a Discord role tags object with detailed classification logic.
78
/// </summary>
8-
public class DiscordRoleTags : ObservableApiObject
9+
public sealed class DiscordRoleTags : ObservableApiObject
910
{
11+
/// <summary>
12+
/// Whether this role is a guild's linked role.
13+
/// </summary>
1014
[JsonProperty("guild_connections", NullValueHandling = NullValueHandling.Include)]
11-
internal bool? GuildConnection = false;
15+
public bool? GuildConnections { get; internal set; }
1216

17+
/// <summary>
18+
/// Whether this is the guild's booster role.
19+
/// </summary>
1320
[JsonProperty("premium_subscriber", NullValueHandling = NullValueHandling.Include)]
14-
internal bool? PremiumSubscriber = false;
21+
public bool? PremiumSubscriber { get; internal set; }
1522

1623
/// <summary>
17-
/// Gets the id of the bot this role belongs to.
24+
/// Gets the ID of the bot this role belongs to.
1825
/// </summary>
19-
[JsonProperty("bot_id", NullValueHandling = NullValueHandling.Ignore)]
26+
[JsonProperty("bot_id", NullValueHandling = NullValueHandling.Include)]
2027
public ulong? BotId { get; internal set; }
2128

2229
/// <summary>
23-
/// Gets the id of the integration this role belongs to.
30+
/// Gets the ID of the integration this role belongs to.
2431
/// </summary>
25-
[JsonProperty("integration_id", NullValueHandling = NullValueHandling.Ignore)]
32+
[JsonProperty("integration_id", NullValueHandling = NullValueHandling.Include)]
2633
public ulong? IntegrationId { get; internal set; }
2734

2835
/// <summary>
29-
/// Gets whether this is the guild's booster role.
36+
/// Gets the ID of this role's subscription SKU and listing.
3037
/// </summary>
31-
[JsonIgnore]
32-
public bool IsPremiumSubscriber
33-
=> this.PremiumSubscriber.HasValue && this.PremiumSubscriber.Value;
34-
35-
/// <summary>
36-
/// The id of this role's subscription sku and listing.
37-
/// </summary>
38-
[JsonProperty("subscription_listing_id", NullValueHandling = NullValueHandling.Ignore)]
38+
[JsonProperty("subscription_listing_id", NullValueHandling = NullValueHandling.Include)]
3939
public ulong? SubscriptionListingId { get; internal set; }
4040

4141
/// <summary>
4242
/// Whether this role is available for purchase.
4343
/// </summary>
44-
[JsonProperty("available_for_purchase", NullValueHandling = NullValueHandling.Ignore)]
44+
[JsonProperty("available_for_purchase", NullValueHandling = NullValueHandling.Include)]
4545
public bool? AvailableForPurchase { get; internal set; }
4646

4747
/// <summary>
48-
/// Gets whether this is the guild's premium subscriber role.
48+
/// Determines the type of role based on its tags.
4949
/// </summary>
50-
[JsonIgnore]
51-
public bool IsLinkedRole
52-
=> this.GuildConnection.HasValue && this.GuildConnection.Value;
50+
public RoleType DetermineRoleType()
51+
{
52+
if (this.BotId is not null)
53+
return RoleType.Bot;
54+
55+
switch (this.GuildConnections)
56+
{
57+
case false when this.PremiumSubscriber is null:
58+
return RoleType.Booster;
59+
case false when this.PremiumSubscriber is false:
60+
return RoleType.ServerProduct;
61+
case false when this.IntegrationId is not null && this.PremiumSubscriber is false:
62+
return RoleType.PremiumSubscriber;
63+
case false when this.IntegrationId is not null && this.SubscriptionListingId is not null:
64+
return RoleType.PremiumSubscriberTier;
65+
}
66+
67+
if (this.IntegrationId is not null && this.BotId is null && this.PremiumSubscriber is null && this.SubscriptionListingId is null)
68+
return RoleType.ExternalPlatform;
69+
70+
return this.GuildConnections is null
71+
? RoleType.RoleConnection
72+
: RoleType.Normal;
73+
}
5374
}

DisCatSharp/Enums/Guild/RoleType.cs

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.ComponentModel;
2+
3+
namespace DisCatSharp.Enums;
4+
5+
/// <summary>
6+
/// Represents the classification of a Discord role.
7+
/// </summary>
8+
public enum RoleType
9+
{
10+
/// <summary>
11+
/// The role type is unknown.
12+
/// </summary>
13+
[Description("Normal role type.")]
14+
Normal,
15+
16+
/// <summary>
17+
/// A bot or application role.
18+
/// </summary>
19+
[Description("Bot or application role.")]
20+
Bot,
21+
22+
/// <summary>
23+
/// A booster role.
24+
/// </summary>
25+
[Description("Server booster role.")]
26+
Booster,
27+
28+
/// <summary>
29+
/// A server product role.
30+
/// </summary>
31+
[Description("Server product role.")]
32+
ServerProduct,
33+
34+
/// <summary>
35+
/// A premium subscriber role.
36+
/// </summary>
37+
[Description("Premium subscriber role.")]
38+
PremiumSubscriber,
39+
40+
/// <summary>
41+
/// A premium subscriber tier role.
42+
/// </summary>
43+
[Description("Premium subscriber tier role.")]
44+
PremiumSubscriberTier,
45+
46+
/// <summary>
47+
/// A role linked to an external platform (e.g., Twitch or YouTube).
48+
/// </summary>
49+
[Description("External platform role. For example Twitch or YouTube.")]
50+
ExternalPlatform,
51+
52+
/// <summary>
53+
/// A role connection role.
54+
/// </summary>
55+
[Description("Role connection role.")]
56+
RoleConnection
57+
}

0 commit comments

Comments
 (0)