stoat-for-desktop/src/native/discordRpc.ts

45 lines
927 B
TypeScript

import { Client } from "discord-rpc";
import { config } from "./config";
// internal state
let rpc: Client;
export async function initDiscordRpc() {
if (!config.discordRpc) return;
// clean up existing client if one exists
rpc?.removeAllListeners();
try {
rpc = new Client({ transport: "ipc" });
rpc.on("ready", () =>
rpc.setActivity({
state: "stoat.qinbeans.net",
details: "Chatting with others",
largeImageKey: "qr",
largeImageText: "Join Stoat!",
buttons: [
{
label: "Join Stoat",
url: "https://stoat.qinbeans.net/",
},
],
}),
);
rpc.on("disconnected", reconnect);
rpc.login({ clientId: "872068124005007420" });
} catch (err) {
reconnect();
}
}
const reconnect = () => setTimeout(() => initDiscordRpc(), 1e4);
export async function destroyDiscordRpc() {
rpc?.destroy();
}