Table of Contents

Class DiscordRpcClient

Namespace
DiscordRPC
Assembly
DiscordRPC.dll

A Discord RPC Client which is used to send Rich Presence updates and receive Join events.

public sealed class DiscordRpcClient : IDisposable
Inheritance
DiscordRpcClient
Implements
Inherited Members

Constructors

DiscordRpcClient(string)

Creates a new Discord RPC Client which can be used to send Rich Presence and receive Join events.

public DiscordRpcClient(string applicationID)

Parameters

applicationID string

The ID of the application created at discord's developers portal.

DiscordRpcClient(string, int, ILogger, bool, INamedPipeClient)

Creates a new Discord RPC Client which can be used to send Rich Presence and receive Join events. This constructor exposes more advance features such as custom NamedPipeClients and Loggers.

public DiscordRpcClient(string applicationID, int pipe = -1, ILogger logger = null, bool autoEvents = true, INamedPipeClient client = null)

Parameters

applicationID string

The ID of the application created at discord's developers portal.

pipe int

The pipe to connect too. If -1, then the client will scan for the first available instance of Discord.

logger ILogger

The logger used to report messages. If null, then a NullLogger will be created and logs will be ignored.

autoEvents bool

Should events be automatically invoked from the RPC Thread as they arrive from discord?

client INamedPipeClient

The pipe client to use and communicate to discord through. If null, the default ManagedNamedPipeClient will be used.

Properties

ApplicationID

Gets the Application ID of the RPC Client.

public string ApplicationID { get; }

Property Value

string

AutoEvents

Indicates if the client will automatically invoke the events without Invoke() having to be called.

public bool AutoEvents { get; }

Property Value

bool

Configuration

The current configuration the connection is using. Only becomes available after a ready event.

public Configuration Configuration { get; }

Property Value

Configuration

CurrentPresence

The current presence that the client has. Gets set with SetPresence(RichPresence) and updated on OnPresenceUpdate.

public RichPresence CurrentPresence { get; }

Property Value

RichPresence

CurrentUser

The current discord user. This is updated with the ready event and will be null until the event is fired from the connection.

public User CurrentUser { get; }

Property Value

User

HasRegisteredUriScheme

Gets a value indicating if the client has registered a URI Scheme. If this is false, Join events will fail.

To register a URI Scheme, call RegisterUriScheme(string, string).

public bool HasRegisteredUriScheme { get; }

Property Value

bool

IsDisposed

The dispose state of the client object.

public bool IsDisposed { get; }

Property Value

bool

IsInitialized

Represents if the client has been Initialize()

public bool IsInitialized { get; }

Property Value

bool

Logger

The logger used this client and its associated components. ILogger are not called safely and can come from any thread. It is upto the ILogger to account for this and apply appropriate thread safe methods.

public ILogger Logger { get; set; }

Property Value

ILogger

MaxQueueSize

The maximum size of the message queue received from Discord.

public int MaxQueueSize { get; }

Property Value

int

ProcessID

Gets the ID of the process used to run the RPC Client. Discord tracks this process ID and waits for its termination. Defaults to the current application process ID.

public int ProcessID { get; }

Property Value

int

ShutdownOnly

Forces the connection to shutdown gracefully instead of just aborting the connection.

This option helps prevents ghosting in applications where the Process ID is a host and the game is executed within the host (ie: the Unity3D editor). This will tell Discord that we have no presence and we are closing the connection manually, instead of waiting for the process to terminate.

public bool ShutdownOnly { get; set; }

Property Value

bool

SkipIdenticalPresence

Skips sending presences that are identical to the current one.

public bool SkipIdenticalPresence { get; set; }

Property Value

bool

SteamID

Gets the Steam ID of the RPC Client. This value can be null if none was supplied.

public string SteamID { get; }

Property Value

string

Subscription

Current subscription to events. Gets set with Subscribe(EventType), UnsubscribeMessage and updated on OnSubscribe, OnUnsubscribe.

public EventType Subscription { get; }

Property Value

EventType

TargetPipe

The pipe the discord client is on, ranging from 0 to 9. Use -1 to scan through all pipes.

This property can be used for testing multiple clients. For example, if a Discord Client was on pipe 0, the Discord Canary is most likely on pipe 1.

