Skip to content

Commit

Permalink
added *preliminary* discord rich presence support
Browse files Browse the repository at this point in the history
  • Loading branch information
bdotsamir committed Dec 17, 2022
1 parent c9e2369 commit 2a2b9b9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ tauri-build = { version = "1.1", features = [] }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.1", features = ["api-all"] }
discord-rich-presence = "0.2.3"

[features]
# by default Tauri runs in production mode
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL
default = [ "custom-protocol" ]
default = ["custom-protocol"]
# this feature is used used for production builds where `devPath` points to the filesystem
# DO NOT remove this
custom-protocol = [ "tauri/custom-protocol" ]
custom-protocol = ["tauri/custom-protocol"]
37 changes: 37 additions & 0 deletions src-tauri/src/drpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::{thread, time::Duration};

use discord_rich_presence::{
activity::{self, Activity, Assets, Timestamps},
DiscordIpc, DiscordIpcClient,
};

pub fn set_details(ns_version: String, project_name: String) {
let mut client = DiscordIpcClient::new("998040133829922817").expect("Discord auth error");

// this is required to maintain a connection.
loop {
if client.connect().is_ok() {
break;
}
}

loop {

let large_text = format!("v{}", &ns_version);

let assets = Assets::new()
.large_image("nodestudio")
.large_text(large_text.as_str());

let details = format!("Working on {}", &project_name);
let activity = Activity::new()
.details(details.as_str())
.assets(assets);

if client.set_activity(activity).is_err() && client.reconnect().is_ok() {
continue;
}

thread::sleep(Duration::from_secs(10));
}
}
16 changes: 15 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,29 @@
windows_subsystem = "windows"
)]

use std::thread;
mod drpc;

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}

#[tauri::command]
fn enable_drpc(ns_version: String, project_name: String) {

thread::spawn(move || {
drpc::set_details(ns_version, project_name);
});

}

fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.invoke_handler(tauri::generate_handler![greet, enable_drpc])
.run(tauri::generate_context!())
.expect("error while running tauri application");

// drpc::set_details();
}
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import memory from "./memory";
import setProjectTitle from './util/set-project-title';
import isInterface from './util/is-interface';

import { invoke } from "@tauri-apps/api";
import { getVersion } from "@tauri-apps/api/app";

console.warn(`reminder: cursor changes don't work with the console open!!!`);

memory.masterVolume = memory.audioCTX.createGain();
Expand Down Expand Up @@ -31,9 +34,11 @@ const pTitle: HTMLInputElement | null = document.querySelector(`#projectTitle`);

if (!pTitle) {
throw new Error(`projectTitle element not found`);
}6
}

setProjectTitle("APP TITLE TESTING");

setProjectTitle(pTitle.value || pTitle.placeholder);
invoke("enable_drpc", { nsVersion: await getVersion(), projectName: "TEST PROJECT NAME" });

import('./ui/panel-manager.js');
import('./ui/slider');
Expand All @@ -50,7 +55,7 @@ window.addEventListener(`keydown`, (e: KeyboardEvent) => {
if (isInterface<HTMLInputElement>(e.target, `type`)) {