in

Discord Rich Presence – JavaScript – SitePoint Forums

cf11b3ed553d6c359d3dcc08377cc4b121fe8bf0

[ad_1]

Hello! I’m very new to javascript and I’m trying to use discordjs-rpc to create something that changes the status based on the currently active window in some select windows. (about 4 animals)

I was able to get another script that does this to work, but only under a single “rpc. I want to. I want the name of the status to properly reflect the active window. I had no luck with commands like rpc.logout (says it didn’t exist as a command)

I have tried so many things and can’t seem to get this to work. Right now I’m just getting an error and after switching to this new way of trying spawn.node I keep getting a powershell popup that does nothing immediately. I have to close them and end them in task manager. I don’t even know how to actually fix it. The code is rather long, so I think I should probably post everything so people can understand what I’m trying to do.

This is done using ‘npm run’ in a git bash window. Haven’t learned how to do that in visual studio yet. This is the library I’m using, discordjs rpc

Main base script (I need to monitor when a particular window becomes active, ideally it should launch another script that runs 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 four 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);

[ad_2]

Source link

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

    468513 062853 updates

    CSS 2023 test paper sparks discussion on social media

    poster a417133236a443fca7e08dd792637e61

    Utah Students Compete in National Competition with Safety App