public int TargetPipe { get; }

Property Value

int

Methods

ClearPresence()

Clears the Rich Presence. Use this just before disposal to prevent ghosting.

public void ClearPresence()

Deinitialize()

Attempts to disconnect and deinitialize the IPC connection while retaining the settings.

public void Deinitialize()

Dispose()

Terminates the connection to Discord and disposes of the object.

public void Dispose()

Initialize()

Attempts to initalize a connection to the Discord IPC.

public bool Initialize()

Returns

bool

Invoke()

Dequeues all the messages from Discord, processes them and then invoke appropriate event handlers. This will process the message and update the internal state before invoking the events. Returns the messages that were invoked in the order they were invoked.

This method cannot be used if AutoEvents is enabled.

public IMessage[] Invoke()

Returns

IMessage[]

Returns the messages that were invoked and in the order they were invoked.

RegisterUriScheme(string, string)

Registers the application executable to a custom URI Scheme.

This is required for the Join feature. Discord will run this custom URI Scheme to launch your application when a user presses either of the buttons.

public bool RegisterUriScheme(string steamAppID = null, string executable = null)

Parameters

steamAppID string

Optional Steam ID. If supplied, Discord will launch the game through steam instead of directly calling it.

executable string

The path to the executable. If null, the path to the current executable will be used instead.

Returns

bool

Respond(JoinRequestMessage, bool)

Respond to a Join Request. All requests will timeout after 30 seconds.

Because of the 30 second timeout, it is recommended to call Invoke() faster than every 15 seconds to give your users adequate time to respond to the request.

public void Respond(JoinRequestMessage request, bool acceptRequest)

Parameters

request JoinRequestMessage

The request that is being responded too.

acceptRequest bool

Accept the join request.

Respond(User, bool)

Respond to a Join Request. All requests will timeout after 30 seconds.

Because of the 30 second timeout, it is recommended to call Invoke() faster than every 15 seconds to give your users adequate time to respond to the request.

public void Respond(User user, bool acceptRequest)

Parameters

user User

The user to respond to.

acceptRequest bool

Accept the join request.

Respond(ulong, bool)

Respond to a Join Request. All requests will timeout after 30 seconds.

Because of the 30 second timeout, it is recommended to call Invoke() faster than every 15 seconds to give your users adequate time to respond to the request.

public void Respond(ulong userID, bool acceptRequest)

Parameters

userID ulong

The ID of the user to respond to.

acceptRequest bool

Accept the join request.

SetButton(Button, int)

Updates only the Buttons of the CurrentPresence and updates the button with the given index. Returns the newly edited Rich Presence.

public RichPresence SetButton(Button button, int index = 0)

Parameters

button Button

The buttons of the Rich Presence

index int

The number of the button

Returns

RichPresence

Updated Rich Presence

SetPresence(RichPresence)

Sets the Rich Presence.

public void SetPresence(RichPresence presence)

Parameters

presence RichPresence

The Rich Presence to set on the current Discord user.

SetSubscription(EventType)

Sets the subscription to the events sent from Discord.

Requires the UriScheme to be registered.

public void SetSubscription(EventType type)

Parameters

type EventType

The new subscription as a flag. Events selected in the flag will be subscribed too and the other events will be unsubscribed.

Subscribe(EventType)

Subscribes to an event sent from discord. Used for Join feature.

Requires the UriScheme to be registered.

public void Subscribe(EventType type)

Parameters

type EventType

The event type to subscribe to

SynchronizeState()

Resends the current presence and subscription. This is used when Ready is called to keep the current state within discord.

public void SynchronizeState()

Unsubscribe(EventType)

Unsubscribe from the event sent by discord. Used for Join feature.

Requires the UriScheme to be registered.

public void Unsubscribe(EventType type)

Parameters

type EventType

The event type to unsubscribe from

Update(Action<RichPresence>)

Updates the values assigned in the delegate passed

public RichPresence Update(Action<RichPresence> func)

Parameters

func Action<RichPresence>

Delegate used to update the rich presence

Returns

RichPresence

Updated Rich Presence

UpdateButtons(Button[])

Updates only the Buttons of the CurrentPresence and updates/removes the buttons. Returns the newly edited Rich Presence.

