• Stars
    star
    580
  • Rank 77,010 (Top 2 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C# custom implementation for Discord Rich Presence. Not deprecated and still available!

Discord Rich Presence

Release πŸ“¦ Codacy Badge Nuget GitHub package.json version

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*.

While the official C++ library has been deprecated, this library has continued support and development for all your Rich Presence need, without requiring the Game SDK.

Here are some key features of this library:

  • 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 IntelliSense 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).

Documentation

All the documentation can be found lachee.github.io/discord-rpc-csharp/docs/

Installation

Dependencies:

  • Newtonsoft.Json
  • .NET Standard 2.0

.NET Project

For projects that target either .NET Core or .NETFX, you can get the package on nuget:

PM> Install-Package DiscordRichPresence

You can also Download or Build your own version of the library if you have more specific requirements.

Unity3D Game Engine

Unity Package is being moved to Lachee/Discord-RPC-Unity. Please check the releases / documentation there.

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. It's 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 initialization 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.
	*/
	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

Invoking is optional. Use this when thread safety is paramount.

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 safety and order of operations (especially important in Unity3D applications).

In order to enable this method of event calling, you need to set it in the constructor of the DiscordRpcClient under autoEvents.

//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

It's important that you dispose your client before your 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

Release πŸ“¦ Documentation πŸ“š

DiscordRPC Library

dotnet build -c Release

Unity3D

If you wish to have barebones Unity3D implementation, you need to build the DiscordRPC.dll, the Unity Named Pipes Library and the UnityNamedPipe.cs. Put these in your own Unity Project and the .dlls in a folder called Plugins.

More Repositories

1

raylib-goplus

A newer version of the Go bindings for Raylib
C
75
star
2

unity-named-pipes

A native named pipe wrapper for Unity3D
C#
60
star
3

discord-rpc-unity

C# custom implementation for Discord Rich Presence. Unity package to easy add to your own games.
C#
42
star
4

Unity-Package-Exporter

Exports Unity3D packages without the need of Unity3D. Perfect for build automation and CI.
C#
39
star
5

unity-utilities

Collections of tools and utilites
C#
16
star
6

fivem-busdriver

ESX Job for Bus Driving
Lua
16
star
7

starwatch

Starwatch is a Starbound Server manager with player management, crash recovery and a REST and websocket (live) API.
C#
15
star
8

fivem-garage

Customised version of a Garage Script for SkullFace's server.
Lua
8
star
9

noodle

Go WebGL game library thing. Experimental playground.
Go
7
star
10

fivem-chat

Customised version of FiveM's default chat.
TypeScript
5
star
11

Code-Spaceship-Generator

This generates a spaceship based on the tab indentation of a given text or code document. Inspired by https://twitter.com/Managore/status/577252673621102592
C#
5
star
12

unity-reference-navigator

Find all References of assets in Unity3D
C#
4
star
13

xve-docs

Visualise your discord bots with a fully featured node based editor.
JavaScript
2
star
14

discord-chat-preview

Live preview of Discord Channel
JavaScript
2
star
15

V21

Discord Bot for the V21 Server
C#
2
star
16

hypecorner-api

API for HypeCorner that handles the orchestra and blacklist
JavaScript
2
star
17

chickatrice.net

Chickatrice Website
PHP
2
star
18

spaghetti

Node Graph editor written in Go using the Noodle framework
Go
2
star
19

gall-bot

Discord bot for GALL
JavaScript
1
star
20

File-Search

Scans all files in a folder for matching regex. Useful for quickly searching in code.
C#
1
star
21

git-deploy

Lightweight HTTP service for deploying git projects remotely.
Go
1
star
22

r6stats-csharp

.NET Core library for R6Stats
C#
1
star
23

Thylacine

[DEAD] A simpler C# library for Discord ( https://discordapp.com/ ).
C#
1
star
24

webhook-publisher

Publishes Webhook events behind the scenes, freeing your frontend.
JavaScript
1
star
25

fivem-boomgate

A Boom Gate Fix
Lua
1
star
26

role-react

A simple discord bot for your private servers that will give you roles on reaction.
JavaScript
1
star
27

gall

Image sharing social network
PHP
1
star
28

pixiv-bot

Simple bot that will embed pixiv images to discord.
JavaScript
1
star
29

discord-php-kiss

A Keep It Simple Stupid approach of Discord and PHP
PHP
1
star
30

Lachee

Its me... you nosy git
1
star
31

wails-template-sveltekit

Template for Wails and Sveltekit.
Svelte
1
star
32

not-safe-to-scrape

A NSFW Web Scraper
JavaScript
1
star
33

gobbo-name-generator

Generate names for Goblins
HTML
1
star
34

koala-bot

A bot for iLoveBacons, using an advance permission system.
C#
1
star
35

reddit-download

Download Media from Reddit
TypeScript
1
star
36

hypecorner

Twitch Bot that will host Rainbow Six: Siege players that are on match point.
C#
1
star