Discord Rich Presence

Hello! Extremely new to java-script and I’m trying to use discordjs-rpc to make something that will change my status based on whatever window is currently active for a few select windows. (About 4 or so)

I managed to get a separate script working that will do this but only under a single “rpc.login({ clientId })” … The problem with that is that I’d very much like for it to have a separate clientId for each program so that the name of the status can properly reflect whatever window is active. I have not had any luck with commands like rpc.logout (says it didn’t exist as a command)

I’ve tried so many things but can’t seem to get this to work, right now it is just giving errors and after I switched to this new method of trying spawn.node I keep getting power-shell pop ups that quickly do nothing and close and end up having to end them in task manager. Not sure how to fix that either really. Code is somewhat long I think I probably have to post all of it so people get an idea of what I’m -trying- to do.

I run this using the ‘npm run’ in a git bash window. haven’t quite learned how to do that with visual studio yet. This is the library I’ve been using, discordjs rpc ​

​Main base script (that I would like for it to watch for when certain windows are active, and when they are it should ideally launch another script that does the rpc.login.)

const DiscordRPC = require('discord-rpc');

const monitor = require('node-active-window');

const { spawn } = require('child_process');


const clientIdGame1 = '00000000000000000';

//const clientIdGame2 = '00000000000000000000';

//const clientIdGame3 = '000000000000000000000';

//const clientIdGame4 = '0000000000000000000000';


const rpcGame1 = new DiscordRPC.Client({ transport: 'ipc' });

const rpcGame2 = new DiscordRPC.Client({ transport: 'ipc' });

const rpcGame3 = new DiscordRPC.Client({ transport: 'ipc' });

const rpcGame4 = new DiscordRPC.Client({ transport: 'ipc' });


DiscordRPC.register(clientIdGame1);

//DiscordRPC.register(clientIdGame2);

//DiscordRPC.register(clientIdGame3);

//DiscordRPC.register(clientIdGame4);


let activeRpc;


async function setActivity() {

  monitor.getActiveWindow((err, window) => {

    if (err) {

      console.error(err);

      return;

    }


const windowTitle = window.title;

if (windowTitle.includes('Game1')) {

  activeRpc = rpcGame1;

  spawn('node', ['Game1.js'], { detached: true, stdio: 'ignore' });

}

  });

}


rpcGame1.on('ready', async () => {

  setActivity();


  setInterval(() => {

    setActivity();

  }, 15 * 1000);

});


rpcGame2.on('ready', async () => {

  setActivity();


  setInterval(() => {

    setActivity();

  }, 15 * 1000);

});


rpcGame3.on('ready', async () => {

  setActivity();


  setInterval(() => {

    setActivity();

  }, 15 * 1000);

});


rpcGame4.on('ready', async () => {

  setActivity();


  setInterval(() => {

    setActivity();

  }, 15 * 1000);

});


rpcGame1.login({ clientId: clientIdGame1 }).catch(console.error);

//rpcGame2.login({ clientId: clientIdGame2 }).catch(console.error);

//rpcGame3.login({ clientId: clientIdGame3 }).catch(console.error);

//rpcGame4.login({ clientId: clientIdGame4 }).catch(console.error);

One of the 4 game scripts:

const DiscordRPC = require('discord-rpc');

const monitor = require('node-active-window');

const { spawn, exec } = require('child_process');


const clientId = '00000000000000'; //my client id

const rpc = new DiscordRPC.Client({ transport: 'ipc' });


DiscordRPC.register(clientId);


let Game1Process, Game2Process, Game3Process;


async function setActivity() {

  if (!rpc) return;


  monitor.getActiveWindow((err, window) => {

    if (err) {

      console.error(err);

      return;

    }


    const windowTitle = window.title;

    if (windowTitle.includes('Game1')) {

      rpc.setActivity({

        details: 'Game1',

        state: '     ',

//        startTimestamp: Date.now(),

        largeImageKey: 'Game1',

        largeImageText: 'Game1',

      });

      killOtherProcesses();

    }

  });

}


function killOtherProcesses() {

  if (Game2Process) {

    Game1Process.kill();

  }

  if (Game3Process) {

    Game2Process.kill();

  }

  if (Game4Process) {

    GenshinProcess.kill();

  }

}


rpc.on('ready', async () => {

  setActivity();


  setInterval(() => {

    setActivity();

  }, 15 * 1000);

});


rpc.login({ clientId }).catch(console.error);

Well for starters, Discord-RPC was deprecated 3 years ago.
Normally, i’d follow that sentence with “Go here to see the thing they replaced it with” but… uh… they’re deprecating most of that too, and dont seem to have announced a replacement?

There is mention of an RPC replacement, but it’s in private beta. So…uhhh…