public RichPresence UpdateButtons(Button[] buttons = null)

Parameters

buttons Button[]

The buttons of the Rich Presence

Returns

RichPresence

Updated Rich Presence

UpdateClearTime()

Sets the start and end time of CurrentPresence to null and sends it to Discord.

public RichPresence UpdateClearTime()

Returns

RichPresence

Updated Rich Presence

UpdateDetails(string)

Updates only the Details of the CurrentPresence and sends the updated presence to Discord. Returns the newly edited Rich Presence.

public RichPresence UpdateDetails(string details)

Parameters

details string

The details of the Rich Presence

Returns

RichPresence

Updated Rich Presence

UpdateEndTime()

Sets the end time of the CurrentPresence to now and sends the updated presence to Discord.

public RichPresence UpdateEndTime()

Returns

RichPresence

Updated Rich Presence

UpdateEndTime(DateTime)

Sets the end time of the CurrentPresence and sends the updated presence to Discord.

public RichPresence UpdateEndTime(DateTime time)

Parameters

time DateTime

The new time for the end

Returns

RichPresence

Updated Rich Presence

UpdateLargeAsset(string, string)

Updates the large Assets of the CurrentPresence and sends the updated presence to Discord. Both key and tooltip are optional and will be ignored it null.

public RichPresence UpdateLargeAsset(string key = null, string tooltip = null)

Parameters

key string

Optional: The new key to set the asset too

tooltip string

Optional: The new tooltip to display on the asset

Returns

RichPresence

Updated Rich Presence

UpdateParty(Party)

Updates only the Party of the CurrentPresence and sends the updated presence to Discord. Returns the newly edited Rich Presence.

public RichPresence UpdateParty(Party party)

Parameters

party Party

The party of the Rich Presence

Returns

RichPresence

Updated Rich Presence

UpdatePartySize(int)

Updates the Size of the CurrentPresence and sends the update presence to Discord. Returns the newly edited Rich Presence.

Will return null if no presence exists and will throw a new NullReferenceException if the Party does not exist.

public RichPresence UpdatePartySize(int size)

Parameters

size int

The new size of the party. It cannot be greater than Max

Returns

RichPresence

Updated Rich Presence

UpdatePartySize(int, int)

Updates the Size of the CurrentPresence and sends the update presence to Discord. Returns the newly edited Rich Presence.

Will return null if no presence exists and will throw a new NullReferenceException if the Party does not exist.

public RichPresence UpdatePartySize(int size, int max)

Parameters

size int

The new size of the party. It cannot be greater than Max

max int

The new size of the party. It cannot be smaller than Size

Returns

RichPresence

Updated Rich Presence

UpdateSecrets(Secrets)

Updates the Secrets of the CurrentPresence and sends the updated presence to Discord. Will override previous secret entirely.

public RichPresence UpdateSecrets(Secrets secrets)

Parameters

secrets Secrets

The new secret to send to discord.

Returns

RichPresence

Updated Rich Presence

UpdateSmallAsset(string, string)

Updates the small Assets of the CurrentPresence and sends the updated presence to Discord. Both key and tooltip are optional and will be ignored it null.

public RichPresence UpdateSmallAsset(string key = null, string tooltip = null)

Parameters

key string

Optional: The new key to set the asset too

tooltip string

Optional: The new tooltip to display on the asset

Returns

RichPresence

Updated Rich Presence

UpdateStartTime()

Sets the start time of the CurrentPresence to now and sends the updated presence to Discord.

public RichPresence UpdateStartTime()

Returns

RichPresence

Updated Rich Presence

UpdateStartTime(DateTime)

Sets the start time of the CurrentPresence and sends the updated presence to Discord.

public RichPresence UpdateStartTime(DateTime time)

Parameters

time DateTime

The new time for the start

Returns

RichPresence

Updated Rich Presence

UpdateState(string)

Updates only the State of the CurrentPresence and sends the updated presence to Discord. Returns the newly edited Rich Presence.

public RichPresence UpdateState(string state)

Parameters

state string

The state of the Rich Presence

Returns

RichPresence

Updated Rich Presence

UpdateStatusDisplayType(StatusDisplayType)

