Skip to content

NinetailLabs/discord-rpc-csharp

 
 

Repository files navigation

Discord Rich Presence

Codacy Badge

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)
  • Fully documented (including which fields are obsolete)
  • Helper Functionality (eg: AvatarURL generator from Join Requests)
  • Ghost Prevention (Tells discord to clear the RP on disposal)
  • 🆕 HTTP Rich Presence support!

*Managed Pipes is unavailable for the Unity3D game engine due to technical limitations and a substitute native wrapper library is required.

Doesn't RPC require whitelisting?

RPC: CLOSED,
RPC - Rich Presence: OPEN,
RPC - Rich Presence - Join / Specate: CLOSED
  • All features within RPC need to be given permission and whitelisted (excluding Rich Presence)
  • Rich Presence Join / Spectate feature is whitelist only (but private testing allowed)

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* or above

*Unity3D can run in .NET 2.0, however it cannot run in .NET 2.0 subset **Unity3D requires the DiscordRPC.Native library to be built

Standard .NET (build) For standard use, the only project that needs to be build is Discord RPC. This will create a managed .dll for use in your .NET projects. Simply right-click the project and click build. The .dll will be generated at <project>/Discord RPC/bin. Copy this .dll to your target project and add it as a reference.

Standard .NET (nuget) There is currently no nuget package available. One will be released in the future when this project is in a stable state.

Unity3D Game Engine Unity3D has a limitation with its managed version of pipes. It does not support asynchronous mode (even in .NET 4.6) and will block waiting for a read to finish. Aborting a read will cause a hard freeze of the editor.

This library provides a solution that should only be used when this problem occurs. It is a native library and as such is platform specific and needs to be built for every planned release platform (win32, win64, mac, linux).

To build the library with the native pipes, you want to build Discord RPC and DiscordRPC.Native. Copy both the .dll generated by the libraries and paste them within your project folder. If you are using the .NET 2.0 within unity, you are required to copy the DiscordRPC.Native dll to the root of the project folder (outside assets).

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.

Connecting

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(ClientID, true, DiscordPipe))					
	
	//Set the logger
	client.Logger = new ConsoleLogger() { Level = LogLevel.Warning };

	//Subscribe to events
	client.OnPresenceUpdate += (sender, e) =>
	{
		Console.WriteLine("Received Update! {0}", e.Presence);
	};
	
	//Connect to the RPC
	client.Initialize();

	//Set the rich 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"
		}
	});	
}

Invoking Events Make sure you call invoke regularly to dequeue stored messages received by discord. You can either call Invoke() to call the events or manually dequeue and process the messages with DequeueMessages().

//The main loop of your application, or some sort of timer.
void Update() 
{
	//Invoke all the events, such as OnPresenceUpdate
	client.Invoke();
}

Disposing 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();
}

Special Usage

If you do not wish to support the Join / Request feature and just want a quick and simple way to send messages, you may want to use the WebRPC instead. This will prepare payloads for you to send with HTTP Post to the discord client. It will generate the url, payload and headings, all you need to do is then pass them to your implementation of web requests.

WebClient Example

var request = DiscordRPC.Web.WebRPC.PrepareRequest(presence, ClientID);
RichPresence response = null;

//Now we need to send it. We are using WebClient as a example
using (var web = new System.Net.WebClient())
{
	//Copy over the headers
	foreach (var kp in request.Headers)
		web.Headers.Add(kp.Key, kp.Value);

	//Make the request
	string json = web.UploadString(request.URL, request.Data);

	//Try to parse the request
	if (!DiscordRPC.Web.WebRPC.TryParseResponse(json, out response))
	{
		//We failed to set the rich presence
		Console.WriteLine("Something went wrong while trying to parse the response!");
		Console.WriteLine("Received: {0}", json);
	}
	else
	{
		//We succesfully set the rich presence!
		Console.WriteLine("Succesfully set Rich Presence! State: {0}", response.State);
	}
}

Unity3D 2017+ Example

IEnumeratable SetPresence()
{
	var request = DiscordRPC.Web.WebRPC.PrepareRequest(presence, ClientID);
	RichPresence response = null;

	//TODO: Example coming soon.
}

Build script parameters

The following parameters can be passed to the build script:

  1. target
    Valid parameters are:
  • Default has the same effect as executing the script without the parameter
  • NugetBuild will build a Nuget pacakge and attempt to push it to the Nuget repository (This is geared for running from a build server like AppVeyor)
  1. ScriptArgs
    Can be used to pass one or both of the following build arguments:
  • buildCounter is used to know how many builds the server has generated and is used to generate a CI version number for continuous Nuget releases - Defaults to 0
  • buildType can be used to pass the MSBuild configuration that should be execute - Defaults to Release

To run the script on a build server, pass the following line:

.\build.ps1 -target NugetBuild -ScriptArgs '-buildCounter=1','-buildType="Release"'

About

C# Library for Discord Rich Presence

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 93.5%
  • PowerShell 3.7%
  • C++ 2.6%
  • C 0.2%