Skip to content

Commit

Permalink
Added support for global names.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lachee committed Jul 26, 2023
1 parent 2ab7efc commit eea59a5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
4 changes: 2 additions & 2 deletions DiscordRPC.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,9 @@ private static void OnJoinRequested(object sender, JoinRequestMessage args)
*/

//We have received a request, dump a bunch of information for the user
Console.WriteLine("'{0}' has requested to join our game.", args.User.Username);
Console.WriteLine("'{0}' has requested to join our game.", args.User.ToString());
Console.WriteLine(" - User's Avatar: {0}", args.User.GetAvatarURL(User.AvatarFormat.GIF, User.AvatarSize.x2048));
Console.WriteLine(" - User's Descrim: {0}", args.User.Discriminator);
Console.WriteLine(" - User's Username: {0}", args.User.Username);
Console.WriteLine(" - User's Snowflake: {0}", args.User.ID);
Console.WriteLine();

Expand Down
2 changes: 2 additions & 0 deletions DiscordRPC/RichPresence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ public class Assets
/// Name of the uploaded image for the large profile artwork.
/// <para>Max 256 Bytes.</para>
/// </summary>
/// <remarks>Allows URL to directly link to images.</remarks>
[JsonProperty("large_image", NullValueHandling = NullValueHandling.Ignore)]
public string LargeImageKey
{
Expand Down Expand Up @@ -442,6 +443,7 @@ public string LargeImageText
/// Name of the uploaded image for the small profile artwork.
/// <para>Max 256 Bytes.</para>
/// </summary>
/// <remarks>Allows URL to directly link to images.</remarks>
[JsonProperty("small_image", NullValueHandling = NullValueHandling.Ignore)]
public string SmallImageKey
{
Expand Down
39 changes: 31 additions & 8 deletions DiscordRPC/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,17 @@ public enum AvatarSize
/// <summary>
/// The discriminator of the user.
/// </summary>
[JsonProperty("discriminator")]
/// <remarks>If the user has migrated to unique a <see cref="Username"/>, the discriminator will always be 0.</remarks>
[JsonProperty("discriminator"), Obsolete("Discord no longer uses discriminators.")]
public int Discriminator { get; private set; }


/// <summary>
/// The display name of the user
/// </summary>
/// <remarks>This will be empty if the user has not set a global display name.</remarks>
[JsonProperty("global_name")]
public string DisplayName { get; private set; }

/// <summary>
/// The avatar hash of the user. Too get a URL for the avatar, use the <see cref="GetAvatarURL(AvatarFormat, AvatarSize)"/>. This can be null if the user has no avatar. The <see cref="GetAvatarURL(AvatarFormat, AvatarSize)"/> will account for this and return the discord default.
/// </summary>
Expand Down Expand Up @@ -208,9 +216,15 @@ public string GetAvatarURL(AvatarFormat format, AvatarSize size)
if (format != AvatarFormat.PNG)
throw new BadImageFormatException("The user has no avatar and the requested format " + format.ToString() + " is not supported. (Only supports PNG).");

//Get the endpoint
int descrim = Discriminator % 5;
endpoint = $"/embed/avatars/{descrim}";
// Get the default avatar for the user.
int index = (int)((ID >> 22) % 6);

#pragma warning disable CS0618 // Disable the obsolete warning as we know the discriminator is obsolete and we are validating it here.
if (Discriminator > 0)
index = Discriminator % 5;
#pragma warning restore CS0618

endpoint = $"/embed/avatars/{index}";
}

//Finish of the endpoint
Expand All @@ -228,12 +242,21 @@ public string GetAvatarExtension(AvatarFormat format)
}

/// <summary>
/// Formats the user into username#discriminator
/// Formats the user into a displayable format. If the user has a <see cref="DisplayName"/>, then this will be used.
/// <para>If the user still has a discriminator, then this will return the form of `Username#Discriminator`.</para>
/// </summary>
/// <returns></returns>
/// <returns>String of the user that can be used for display.</returns>
public override string ToString()
{
return Username + "#" + Discriminator.ToString("D4");
if (!string.IsNullOrEmpty(DisplayName))
return DisplayName;

#pragma warning disable CS0618 // Disable the obsolete warning as we know the discriminator is obsolete and we are validating it here.
if (Discriminator != 0)
return Username + "#" + Discriminator.ToString("D4");
#pragma warning restore CS0618

return Username;
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "1.1.4"
"version": "1.2.0"
}

0 comments on commit eea59a5

Please sign in to comment.