Updates only the StatusDisplay of the CurrentPresence and sends the updated presence to Discord. Returns the newly edited Rich Presence.

public RichPresence UpdateStatusDisplayType(StatusDisplayType type)

Parameters

type StatusDisplayType

The type to display on the status

Returns

RichPresence

Updated Rich Presence

UpdateType(ActivityType)

Updates only the Type of the CurrentPresence and sends the updated presence to Discord. Returns the newly edited Rich Presence.

public RichPresence UpdateType(ActivityType type)

Parameters

type ActivityType

The type of the Rich Presence

Returns

RichPresence

Updated Rich Presence

Events

OnClose

Called when connection to the Discord Client is lost. The connection will remain close and unready to accept messages until the Ready event is called again.

If AutoEvents is true then this event will execute on a different thread. If it is not true however, then this event is not invoked untill Invoke() and will be on the calling thread.

public event OnCloseEvent OnClose

Event Type

OnCloseEvent

OnConnectionEstablished

The connection to the discord client was succesfull. This is called before Ready.

If AutoEvents is true then this event will execute on a different thread. If it is not true however, then this event is not invoked untill Invoke() and will be on the calling thread.

public event OnConnectionEstablishedEvent OnConnectionEstablished

Event Type

OnConnectionEstablishedEvent

OnConnectionFailed

Failed to establish any connection with discord. Discord is potentially not running?

If AutoEvents is true then this event will execute on a different thread. If it is not true however, then this event is not invoked untill Invoke() and will be on the calling thread.

public event OnConnectionFailedEvent OnConnectionFailed

Event Type

OnConnectionFailedEvent

OnError

Called when a error has occured during the transmission of a message. For example, if a bad Rich Presence payload is sent, this event will be called explaining what went wrong.

If AutoEvents is true then this event will execute on a different thread. If it is not true however, then this event is not invoked untill Invoke() and will be on the calling thread.

public event OnErrorEvent OnError

Event Type

OnErrorEvent

OnJoin

Called when the Discord Client wishes for this process to join a game.

If AutoEvents is true then this event will execute on a different thread. If it is not true however, then this event is not invoked untill Invoke() and will be on the calling thread.

public event OnJoinEvent OnJoin

Event Type

OnJoinEvent

OnJoinRequested

Called when another discord user requests permission to join this game.

This event is not invoked untill Invoke() is executed.

public event OnJoinRequestedEvent OnJoinRequested

Event Type

OnJoinRequestedEvent

OnPresenceUpdate

Called when the Discord Client has updated the presence.

If AutoEvents is true then this event will execute on a different thread. If it is not true however, then this event is not invoked untill Invoke() and will be on the calling thread.

public event OnPresenceUpdateEvent OnPresenceUpdate

Event Type

OnPresenceUpdateEvent

OnReady

Called when the discord client is ready to send and receive messages.

If AutoEvents is true then this event will execute on a different thread. If it is not true however, then this event is not invoked untill Invoke() and will be on the calling thread.

public event OnReadyEvent OnReady

Event Type

OnReadyEvent

OnRpcMessage

The RPC Connection has sent a message. Called before any other event and executed from the RPC Thread.

public event OnRpcMessageEvent OnRpcMessage

Event Type

OnRpcMessageEvent

OnSpectate

Called when the Discord Client wishes for this process to spectate a game.

If AutoEvents is true then this event will execute on a different thread. If it is not true however, then this event is not invoked untill Invoke() and will be on the calling thread.

[Obsolete("Spectating is no longer supported by Discord.")]
public event OnSpectateEvent OnSpectate

Event Type

OnSpectateEvent

OnSubscribe

Called when the Discord Client has subscribed to an event.

If AutoEvents is true then this event will execute on a different thread. If it is not true however, then this event is not invoked untill Invoke() and will be on the calling thread.

public event OnSubscribeEvent OnSubscribe

Event Type

OnSubscribeEvent

OnUnsubscribe

Called when the Discord Client has unsubscribed from an event.

If AutoEvents is true then this event will execute on a different thread. If it is not true however, then this event is not invoked untill Invoke() and will be on the calling thread.

public event OnUnsubscribeEvent OnUnsubscribe

Event Type

OnUnsubscribeEvent