|
| 1 | +--- |
| 2 | +uid: avatars |
| 3 | +--- |
| 4 | + |
| 5 | +# Avatars |
| 6 | +When the RPC Client connects to Discord it will be given a User Object representing the active user on that client. It is possible to grab the avatar of the user and Display it within your application. |
| 7 | + |
| 8 | +This is useful for scoreboards, invite links, and other personalisation features. |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +## OnReady Event |
| 13 | +The user's information is not immediately available. You must wait for the `OnReady` event emitted by the client. |
| 14 | +This event will contain the current user information, and the [DiscordRpcClient.CurrentUser](xref:DiscordRPC.DiscordRpcClient.CurrentUser) is not valid until it has invoked. |
| 15 | + |
| 16 | +> [!WARNING] |
| 17 | +> As described in the [Getting Started](../getting_started/standard.md#events), events are executed in another thread. |
| 18 | +> |
| 19 | +> If you are using a Game Engine, you need to manually use [DiscordRpcClient.Invoke](xref:DiscordRPC.DiscordRpcClient.Invoke) on your main thread or otherwise handle the cross-thread data transfer. |
| 20 | +
|
| 21 | +## Getting the Avatar |
| 22 | +The [User.Avatar](xref:DiscordRPC.User.Avatar) is the current avatar hash, and isn't the picture. Use the helper functions to get an avatar with the correct format and sizing: |
| 23 | +```cs |
| 24 | +client.OnReady += (sender, msg) => |
| 25 | +{ |
| 26 | + //Create some events so we know things are happening |
| 27 | + Console.WriteLine("Connected to discord with user {0}", msg.User.Username); |
| 28 | + Console.WriteLine("Avatar URL: {0}", msg.User.GetAvatarURL(User.AvatarFormat.PNG, User.AvatarSize.x128)); |
| 29 | +}; |
| 30 | +``` |
| 31 | + |
| 32 | +Once you have the Avatar URL, you can download it using your favourite HTTP Client: |
| 33 | +```cs |
| 34 | +string url = client.CurrentUser.GetAvatarURL(User.AvatarFormat.PNG, User.AvatarSize.x128); |
| 35 | +using (var client = new System.Net.WebClient()) |
| 36 | +{ |
| 37 | + client.DownloadFile(url, "avatar.png"); |
| 38 | +} |
| 39 | + |
| 40 | +// Or using HttpClient (recommended for modern applications) |
| 41 | +using (var client = new HttpClient()) |
| 42 | +{ |
| 43 | + byte[] imageBytes = await client.GetByteArrayAsync(url); |
| 44 | + await File.WriteAllBytesAsync("avatar.png", imageBytes); |
| 45 | +} |
| 46 | + |
| 47 | +// Or if you are using Unity3D |
| 48 | +IEnumerator LoadAvatar() |
| 49 | +{ |
| 50 | + string url = client.CurrentUser.GetAvatarURL(User.AvatarFormat.PNG, User.AvatarSize.x128); |
| 51 | + UnityWebRequest request = UnityWebRequestTexture.GetTexture(url); |
| 52 | + yield return request.SendWebRequest(); |
| 53 | + |
| 54 | + if (request.result == UnityWebRequest.Result.Success) |
| 55 | + { |
| 56 | + Texture2D texture = DownloadHandlerTexture.GetContent(request); |
| 57 | + // Use the texture (e.g., assign to a material or UI image) |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +> [!TIP] |
| 63 | +> You can use Async/Await in Unity3D! |
| 64 | +> |
| 65 | +> You will have much higher code readability and less frustrations when using asnchronous functions rather than coroutine hack Unity came up with. |
| 66 | +> Use a library like [Cysharp/UniTask](https://github.com/Cysharp/UniTask) for extended functionality, support, and performance. |
| 67 | +
|
| 68 | + |
| 69 | +### Animations |
| 70 | +If the avatar has a animation ( its hash begins with `a_` ), then you can get a link to the animated variant by using either a `GIF` or `WebP`: |
| 71 | +```cs |
| 72 | +string url; |
| 73 | +if (client.CurrentUser.IsAvatarAnimated) |
| 74 | +{ |
| 75 | + url = client.CurrentUser.GetAvatarURL(User.AvatarFormat.GIF, User.AvatarSize.x64); |
| 76 | +} |
| 77 | +else |
| 78 | +{ |
| 79 | + url = client.CurrentUser.GetAvatarURL(User.AvatarFormat.PNG, User.AvatarSize.x64); |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +Depending on your platform, you will need to decode and animate these files yourself. |
| 84 | + |
| 85 | +> [!WARNING] |
| 86 | +> If the user has a Default avatar, using anything other than PNG will throw an exception. |
| 87 | +> |
| 88 | +> Check `Avatar != null` before getting urls. |
| 89 | +
|
| 90 | +## Getting the Avatar Decoration |
| 91 | +Avatar Decorations are provided by a struct on [User.AvatarDecoration](xref:DiscordRPC.User.AvatarDecoration). These images are a set size and allows you to layer decoration on top of your users. |
| 92 | + |
| 93 | +These decorations include both a SKU and Hash, however like with avatars there are helper functions to get the correct URL to download them from. |
| 94 | +```cs |
| 95 | +string decorationUrl = client.CurrentUser.GetAvatarDecorationURL(); |
| 96 | +if (decorationUrl != null) { |
| 97 | + DownloadDecoration(decorationUrl); |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +This will return `null` if the user does not have a decoration available. |
| 102 | + |
| 103 | +### Animations |
| 104 | +Avatar Decorations are almost always animated. However, unlike Avatars, they are animated using [Animated PNG](https://en.wikipedia.org/wiki/APNG) (APNG). |
| 105 | + |
| 106 | +The `GIF` variant does not exist, `JPEG` will lose the needed transparency, and `WebP` are not animated. This is a Discord limitation. |
| 107 | + |
| 108 | +Because of this, your program must be able to parse and use `APNG` if you wish to add decorations on top of the user's avatar. |
| 109 | + |
| 110 | +## Default Avatars |
| 111 | +If the user does not have an avatar yet (or it was otherwise deleted), they will be assigned a Default Discord avatar. These avatars are automatically calculated by the user's ID (_or Discriminator if they have not yet moved to a global name_). |
| 112 | + |
| 113 | +However, Discord will only support PNG for these. To prevent frustration and unexpected 404's, this library will throw a `BadImageFormatException` when the user is using a default avatar and you request a non-png image. |
| 114 | + |
| 115 | +This guard pattern is not ideal, so it is best to check if `Avatar != null` manually before trying to get animated or compressed avatars. |
| 116 | + |
0 commit comments