Discord Rich Presence
This is a C# implementation of the Discord RPC library which was originally written in C++. This avoids having to use the official C++ and instead provides a managed way of using the Rich Presence within the .NET environment*.
This library supports all features of the Rich Presence that the official C++ library supports, plus a few extra:
- Message Queuing
- Threaded Reads
- Managed Pipes*
- Error Handling & Error Checking with automatic reconnects
- Events from Discord (such as presence update and join requests)
- Full Rich Presence Implementation (including Join / Spectate)
- Inline Documented (for all your intelli-sense needs)
- Helper Functionality (eg: AvatarURL generator from Join Requests)
- Ghost Prevention (Tells discord to clear the RP on disposal)
- Full Unity3D Editor (Contains all the tools, inspectors and helpers for a Unity3D game all in one package).
Installation
Within the Visual Studio solution, there are 3 projects. The main library is located in Discord RPC
, the example project is DiscordRPC.Example
and a native pipe wrapper is DiscordRPC.Native
.
Dependencies:
- Newtonsoft.Json
- .NET 3.5+
Unity3D can run in .NET 2.0 (not subset) but requires the DiscordRPC.Native library included in the project.
Standard .NET (build)
There is currently no nuget package available, but one will be included soon. At the moment, to include this library simply Download or Build the library and include it as an assembly reference.
Unity3D Game Engine
There is a Unity Package available for quick setup, which includes the editor scripts, managers and tools to make your life 100x easier. Simply download the package from the Artifacts AppVoyer generates. This includes the native library and the managed library prebuilt, so you dont need to worry about a thing.
If you wish to manually include it yourself without the fancy tools I made, you can simply include the DiscordRPC.dll
and DiscordRPC.Native.dll
you Downloaded or Built and include them into your unity project. Make sure the native library is only included on the appropriate platforms (Windows 32bit and 64bit).
Usage
The Discord.Example project within the solution contains example code, showing how to use all available features. For Unity Specific examples, check out the example project included. There are 3 important stages of usage, Initialization, Invoking and Deinitialization. Its important you follow all 3 stages to ensure proper behaviour of the library.
Initialization
This stage will setup the connection to Discord and establish the events. Once you have done the intialization you can call SetPresence
and other variants as many times as you wish throughout your code. Please note that ideally this should only run once, otherwise conflicts may occur with them trying to access the same discord client at the same time.
public DiscordRpcClient client;
//Called when your application first starts.
//For example, just before your main loop, on OnEnable for unity.
void Initialize()
{
/*
Create a discord client
NOTE: If you are using Unity3D, you must use the full constructor and define
the pipe connection as DiscordRPC.IO.NativeNamedPipeClient
*/
client = new DiscordRpcClient("my_client_id");
//Set the logger
client.Logger = new ConsoleLogger() { Level = LogLevel.Warning };
//Subscribe to events
client.OnReady += (sender, e) =>
{
Console.WriteLine("Received Ready from user {0}", e.User.Username);
};
client.OnPresenceUpdate += (sender, e) =>
{
Console.WriteLine("Received Update! {0}", e.Presence);
};
//Connect to the RPC
client.Initialize();
//Set the rich presence
//Call this as many times as you want and anywhere in your code.
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"
}
});
}
Invoking
This stage is very important and is often missed. The client will store messages from the pipe and won't invoke them until you call Invoke()
or DequeueMessages()
. It does this because the pipe is working on another thread, and manually invoking ensures proper thread saftey and order of operations (especially important in Unity3D applications).
//The main loop of your application, or some sort of timer. Literally the Update function in Unity3D
void Update()
{
//Invoke all the events, such as OnPresenceUpdate
client.Invoke();
};
Here is an example on how a Timer could be used to invoke the events for a WinForm
var timer = new System.Timers.Timer(150);
timer.Elapsed += (sender, args) => { client.Invoke(); };
timer.Start();
Deinitialization
Its important that you dispose your client before you application terminates. This will stop the threads, abort the pipe reads and tell discord to clear the presence. Failure to do so may result in a memory leak!
//Called when your application terminates.
//For example, just after your main loop, on OnDisable for unity.
void Deinitialize()
{
client.Dispose();
}
Building
Whats Required
Type | DiscordRPC | DiscordRPC.Native* | DiscordRPC.Example |
---|---|---|---|
WinForm App | X | ||
Console App | X | ||
Mono Game | X | * If Issues Persist | |
Unity3D | X | X |
- DiscordRPC.Native is platform specific!
I recommend downloading the Unity Package
for all Unity3D projects from the Artifacts. Its automatically built and guaranteed to be the latest. I also recommend downloading the DiscordRPC.dll
itself from the artifacts.
The native client I recommend building yourself however, since the artifacts only contains a 32bit version.
You can build the solution easily in Visual Studio, its a simple matter of right clicking the project and hitting build. However if you wish to build via command line, you can do so with the PowerShell build script:
.\build.ps1 -target Default -ScriptArgs '-buildType="Release"'
The Nuget project is TBA.