Skip to content
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
6 changes: 6 additions & 0 deletions src/Disqord.Core/Discord/Cdn/Discord.Cdn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public static string GetEventCoverImageUrl(Snowflake eventId, string coverImageH
return FormatUrl(path, format, size);
}

public static string GetClanBadgeUrl(Snowflake guildId, string badgeHash, CdnAssetFormat format = default, int? size = null)
{
var path = $"clan-badges/{guildId}/{badgeHash}";
return FormatUrl(path, format, size);
}

private static string FormatUrl(string path, CdnAssetFormat format, int? size)
{
Guard.IsDefined(format);
Expand Down
11 changes: 11 additions & 0 deletions src/Disqord.Core/Discord/Cdn/Extensions/CdnEntityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ public static string GetGuildAvatarUrl(this IMember member, CdnAssetFormat forma
: null;
}

public static string? GetBadgeUrl(this IUserPrimaryGuild primaryGuild, CdnAssetFormat format = default, int? size = null)
{
Guard.IsNotNull(primaryGuild);

var guildId = primaryGuild.GuildId;
var badgeHash = primaryGuild.BadgeHash;
return guildId != null && badgeHash != null
? Discord.Cdn.GetClanBadgeUrl(guildId.Value, badgeHash, format, size)
: null;
}

public static string GetUrl(this IPartialSticker sticker)
{
Guard.IsNotNull(sticker);
Expand Down
8 changes: 8 additions & 0 deletions src/Disqord.Core/Entities/Core/Users/IUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ public interface IUser : ISnowflakeEntity, INamableEntity, IMentionableEntity, I
/// Gets the public <see cref="UserFlags"/> of this user.
/// </summary>
UserFlags PublicFlags { get; }

/// <summary>
/// Gets the primary guild (server tag) of this user.
/// </summary>
/// <returns>
/// The primary guild or <see langword="null"/> if the user has no primary guild set.
/// </returns>
IUserPrimaryGuild? PrimaryGuild { get; }
}
41 changes: 41 additions & 0 deletions src/Disqord.Core/Entities/Core/Users/IUserPrimaryGuild.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace Disqord;

/// <summary>
/// Represents the primary guild (server tag) of a user.
/// </summary>
public interface IUserPrimaryGuild
{
/// <summary>
/// Gets the ID of the user's primary guild.
/// </summary>
/// <returns>
/// The ID of the guild or <see langword="null"/> if the identity guild is not set.
/// </returns>
Snowflake? GuildId { get; }

/// <summary>
/// Gets whether the user is displaying the primary guild's server tag.
/// </summary>
/// <remarks>
/// This can be <see langword="null"/> if the system clears the identity,
/// e.g. the server no longer supports tags.
/// This will be <see langword="false"/> if the user manually removes their tag.
/// </remarks>
bool? IsIdentityEnabled { get; }

/// <summary>
/// Gets the text of the user's server tag.
/// </summary>
/// <returns>
/// The server tag text (up to 4 characters) or <see langword="null"/> if not set.
/// </returns>
string? Tag { get; }

/// <summary>
/// Gets the server tag badge image hash.
/// </summary>
/// <returns>
/// The image hash of the badge or <see langword="null"/> if not set.
/// </returns>
string? BadgeHash { get; }
}
13 changes: 13 additions & 0 deletions src/Disqord.Core/Entities/Transient/Users/TransientUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ public class TransientUser : TransientClientEntity<UserJsonModel>, IUser
/// <inheritdoc/>
public virtual UserFlags PublicFlags => Model.PublicFlags.Value;

/// <inheritdoc/>
public virtual IUserPrimaryGuild? PrimaryGuild
{
get
{
if (!Model.PrimaryGuild.HasValue || Model.PrimaryGuild.Value == null)
return null;

return _primaryGuild ??= new TransientUserPrimaryGuild(Model.PrimaryGuild.Value);
}
}
private IUserPrimaryGuild? _primaryGuild;

/// <inheritdoc/>
public virtual string Mention => Disqord.Mention.User(this);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Disqord.Models;

namespace Disqord;

public class TransientUserPrimaryGuild : TransientEntity<UserPrimaryGuildJsonModel>, IUserPrimaryGuild
{
/// <inheritdoc/>
public Snowflake? GuildId => Model.IdentityGuildId;

/// <inheritdoc/>
public bool? IsIdentityEnabled => Model.IdentityEnabled;

/// <inheritdoc/>
public string? Tag => Model.Tag;

/// <inheritdoc/>
public string? BadgeHash => Model.Badge;

public TransientUserPrimaryGuild(UserPrimaryGuildJsonModel model)
: base(model)
{ }
}
3 changes: 3 additions & 0 deletions src/Disqord.Core/Models/UserJsonModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ public class UserJsonModel : JsonModel

[JsonProperty("public_flags")]
public Optional<UserFlags> PublicFlags;

[JsonProperty("primary_guild")]
public Optional<UserPrimaryGuildJsonModel?> PrimaryGuild;
}
19 changes: 19 additions & 0 deletions src/Disqord.Core/Models/UserPrimaryGuildJsonModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Disqord.Serialization.Json;
using Qommon;

