Getting Started
This is a quck start guide on getting very basic presence working.
Platforms
For non-dotnet platforms, there are a variety of guides to get you started
Platform | Guide |
---|---|
.NET app | Getting Started |
Godot | 🚧 📦 Package lachee/discord-rpc-godot |
Mono | Mono Guide |
MonoGame | Mono Guide |
Unity3D | 📦 Package lachee/discord-rpc-unity |
UWP | UWP Guide |
WinForm | WinForm Guide |
The following guide will assume a standard .NET application.
Download
dotnet add package DiscordRichPresence
Download the package with NuGet to start using in your project.
Unpackaged binaries can be found in the project's Releases.
Usage
The library has 3 phases that must be followed,
- Initialization
- Rich Presence Setting
- Deinitialization and Disposal
You can set the Rich Presence of your app at any time while the client object has been constructed. The client will store the state of the presence and automatically resend it once initialized again.
Initialization
The creation of the client should happen once in the lifetime of the app. Where you put the constructor is upto your application design principles, but in general its always a good idea to put it in your initializers.
The client should be treated like a singleton and only ever created once. Multiple instances of the client can conflict with each other and cause unpredictable results within Discord and the end users Rich Presence.
public DiscordRpcClient Client { get; private set; }
void Setup() {
Client = new DiscordRpcClient("my_client_id"); //Creates the client
Client.Initialize(); //Connects the client
}
Note that the Initialize()
can be called later and the current presence state will be re-sent to the Discord Client.
Setting Rich Presence
Setting Rich Presence is easy once the client has been initialized:
//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"
}
});
You may call this as regularly as you wish, the default behaviour of the application will ignore duplicate presence and Discord itself will handle ratelimiting.
With that said, its always a good idea to only set the presence when there is actual change, to avoid any overheads.
Disposal
It is important that the client is properly disposed when finished. This will safely disconnect from Discord and dispose of the resources correctly. If you have any issues with ghosting (particularly in Unity3D), make sure you dispose the client.
//Dispose client
void Cleanup() {
client.Dispose();
}
Events
By defaults, events will be executed as they occur. This means they are executed on the RPC Thread, and not on the main. For most applications, this works fine and is treated as a normal event from any other library you may use. However, for applications where thread-safety is paramount (such as Game Engines), you may need to disable this feature and manually invoke events on your calling thread like so:
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();
}
Note
This method is only required where cross-thread talk is a big no-no. Implementing this as a Timer would just defeat the purpose as they are threaded anyways.
Code Example
The DiscordRPC.Example project contains a very basic example of setting up a client and creating your first presence.
dotnet run --framework net9 --project DiscordRPC.Example --example=Basic
Building
Check out the building guide in the CONTRIBUTING.md
Need More Help?
Still stuck? Make a new GitHub issue!