#tauri-plugin #discord-rich-presence #rich-presence #discord #tauri #ipc

sys tauri-plugin-drpc

A plugin for Tauri that adds support for Discord Rich Presence

5 releases

0.1.4 Feb 19, 2025
0.1.3 Feb 4, 2025
0.1.2 Jan 28, 2025
0.1.1 Jan 27, 2025
0.1.0 Jan 26, 2025

#517 in GUI

Download history 254/week @ 2025-01-26 115/week @ 2025-02-02 28/week @ 2025-02-09 8/week @ 2025-02-16

405 downloads per month

GPL-3.0-only

13KB
144 lines

Tauri Plugin DRPC

A plugin for Tauri that allows you to control Discord Rich Presence.

[!NOTE] This plugin is designed to be used on Desktop only.

Table of contents

Concept

The plugin implements an interface (as a JavaScript plugin) between JavaScript and crate for Rust discord-rich-presence (we have our own fork of it - rpcdiscord).

Right now the plugin works as follows: A developer has to call a function (spawn) in the code that sparks a thread, and that thread keeps Discord Rich Presence running, constantly updating the activity in it. See Usage

[!NOTE] We are now looking at an option without using the creation of separate threads, since Tauri allows you to do asynchronous commands.

Installation

version

Open a terminal, and in the root folder of your project, type these commands:

cd src-tauri
cargo add tauri-plugin-drpc

or just type this line into src-tauri/Cargo.toml.

tauri-plugin-drpc = "*"

Then install the package for JavaScript (in root folder of your project):

npm i tauri-plugin-drpc

Usage

Thread managment

As I said earlier (here), to use Discord Rich Presence you need to spawn a thread, which is what will run Rich Presence.

This can be done through the start function:

import { start } from "tauri-plugin-drpc";

// start(APPLICATION_ID)
await start("700000000000000000");

The start function checks that the drpc thread is not running, if it is running it stops it and then starts a new one.

This means that the start function cannot throw an error, and it is safe to use.

If you need to start a thread without checks, you can use the spawn function.

import { spawn } from "tauri-plugin-drpc";

try {
  // spawn(APPLICATION_ID)
  await spawn("700000000000000000");
} catch (err) {
  console.error(err);
}

If you need to, you can stop the thread yourself via the stop function.

The stop function is as safe as the spawn function, it stops the thread only if it is running, so it does not throw an error.

import { stop } from "tauri-plugin-drpc";

await stop();

And there is a function destroy which stops the thread without checks and may throw an error, so it should be wrapped in try {} catch (...).

import { destroy } from "tauri-plugin-drpc";

try {
  await destroy();
} catch (err) {
  console.error(err);
}

Activity managment

[!NOTE] You can get the Application ID in the settings of your Discord application, in the General Information tab.

You can now customize your Discord Rich Presence activity. This can be done through the setActivity function. But before that, you need to create an activity object. I have written about this below.

You can also clear the activity via clearActivity.

Example:

import { setActivity, clearActivity, destroy } from "tauri-plugin-drpc";
import { Activity } from "tauri-plugin-drpc";

const activity = new Activity()
  .setState("example string")
  .setState("hello")
  .setTimestamps(new Timestamps(Date.now())

await setActivity(activity);

setTimeout(async () => {
  console.log("Clearing activity");

  // clear drpc's activity
  await clearActivity();
  // destroy drpc's thread
  await destroy();
}, 3000)

Activity components

Assets

Assets is responsible for the large and small icons on the left side of the activity.

import { Assets, Activity } from "tauri-plugin-drpc/activity";

const assets = new Assets()
  .setLargeImage("IMAGE_ID")
  .setLargeText("Large image hovered!")
  .setSmallImage("IMAGE_ID")
  .setSmallText("Small image hovered!")

const activity = new Activity()
  .setAssets(assets);

Buttons

Button add buttons to your activity, when clicked, the user will have the link given to each button open in the browser.

import { Button } from "tauri-plugin-drpc/activity";

const activity = new Activity()
  .setButtons([
    new Button("Button text #1", "https://example.com"),
    new Button("Button text #2", "https://example.com"),
  ]);

Party

There is no documentation for the Party component, so you, dear friend, will have to check the discord-rich-presence crate documentation to understand how this component works.

Timestamps

import { Timestamps, Activity } from "tauri-plugin-drpc/activity";

const timestamp = new Timestamps(Date.now() - 1000000); // 1000 seconds ago

const activity = new Activity()
  .setTimestamps(timestamp);

Dependencies

~17–52MB
~843K SLoC