### Run DiscordRPC.Example Project Source: https://github.com/lachee/discord-rpc-csharp/blob/master/README.md Execute the example project with a specific framework and example name to test various functionalities. ```sh dotnet run --framework net9 --project DiscordRPC.Example --example=Basic ``` -------------------------------- ### Running the Joining Example Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/joining.md These commands demonstrate how to run the Discord RPC C# joining example on two separate pipes. Ensure you have the correct framework and project specified. ```bash # Run on the first pipe dotnet run --framework net9 --project DiscordRPC.Example --example=Joining --pipe=0 # Run on the second pipe dotnet run --framework net9 --project DiscordRPC.Example --example=Joining --pipe=1 ``` -------------------------------- ### Get Avatar Decoration URL Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/avatars.md This example demonstrates how to retrieve the URL for a user's avatar decoration. It checks if a decoration is available and calls a helper function to get the URL. ```csharp string decorationUrl = client.CurrentUser.GetAvatarDecorationURL(); if (decorationUrl != null) { DownloadDecoration(decorationUrl); } ``` -------------------------------- ### Basic Discord Rich Presence Setup Source: https://github.com/lachee/discord-rpc-csharp/blob/master/README.md Initialize the DiscordRpcClient, set up basic events, connect to Discord, and set a Rich Presence with details, state, assets, and buttons. ```csharp using DiscordRPC; public const string DISCORD_APP_ID = "424087019149328395"; public static DiscordRpcClient client; public static void Main() { // Create the client and setup some basic events client = new DiscordRpcClient(DISCORD_APP_ID) { Logger = new Logging.ConsoleLogger(Logging.LogLevel.Info, true) }; client.OnReady += (sender, e) => { Console.WriteLine("Connected to discord with user {0}", msg.User.Username); Console.WriteLine("Avatar: {0}", msg.User.GetAvatarURL(User.AvatarFormat.WebP)); Console.WriteLine("Decoration: {0}", msg.User.GetAvatarDecorationURL()); }; //Connect to the RPC client.Initialize(); //Set the rich presence client.SetPresence(new RichPresence() { Details = "A Basic Example", State = "In Game", Assets = new Assets() { LargeImageKey = "image_large", LargeImageText = "Lachee's Discord IPC Library", SmallImageKey = "image_small" }, Buttons = new Button[] { new Button() { Label = "lachee.dev", Url = "https://lachee.dev/" } } }); // ... Do Stuff ... Console.ReadKey(); // Cleanup client.Dispose(); } ``` -------------------------------- ### Set Up a Party with Initial Size Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/joining.md Use the Party object to define a party's ID, privacy, current size, and maximum capacity. This is useful for initializing a party when a user starts the game. ```csharp client.SetPresence(new() { Details = "Party Example", State = "In Game", Party = new() { ID = "my-unique-party-id", Privacy = Party.PrivacySetting.Private, Size = 1, Max = 4, }, }); ``` -------------------------------- ### Download User Avatar using WebClient Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/avatars.md This example demonstrates downloading a user's avatar to a local file using the System.Net.WebClient class. Ensure you have the avatar URL first. ```csharp string url = client.CurrentUser.GetAvatarURL(User.AvatarFormat.PNG, User.AvatarSize.x128); using (var client = new System.Net.WebClient()) { client.DownloadFile(url, "avatar.png"); } ``` -------------------------------- ### Add DiscordRichPresence Package Source: https://github.com/lachee/discord-rpc-csharp/blob/master/README.md Install the DiscordRichPresence NuGet package to your project. ```sh dotnet add package DiscordRichPresence ``` -------------------------------- ### Get User Avatar URL on Ready Event Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/avatars.md This snippet shows how to access the user's username and avatar URL once the client is ready. It demonstrates logging the username and the generated avatar URL. ```csharp client.OnReady += (sender, msg) => { //Create some events so we know things are happening Console.WriteLine("Connected to discord with user {0}", msg.User.Username); Console.WriteLine("Avatar URL: {0}", msg.User.GetAvatarURL(User.AvatarFormat.PNG, User.AvatarSize.x128)); }; ``` -------------------------------- ### Get Animated Avatar URL Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/avatars.md This code snippet shows how to conditionally get the URL for an animated avatar (GIF) if available, otherwise falling back to a static PNG. It checks the IsAvatarAnimated property. ```csharp string url; if (client.CurrentUser.IsAvatarAnimated) { url = client.CurrentUser.GetAvatarURL(User.AvatarFormat.GIF, User.AvatarSize.x64); } else { url = client.CurrentUser.GetAvatarURL(User.AvatarFormat.PNG, User.AvatarSize.x64); } ``` -------------------------------- ### Initialize Discord RPC client Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/getting_started/introduction.md Create and initialize the client once during application startup. The client should be treated as a singleton to avoid conflicts. ```csharp public DiscordRpcClient Client { get; private set; } void Setup() { Client = new DiscordRpcClient("my_client_id"); //Creates the client Client.Initialize(); //Connects the client } ``` -------------------------------- ### Build Project in Release Mode Source: https://github.com/lachee/discord-rpc-csharp/blob/master/CONTRIBUTING.md Builds the project using the .NET CLI in Release configuration. This command is typically used for creating production-ready builds. ```bash dotnet build -c Release ``` -------------------------------- ### Set Rich Presence with details and assets Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/getting_started/introduction.md Configure and send rich presence data including details, state, and asset information to Discord. ```csharp //Set Presence client.SetPresence(new RichPresence() { Details = "Example Project", State = "csharp example", Assets = new Assets() { LargeImageKey = "image_large", LargeImageText = "Lachee's Discord IPC Library", SmallImageKey = "image_small" } }); ``` -------------------------------- ### Add runFullTrust Capability to Package.appxmanifest Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/getting_started/uwp.md This XML snippet shows how to add the 'runFullTrust' capability to the Package.appxmanifest file, which is necessary for WIN UI 3 related applications like .NET MAUI. ```xml ... ``` -------------------------------- ### Register Default URI Scheme Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/joining.md Register the default URI scheme for your application to enable Discord to launch your game. This must be done before initializing the DiscordRpcClient. ```csharp client.RegisterUriScheme(); client.Initialize(); ``` -------------------------------- ### Subscribe to Join Request Event and Respond Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/joining.md Subscribe to the JoinRequest event to handle users asking to join a private lobby. Respond to Discord to accept or reject the request. ```csharp client.Subscribe(EventType.JoinRequest); client.OnJoinRequested += async (object sender, JoinRequestMessage args) => { var result = await UI.ShowRequestToJoin(args.user); // Hypothetical UI system client.Respond(args.User, result.accepted); }; ``` -------------------------------- ### Download User Avatar using HttpClient Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/avatars.md This snippet shows how to download a user's avatar using the modern HttpClient class and save it to a file asynchronously. This is the recommended approach for new applications. ```csharp // Or using HttpClient (recommended for modern applications) using (var client = new HttpClient()) { byte[] imageBytes = await client.GetByteArrayAsync(url); await File.WriteAllBytesAsync("avatar.png", imageBytes); } ``` -------------------------------- ### Load User Avatar in Unity3D Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/avatars.md This coroutine demonstrates how to download and load a user's avatar as a Unity Texture2D using UnityWebRequestTexture. It handles the asynchronous download process within Unity. ```csharp // Or if you are using Unity3D IEnumerator LoadAvatar() { string url = client.CurrentUser.GetAvatarURL(User.AvatarFormat.PNG, User.AvatarSize.x128); UnityWebRequest request = UnityWebRequestTexture.GetTexture(url); yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.Success) { Texture2D texture = DownloadHandlerTexture.GetContent(request); // Use the texture (e.g., assign to a material or UI image) } } ``` -------------------------------- ### Subscribe to Join Event and Join Lobby Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/joining.md Subscribe to the Join event to receive a secret when a user clicks the 'Join' button in Discord. Use this secret to join the lobby. ```csharp client.Subscribe(EventType.Join); client.OnJoin += (object sender, JoinMessage args) => { Lobby.JoinWithToken(args.Secret); }; ``` -------------------------------- ### Set Dynamic Assets for Rich Presence Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/assets.md Assigns dynamic images to your Rich Presence activity using URLs. This is useful for external content like album art but be mindful of URL length limits and potential hot-link protection. ```csharp // omitted: // HTTP GET https://coverartarchive.org/release/6bf261f9-f18b-4ba8-92be-7afc36575f2d client.SetPresence(new RichPresence() { Type = ActivityType.Listening, Details = "Invisible", State = "Duran Duran", Timestamps = Timestamps.FromTimeSpan(191), Assets = new Assets() { LargeImageKey = "https://coverartarchive.org/release/6bf261f9-f18b-4ba8-92be-7afc36575f2d/33192928727-500.jpg", // Links to the image LargeImageUrl = "https://music.youtube.com/watch?v=SMCd5zrsFpE" // Links to the song when clicked LargeImageText = "Future Past", }, }); ``` -------------------------------- ### Register Steam URI Scheme Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/joining.md Register a specific URI scheme for a Steam game by providing its App ID. This allows Discord to launch your Steam game directly. ```csharp client.RegisterUriScheme("657300"); client.Initialize(); ``` -------------------------------- ### Set Static Assets for Rich Presence Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/assets.md Assigns static images to your Rich Presence activity using keys defined in the Discord Developer Portal. Use this for game maps, characters, or loadouts. ```csharp client.SetPresence(new RichPresence() { Type = ActivityType.Playing, Details = "A Basic Example", State = "In Game", Assets = new Assets() { LargeImageKey = "image_large", LargeImageText = "Lachee's Discord IPC Library", SmallImageKey = "koala" }, }); ``` -------------------------------- ### Set Presence with Watching Activity and Timestamp Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/timestamps.md Use this snippet to set a 'Watching' activity with a specific duration displayed as a timer. This is useful for indicating how long you've been watching something. ```csharp client.SetPresence(new RichPresence() { Type = ActivityType.Watching, StatusDisplay = StatusDisplayType.Details, Details = "Skibidi Toilet - Season 1", State = "DaFuq!?Boom!", Timestamps = Timestamps.FromTimeSpan(66), }); ``` -------------------------------- ### Set Discord Rich Presence with Buttons Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/buttons.md Use this snippet to set a Rich Presence with two clickable buttons. Ensure you have the necessary RichPresence and Button classes available from the discord-rpc-csharp library. ```csharp client.SetPresence(new RichPresence() { Details = "A Basic Example", State = "In Game", Buttons = new Button[] { new Button() { Label = "Fish", Url = "https://lachee.dev/" }, new Button() { Label = "Sticks", Url = "https://en.wikipedia.org/wiki/Stick" } } }); ``` -------------------------------- ### Set Join Secret for Lobby Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/features/joining.md Include a 'Join' secret in your presence data to allow other users to join your game lobby. This secret should contain information needed to connect to the lobby, such as a unique lobby ID. ```csharp client.SetPresence(new() { Details = "Party Example", State = "In Game", Party = new() { /* .. Party Details .. */ }, // This is how a launching app will figure out how to connect to the game. Secrets = new() { Join = Lobby.CreateJoinToken(Lobby.current.ID) }, }); ``` -------------------------------- ### Dispose Discord RPC client Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/getting_started/introduction.md Properly clean up and disconnect the client when the application terminates to avoid resource leaks and ghosting issues. ```csharp //Dispose client void Cleanup() { client.Dispose(); } ``` -------------------------------- ### Thread-Safe Timer for Discord RPC Invocation Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/getting_started/winform.md Use a System.Timers.Timer to ensure thread-safe invocation of Discord RPC client methods within a WinForm application. The timer interval should be set appropriately for your application's needs. ```csharp var timer = new System.Timers.Timer(150); timer.Elapsed += (sender, args) => { client.Invoke(); }; timer.Start(); ``` -------------------------------- ### Handle events with manual thread invocation Source: https://github.com/lachee/discord-rpc-csharp/blob/master/Docfx/articles/getting_started/introduction.md Disable automatic event invocation and manually invoke events on the calling thread for thread-safe applications like game engines. Call Invoke() once per frame or update cycle. ```csharp void Start() { //Creates a new client, telling it not to automatically invoke the events on RPC thread. Client = new DiscordRpcClient("my_client_id", autoEvents: false); Client.Initialize(); } void Update() { //Invoke the events once per-frame. The events will be executed on calling thread. Client.Invoke(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.