namespace Disqord.Models;

public class UserPrimaryGuildJsonModel : JsonModel
{
[JsonProperty("identity_guild_id")]
public Snowflake? IdentityGuildId;

[JsonProperty("identity_enabled")]
public bool? IdentityEnabled;

[JsonProperty("tag")]
public string? Tag;

[JsonProperty("badge")]
public string? Badge;
}
7 changes: 7 additions & 0 deletions src/Disqord.Gateway/Entities/Cached/User/CachedSharedUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class CachedSharedUser : CachedUser, ICachedSharedUser
/// <inheritdoc/>
public override UserFlags PublicFlags => _publicFlags;

/// <inheritdoc/>
public override IUserPrimaryGuild? PrimaryGuild => _primaryGuild;

/// <inheritdoc/>
public int ReferenceCount => _referenceCount;

Expand All @@ -37,6 +40,7 @@ public class CachedSharedUser : CachedUser, ICachedSharedUser
private string? _avatarHash;
private readonly bool _isBot;
private UserFlags _publicFlags;
private IUserPrimaryGuild? _primaryGuild;
private int _referenceCount;

/// <summary>
Expand All @@ -63,6 +67,9 @@ public override void Update(UserJsonModel model)

if (model.PublicFlags.HasValue)
_publicFlags = model.PublicFlags.Value;

if (model.PrimaryGuild.HasValue)
_primaryGuild = model.PrimaryGuild.Value != null ? new TransientUserPrimaryGuild(model.PrimaryGuild.Value) : null;
}

/// <inheritdoc/>
Expand Down
3 changes: 3 additions & 0 deletions src/Disqord.Gateway/Entities/Cached/User/CachedShareeUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public abstract class CachedShareeUser : CachedUser
/// <inheritdoc/>
public override UserFlags PublicFlags => SharedUser.PublicFlags;

/// <inheritdoc/>
public override IUserPrimaryGuild? PrimaryGuild => SharedUser.PrimaryGuild;

[EditorBrowsable(EditorBrowsableState.Never)]
public CachedSharedUser SharedUser { get; private set; }

Expand Down
3 changes: 3 additions & 0 deletions src/Disqord.Gateway/Entities/Cached/User/CachedUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public abstract class CachedUser : CachedSnowflakeEntity, IUser
/// <inheritdoc/>
public abstract UserFlags PublicFlags { get; }

/// <inheritdoc/>
public abstract IUserPrimaryGuild? PrimaryGuild { get; }

/// <inheritdoc/>
public string Mention => Disqord.Mention.User(this);

Expand Down
Loading