Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Node.JS Discord Rich Presence

DarkKitten

New Coder
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 here

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.)

JavaScript:
const DiscordRPC = require('discord-rpc');
const monitor = require('node-active-window');
const { spawn } = require('child_process');

const clientIdGame1 = '00000000000000';
//const clientIdGame2 = '0000000000000000';
//const clientIdGame3 = '000000000000000000';
//const clientIdGame4 = '000000000000000000';

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:

Code:
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);
 
Last edited:

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom